update to latest SoftPosit (w/ critical bugfixes)
[sfpy.git] / README.md
1 # sfpy
2 softfloat and softposit in Python
3 * support for softfloat float16, float32, and float64
4 * support for softposit posit8, quire8, posit16, and quire16
5
6 ## Demo
7 ```
8 >>> import sfpy
9 >>> sfpy.Float32(1.3) + sfpy.Float32(1.4) # <-- construct from doubles
10 Float32(2.6999998092651367)
11 >>> sfpy.Float32(3) # <-- construct from raw bits
12 Float32(4.203895392974451e-45)
13 >>> sfpy.Float32(3).bits
14 3
15 >>> x = sfpy.Float16(0)
16 >>> x
17 Float16(0.0)
18 >>> x += sfpy.Float16(10.0) # <-- in-place operators have better performance
19 >>> x
20 Float16(10.0)
21 >>> sfpy.Posit16(1.3) + sfpy.Posit16(1.4) # <-- posits work the same way as floats
22 Posit16(2.7001953125)
23 >>> q = sfpy.Quire16(0) # <-- quire is also supported
24 >>> q
25 Quire16(0.0)
26 >>> q.iqam(sfpy.Posit16(3), sfpy.Posit16(5))
27 >>> q
28 Quire16(3.725290298461914e-09)
29 >>> q.iqam(sfpy.Posit16(3.0), sfpy.Posit16(5.0))
30 >>> q
31 Quire16(15.0)
32 >>> q.bits
33 1080863910568919232
34 >>> bin(q.bits)
35 '0b111100000000000000000000000000000000000000000000000011000000'
36 ```
37
38 ## Building (on Linux)
39 The Cython module can be built in place in the usual way:
40
41 `python setup.py build_ext --inplace`
42
43 This requires the submodules to be checked out, and the static libraries `SoftPosit/build/Linux-x86_64-GCC/softposit.a` and `berkeley-softfloat-3/build/Linux-x86_64-GCC/softfloat.a` to be built. Note that in order for Cython to be able to build the shared objects for the module, the static libraries must be compiled with -fPIC, which currently requires modifying the appropriate Makefiles manually. SoftPosit can be build with -fPIC using its python2 and python3 targets.
44
45 The package can also be installed to a local Python distribution with pip, i.e. `pip install .` from the top level of the repository using the appropriate pip. This requires that Cython be installed.