Skip to content

Commit

Permalink
Add casting from FlexFloat objects to float (#2)
Browse files Browse the repository at this point in the history
* Add casting from `FlexFloat` objects to `float`

* Update `README.md`

* ci: Add build and tests
  • Loading branch information
colluca authored Mar 19, 2024
1 parent c17eea6 commit 7dd0e9e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 ETH Zurich and University of Bologna.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

name: Test PyFlexFloat library
on: [push, pull_request]

jobs:
test:
name: Build and test package
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Build
run: pip install .
- name: Install requirements
run: pip install -r requirements.txt
- name: Test
run: ./test/test.py
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
## PyFlexFloat

A Python wrapper around OPRECOMP's [FlexFloat](https://github.com/oprecomp/flexfloat) library.
A Python wrapper around OPRECOMP's [FlexFloat](https://github.com/oprecomp/flexfloat) library, enabling arbitrary precision floating-point arithmetic emulation in Python.

## Installation

Clone the repository:
`
git clone https://github.com/colluca/pyflexfloat.git
`
Simply install via pip:
```
pip install pyflexfloat
```

Enter the local repository folder:
`
cd pyflexfloat
`
For developers, you can create an editable installation, to test your changes to the source code:
```
pip install -e .
```

Install in your active Python environment:
`
pip install .
`

Test installation:
`
Test the installation:
```
./test/test.py
`
```
5 changes: 4 additions & 1 deletion pyflexfloat/FlexFloat.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, desc, val=None):
lib.ff_init(self.ptr, desc[0])

def __str__(self):
return str(lib.ff_get_double(self.ptr))
return str(float(self))

def _uniformize(self, other):
"""Ensures that `self` and `other` have compatible types.
Expand Down Expand Up @@ -193,6 +193,9 @@ def __neg__(self):
def __abs__(self):
return -self if self < 0 else self

def __float__(self):
return lib.ff_get_double(self.ptr)

def sqrt(self):
result = FlexFloat(self.desc)
lib.ff_sqrt(result.ptr, self.ptr)
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[build-system]
requires = [
"setuptools>=60",
"setuptools>=65.0",
"setuptools-scm>=8.0",
"cmake_build_extension==0.5.2.dev15",
"cffi"
]
build-backend = "setuptools.build_meta"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
#
# Author: Luca Colagrande <[email protected]>
import setuptools
import cmake_build_extension
from setuptools.command.build import build
import subprocess
from pathlib import Path


class CMakeBuild(setuptools.command.build.build):
class CMakeBuild(build):

def run(self):
# Required to generate "flexfloat_config.h"
Expand Down
11 changes: 11 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

# Test FlexFloat initialization from bytes
print('= FlexFloat(..., bytes) =')
a = FlexFloat('e5m2', 3.14)
b = FlexFloat('e5m2', a.bits().to_bytes(1, 'big'))
print(f'bits: {b.bitstring()}')
print(f'hex: {hex(b.bits())}')
Expand All @@ -86,3 +87,13 @@
print(f'a: {a}')
print(f'-a: {-a}')
print(f'abs(a): {abs(a)}')

# Casting
print('= Casting =')
A = np.random.rand(length).astype(np.float32)
B = ff.array(A, 'fp32')
C = B.astype(np.float32)
D = B.astype(np.float64)
E = B.astype(np.float16)
F = ff.array(A, 'fp16')
print(A, B, C, D, E, F)

0 comments on commit 7dd0e9e

Please sign in to comment.