import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__),'..'))


from decimal import Decimal
from decimal import getcontext
from FormulaSolidityPort import fixedLog2


MIN_PRECISION = 32
MAX_PRECISION = 127


getcontext().prec = MAX_PRECISION
ln2 = Decimal(2).ln()


fixedLog2MaxInput  = ((1<<(256-MAX_PRECISION))-1)<<MAX_PRECISION
fixedLog2MaxOutput = fixedLog2(fixedLog2MaxInput,MAX_PRECISION)


FLOOR_LN2_EXPONENT   = int((((1<<256)-1)/(fixedLog2MaxOutput*ln2)).ln()/ln2)
FLOOR_LN2_MANTISSA   = int(2**FLOOR_LN2_EXPONENT*ln2)
CEILING_LN2_MANTISSA = int(ln2*(1<<MIN_PRECISION)+1)


print '    uint256 constant CEILING_LN2_MANTISSA = 0x{:x};'.format(CEILING_LN2_MANTISSA)
print '    uint256 constant FLOOR_LN2_MANTISSA   = 0x{:x};'.format(FLOOR_LN2_MANTISSA  )
print '    uint8   constant FLOOR_LN2_EXPONENT   = {:d};'  .format(FLOOR_LN2_EXPONENT  )

from decimal import Decimal
from decimal import getcontext
from decimal import ROUND_FLOOR
from decimal import ROUND_CEILING


MIN_PRECISION = 32
MAX_PRECISION = 127


def ln(n):
    return Decimal(n).ln()


def log2(n):
    return ln(n)/ln(2)


def floor(d):
    return int(d.to_integral_exact(rounding=ROUND_FLOOR))


def ceiling(d):
    return int(d.to_integral_exact(rounding=ROUND_CEILING))


getcontext().prec = MAX_PRECISION


maxVal = floor(log2(2**(256-MAX_PRECISION)-1)*2**MAX_PRECISION)-1


FLOOR_LN2_EXPONENT   = floor(log2((2**256-1)/(maxVal*ln(2))))
FLOOR_LN2_MANTISSA   = floor(2**FLOOR_LN2_EXPONENT*ln(2))
CEILING_LN2_MANTISSA = ceiling(2**MIN_PRECISION*ln(2))


print '    uint256 constant CEILING_LN2_MANTISSA = 0x{:x};'.format(CEILING_LN2_MANTISSA)
print '    uint256 constant FLOOR_LN2_MANTISSA   = 0x{:x};'.format(FLOOR_LN2_MANTISSA  )
print '    uint8   constant FLOOR_LN2_EXPONENT   = {:d};'  .format(FLOOR_LN2_EXPONENT  )

Make this constant-generating script independent of the solidity emulation module.
Make this constant-generating script independent of the solidity emulation module.
