<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> import veriloggen . core . vtypes as vtypes <EOL> def to_fixed ( value , point , signed = False ) : <EOL> if point < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if point == <NUM_LIT:0> : <EOL> return value <EOL> if isinstance ( value , ( int , float ) ) : <EOL> if not isinstance ( point , int ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> mag = <NUM_LIT:2> ** point <EOL> return int ( value * mag ) <EOL> if hasattr ( value , '<STR_LIT>' ) and value . signed : <EOL> signed = True <EOL> return shift_left ( value , point , signed ) <EOL> def fixed_to_int ( value , point , signed = False ) : <EOL> if point < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if point == <NUM_LIT:0> : <EOL> return value <EOL> if isinstance ( value , ( int , float ) ) : <EOL> if not isinstance ( point , int ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> mag = <NUM_LIT:2> ** point <EOL> return float ( value ) / mag <EOL> if hasattr ( value , '<STR_LIT>' ) and value . signed : <EOL> signed = True <EOL> return shift_right ( value , point , signed ) <EOL> def fixed_to_int_low ( value , point ) : <EOL> if point < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if point == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> if isinstance ( value , ( int , float ) ) : <EOL> if not isinstance ( point , int ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> mag = <NUM_LIT:2> ** point <EOL> return ( float ( value ) / mag ) % <NUM_LIT:1.0> <EOL> return vtypes . And ( value , vtypes . Repeat ( vtypes . Int ( <NUM_LIT:1> , <NUM_LIT:1> ) , point ) ) <EOL> def fixed_to_real ( value , point , signed = False ) : <EOL> if point < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if point == <NUM_LIT:0> : <EOL> return vtypes . SystemTask ( '<STR_LIT>' , value ) <EOL> if not isinstance ( value , vtypes . _Variable ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if hasattr ( value , '<STR_LIT>' ) and value . signed : <EOL> signed = True <EOL> msb = value [ value . width - <NUM_LIT:1> ] <EOL> v0 = ( vtypes . SystemTask ( '<STR_LIT>' , fixed_to_int ( value , point ) ) + <EOL> vtypes . SystemTask ( '<STR_LIT>' , fixed_to_int_low ( value , point ) ) / ( <NUM_LIT:2> ** point ) ) <EOL> nv = vtypes . Unot ( value ) + <NUM_LIT:1> <EOL> v1 = ( ( vtypes . SystemTask ( '<STR_LIT>' , fixed_to_int ( nv , point ) ) + <EOL> vtypes . SystemTask ( '<STR_LIT>' , fixed_to_int_low ( nv , point ) ) / ( <NUM_LIT:2> ** point ) ) ) * - <NUM_LIT:1> <EOL> return vtypes . Mux ( signed and msb == <NUM_LIT:0> , v0 , v1 ) <EOL> def adjust ( left , right , lpoint , rpoint , signed = True ) : <EOL> diff_lpoint = rpoint - lpoint <EOL> diff_rpoint = lpoint - rpoint <EOL> if diff_lpoint < <NUM_LIT:0> : diff_lpoint = <NUM_LIT:0> <EOL> if diff_rpoint < <NUM_LIT:0> : diff_rpoint = <NUM_LIT:0> <EOL> ldata = left if diff_lpoint == <NUM_LIT:0> else shift_left ( left , diff_lpoint , signed ) <EOL> rdata = right if diff_rpoint == <NUM_LIT:0> else shift_left ( right , diff_rpoint , signed ) <EOL> if signed : <EOL> ldata = vtypes . SystemTask ( '<STR_LIT>' , ldata ) <EOL> rdata = vtypes . SystemTask ( '<STR_LIT>' , rdata ) <EOL> return ldata , rdata <EOL> def shift_left ( value , size , signed = True ) : <EOL> if isinstance ( value , vtypes . Int ) : <EOL> v = value . value & <NUM_LIT> <EOL> ret = value <EOL> for i in range ( size ) : <EOL> ret = ( ret << <NUM_LIT:1> ) | v <EOL> return ret <EOL> if isinstance ( value , int ) : <EOL> v = value & <NUM_LIT> <EOL> ret = value <EOL> for i in range ( size ) : <EOL> ret = ( ret << <NUM_LIT:1> ) | v <EOL> return ret <EOL> if isinstance ( value , bool ) : <EOL> return value << size <EOL> if not isinstance ( value , vtypes . _Variable ) : <EOL> raise TypeError ( "<STR_LIT>" % str ( type ( value ) ) ) <EOL> if signed : <EOL> return vtypes . Cat ( value , vtypes . Repeat ( value [ <NUM_LIT:0> ] , size ) ) <EOL> return vtypes . Sll ( value , size ) <EOL> def shift_right ( value , size , signed = True ) : <EOL> if signed : <EOL> return vtypes . Sra ( value , size ) <EOL> return vtypes . Srl ( value , size ) </s>
