| 1 |
""" |
| 2 |
This module exposes the functions longlong2float() and float2longlong(), |
| 3 |
which cast the bit pattern of a float into a long long and back. |
| 4 |
Warning: don't use in the other direction, i.e. don't cast a random |
| 5 |
long long to a float and back to a long long. There are corner cases |
| 6 |
in which it does not work. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import with_statement |
| 10 |
import sys |
| 11 |
from rpython.annotator import model as annmodel |
| 12 |
from rpython.rlib.rarithmetic import r_int64, intmask, r_longlong, r_int |
| 13 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 14 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 15 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 16 |
|
| 17 |
if r_longlong is r_int: |
| 18 |
r_rettype = int |
| 19 |
else: |
| 20 |
r_rettype = r_longlong |
| 21 |
|
| 22 |
|
| 23 |
UINT_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.UINT)) |
| 24 |
FLOAT_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.FLOAT)) |
| 25 |
|
| 26 |
import struct |
| 27 |
|
| 28 |
|
| 29 |
def longlong2float(llval): |
| 30 |
return struct.unpack('@d', struct.pack('@q', llval))[0] |
| 31 |
|
| 32 |
def float2longlong(floatval): |
| 33 |
return r_rettype(struct.unpack('@q', struct.pack('@d', floatval))[0]) |
| 34 |
|
| 35 |
def uint2singlefloat_emulator(ival): |
| 36 |
with lltype.scoped_alloc(FLOAT_ARRAY_PTR.TO, 1) as f_array: |
| 37 |
i_array = rffi.cast(UINT_ARRAY_PTR, f_array) |
| 38 |
i_array[0] = ival |
| 39 |
singlefloatval = f_array[0] |
| 40 |
return singlefloatval |
| 41 |
|
| 42 |
def singlefloat2uint_emulator(singlefloatval): |
| 43 |
with lltype.scoped_alloc(FLOAT_ARRAY_PTR.TO, 1) as f_array: |
| 44 |
i_array = rffi.cast(UINT_ARRAY_PTR, f_array) |
| 45 |
f_array[0] = singlefloatval |
| 46 |
ival = i_array[0] |
| 47 |
return ival |
| 48 |
|
| 49 |
eci = ExternalCompilationInfo(includes=['string.h', 'assert.h'], |
| 50 |
post_include_bits=[""" |
| 51 |
static float pypy__uint2singlefloat(unsigned int x) { |
| 52 |
float ff; |
| 53 |
assert(sizeof(float) == 4 && sizeof(unsigned int) == 4); |
| 54 |
memcpy(&ff, &x, 4); |
| 55 |
return ff; |
| 56 |
} |
| 57 |
static unsigned int pypy__singlefloat2uint(float x) { |
| 58 |
unsigned int ii; |
| 59 |
assert(sizeof(float) == 4 && sizeof(unsigned int) == 4); |
| 60 |
memcpy(&ii, &x, 4); |
| 61 |
return ii; |
| 62 |
} |
| 63 |
"""]) |
| 64 |
|
| 65 |
uint2singlefloat = rffi.llexternal( |
| 66 |
"pypy__uint2singlefloat", [rffi.UINT], rffi.FLOAT, |
| 67 |
_callable=uint2singlefloat_emulator, compilation_info=eci, |
| 68 |
_nowrapper=True, elidable_function=True, sandboxsafe=True) |
| 69 |
|
| 70 |
singlefloat2uint = rffi.llexternal( |
| 71 |
"pypy__singlefloat2uint", [rffi.FLOAT], rffi.UINT, |
| 72 |
_callable=singlefloat2uint_emulator, compilation_info=eci, |
| 73 |
_nowrapper=True, elidable_function=True, sandboxsafe=True) |
| 74 |
|
| 75 |
|
| 76 |
class Float2LongLongEntry(ExtRegistryEntry): |
| 77 |
_about_ = float2longlong |
| 78 |
|
| 79 |
def compute_result_annotation(self, s_float): |
| 80 |
assert annmodel.SomeFloat().contains(s_float) |
| 81 |
return annmodel.SomeInteger(knowntype=r_int64) |
| 82 |
|
| 83 |
def specialize_call(self, hop): |
| 84 |
[v_float] = hop.inputargs(lltype.Float) |
| 85 |
hop.exception_cannot_occur() |
| 86 |
return hop.genop("convert_float_bytes_to_longlong", [v_float], resulttype=lltype.SignedLongLong) |
| 87 |
|
| 88 |
class LongLong2FloatEntry(ExtRegistryEntry): |
| 89 |
_about_ = longlong2float |
| 90 |
|
| 91 |
def compute_result_annotation(self, s_longlong): |
| 92 |
assert annmodel.SomeInteger(knowntype=r_int64).contains(s_longlong) |
| 93 |
return annmodel.SomeFloat() |
| 94 |
|
| 95 |
def specialize_call(self, hop): |
| 96 |
[v_longlong] = hop.inputargs(lltype.SignedLongLong) |
| 97 |
hop.exception_cannot_occur() |
| 98 |
return hop.genop("convert_longlong_bytes_to_float", [v_longlong], resulttype=lltype.Float) |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
nan_high_word_int32 = -2 |
| 106 |
nan_encoded_zero = r_int64(nan_high_word_int32 << 32) |
| 107 |
|
| 108 |
def encode_int32_into_longlong_nan(value): |
| 109 |
return (nan_encoded_zero + |
| 110 |
rffi.cast(rffi.LONGLONG, rffi.cast(rffi.UINT, value))) |
| 111 |
|
| 112 |
def decode_int32_from_longlong_nan(value): |
| 113 |
return rffi.cast(lltype.Signed, rffi.cast(rffi.INT, value)) |
| 114 |
|
| 115 |
def is_int32_from_longlong_nan(value): |
| 116 |
return intmask(value >> 32) == nan_high_word_int32 |
| 117 |
|
| 118 |
CAN_ALWAYS_ENCODE_INT32 = (sys.maxint == 2147483647) |
| 119 |
|
| 120 |
def can_encode_int32(value): |
| 121 |
if CAN_ALWAYS_ENCODE_INT32: |
| 122 |
return True |
| 123 |
return value == rffi.cast(lltype.Signed, rffi.cast(rffi.INT, value)) |
| 124 |
|
| 125 |
def can_encode_float(value): |
| 126 |
return intmask(float2longlong(value) >> 32) != nan_high_word_int32 |
| 127 |
|
| 128 |
def maybe_decode_longlong_as_float(value): |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if is_int32_from_longlong_nan(value): |
| 139 |
return float(decode_int32_from_longlong_nan(value)) |
| 140 |
else: |
| 141 |
return longlong2float(value) |