Skip to content

Commit

Permalink
Merge pull request #279 from robtaylor/robtaylor/liberty-speed
Browse files Browse the repository at this point in the history
Faster version of liberty_float - gives a 25% to 50% speedup
  • Loading branch information
mithro committed Jan 13, 2021
2 parents 3d7617a + 9d39c63 commit f6f76f3
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions scripts/python-skywater-pdk/skywater_pdk/liberty.py
Expand Up @@ -31,12 +31,15 @@

from typing import Tuple, List, Dict

from math import frexp, log2

from . import sizes
from .utils import sortable_extracted_numbers


debug = False

LOG2_10 = log2(10)

class TimingType(enum.IntFlag):
"""
Expand Down Expand Up @@ -766,6 +769,15 @@ def liberty_float(f):
>>> liberty_float(1)
'1.0000000000'
>>> liberty_float(1e9)
'1000000000.0'
>>> liberty_float(1e10)
'1.000000e+10'
>>> liberty_float(1e15)
'1.000000e+15'
>>> liberty_float(True)
Traceback (most recent call last):
...
Expand All @@ -792,36 +804,25 @@ def liberty_float(f):
"""
try:
f2 = float(f)
r = float(f)
except (ValueError, TypeError):
f2 = None
r = None

if isinstance(f, bool):
f2 = None
r = None

if f is None or f2 != f:
if f is None or r != f:
raise ValueError("%r is not a float" % f)

WIDTH = len(str(0.0083333333))

s = json.dumps(f)
if 'e' in s:
a, b = s.split('e')
if '.' not in a:
a += '.'
while len(a)+len(b)+1 < WIDTH:
a += '0'
s = "%se%s" % (a, b)
elif '.' in s:
while len(s) < WIDTH:
s += '0'
else:
if len(s) < WIDTH:
s += '.'
while len(s) < WIDTH:
s += '0'
return s
width = 11

mag = int(frexp(r)[1]/LOG2_10)
if mag > 9:
return f'%{width}e' % r
if mag < 0:
return f"%{width+1}.{width-1}f" % r
else:
return f"%{width+1}.{width-mag-1}f" % r

LIBERTY_ATTRIBUTE_TYPES = {
'boolean': liberty_bool,
Expand Down

0 comments on commit f6f76f3

Please sign in to comment.