-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline_abi.py
61 lines (50 loc) · 1.99 KB
/
inline_abi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#=======================================================================
# inline_abi.py
#=======================================================================
#
# See: https://cffi.readthedocs.org/en/latest/cdef.html
#
# CFFI's ABI mode requires that a shared library be compiled separately,
# then imported using the CFFI dlopen() interface.
#
# To prepare the .so file, run:
#
# - gcc -fPIC -shared softfloat-c/*.c -o libsoftfloat.so
#
# Now softfloat is ready to import into Python using the code below.
import cffi
from softfloat import bits2float, float2bits
#-----------------------------------------------------------------------
# cffi import lib
#-----------------------------------------------------------------------
ffi = cffi.FFI()
ffi.cdef('''
typedef uint32_t float32_t;
typedef uint64_t float64_t;
typedef struct { uint64_t v; uint16_t x; } floatx80_t;
typedef struct { uint64_t v[ 2 ]; } float128_t;
int_fast8_t softfloat_exceptionFlags;
float32_t f32_add( float32_t a, float32_t b );
''')
lib = ffi.dlopen('./libsoftfloat.so')
#-----------------------------------------------------------------------
# testing
#-----------------------------------------------------------------------
assert bits2float(0x3f800000) == 1.0
def test_add( is_inexact, out, a, b ):
a_bits = float2bits(a)
b_bits = float2bits(b)
print 'expected:', float2bits(out), out, lib.softfloat_exceptionFlags
out_bits = lib.f32_add( a_bits, b_bits )
print 'actual: ', out_bits, bits2float( out_bits ), is_inexact
assert float2bits(out) == out_bits
assert is_inexact == lib.softfloat_exceptionFlags
if out != bits2float( out_bits ):
print "WARNING: bits match but float conversion doesn't!"
print
lib.softfloat_exceptionFlags = 0
test_add( 0, 3.5, 2.5, 1.0 )
test_add( 1, -1234, -1235.1, 1.1 )
test_add( 1, 3.14159265, 3.14159265, 0.00000001 )
test_add( 0, 3.5, 2.5, 1.0 )
test_add( 1, -1234, -1235.1, 1.1 )