在Perl中,有没有比打包打包更快的方法来获取浮点位?不必是IEEE754
答案 0 :(得分:3)
您的问题毫无道理。您需要一种快速的方法来缓慢地做某事。如果我们知道you really wanted to do,我们可以提供更好的解决方案。
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);