简体   繁体   中英

Is there a faster way to get floating point bits than pack/unpack in Perl?

Is there a faster way to get floating point bits than pack/unpack in Perl? It is not necessary to be IEEE754

Your question makes no sense. You're asking for the fast way to do something the slow way. If we knew what you really wanted to do , we could provide a better solution.

use strict;
use warnings;
use feature qw( say );

use Inline C => <<'__EOS__';

   void cast_double_to_uv(SV* sv) {
      dXSARGS;

      double d = (double)SvNV(sv);
      UV uv;

      if (sizeof(uv) < sizeof(double))
         croak("Integers too small to hold a double.");

      if (BYTEORDER == 0x1234) {
         /* Little-endian */
         memcpy(&uv, &d, sizeof(double));

         if (sizeof(uv) > sizeof(double))
            memset(((char*)&uv)+sizeof(double), 0, sizeof(double)-sizeof(uv));
      } else {
         /* Big-endian */
         if (sizeof(uv) > sizeof(double))
            memset(&uv, 0, sizeof(double)-sizeof(uv));

         memcpy(((char*)&uv)+sizeof(double)-sizeof(uv), &d, sizeof(double));
      }

      XSRETURN_UV(uv);
   }

__EOS__

say cast_double_to_uv(5.5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM