From a36c656f8c9d760693fc98a5450878bb5365f411 Mon Sep 17 00:00:00 2001 From: Bill Zorn Date: Wed, 25 Jul 2018 13:59:07 -0700 Subject: [PATCH] move things into sfpy package --- setup.py | 11 +++++-- sfpy/__init__.py | 6 ++++ cposit.pxd => sfpy/cposit.pxd | 2 +- posit.pyx => sfpy/posit.pyx | 0 timing.py | 55 +++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 sfpy/__init__.py rename cposit.pxd => sfpy/cposit.pxd (98%) rename posit.pyx => sfpy/posit.pyx (100%) create mode 100644 timing.py diff --git a/setup.py b/setup.py index 82b066d..2aa76e2 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,8 @@ from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize -ext = Extension('posit', ['posit.pyx'], +posit_ext = Extension( + 'sfpy.posit', ['sfpy/posit.pyx'], include_dirs=['SoftPosit/source/include/'], extra_objects=['./SoftPosit/build/Linux-x86_64-GCC/softposit.a'], libraries=['m'], @@ -10,5 +11,11 @@ ext = Extension('posit', ['posit.pyx'], setup( name='sfpy', - ext_modules=cythonize([ext]), + version='0.1.0', + description='softfloat and softposit in python', + author='Bill Zorn', + author_email='bill.zorn@gmail.com', + url='https://github.com/billzorn/sfpy', + packages=['sfpy'], + ext_modules=cythonize([posit_ext]), ) diff --git a/sfpy/__init__.py b/sfpy/__init__.py new file mode 100644 index 0000000..d213f2b --- /dev/null +++ b/sfpy/__init__.py @@ -0,0 +1,6 @@ +import os +mypath = os.path.abspath(os.path.dirname(__file__)) + +print('hi from module sfpy @ {:s}'.format(mypath)) + +from .posit import Posit8, Quire8, Posit16, Quire16 diff --git a/cposit.pxd b/sfpy/cposit.pxd similarity index 98% rename from cposit.pxd rename to sfpy/cposit.pxd index 913a96b..c7c4c3e 100644 --- a/cposit.pxd +++ b/sfpy/cposit.pxd @@ -1,6 +1,6 @@ from libc.stdint cimport * -cdef extern from 'SoftPosit/source/include/softposit.h': +cdef extern from '../SoftPosit/source/include/softposit.h': ctypedef struct posit8_t: pass diff --git a/posit.pyx b/sfpy/posit.pyx similarity index 100% rename from posit.pyx rename to sfpy/posit.pyx diff --git a/timing.py b/timing.py new file mode 100644 index 0000000..ff13387 --- /dev/null +++ b/timing.py @@ -0,0 +1,55 @@ +"""Some basic performance tests for the wrapper class""" + +import timeit + + +def ops_sec(stmt, setup): + # print('----') + # print(setup) + print('-- timing: --') + print(stmt) + print('----') + + timer = timeit.Timer(stmt, setup) + iters, time = timer.autorange() + + rate = iters/time + if rate > 1000000000: + erate = rate / 1000000000 + unit = 'Gops' + elif rate > 1000000: + erate = rate / 1000000 + unit = 'Mops' + elif rate > 1000: + erate = rate / 1000 + unit = 'kops' + else: + erate = rate + unit = 'ops' + + print('{} ops, {} s, {} {}/s\n'.format(iters, time, erate, unit)) + + +setup = """import sfpy +P8 = sfpy.Posit8 +Q8 = sfpy.Quire8 +a = P8(1.3) +b = P8(-0.5) +c = P8(15.0) +q = Q8() +zero = 0.0 +""" + +ops_sec('pass', setup) +ops_sec('P8(0)', setup) +ops_sec('P8("0")', setup) +ops_sec('P8.from_bits(0)', setup) + +ops_sec('1.1 + 2', setup) +ops_sec('a + b', setup) +ops_sec('a.add(b)', setup) +ops_sec('zero += 1', setup) +ops_sec('a += b', setup) +ops_sec('a.iadd(b)', setup) + +ops_sec('q.fdp_add(b, c)', setup) -- 2.30.2