Commit 65d8042b authored by Cerlane Leong's avatar Cerlane Leong 💬

Modified python to accept bits directly, map NaR to integer 0 and pX1 unsafed...

Modified python to accept bits directly, map NaR to integer 0 and pX1 unsafed files that will not be compiled
parent 448b86d0
......@@ -811,6 +811,32 @@ class ui32_pX2(_object):
ui32_pX2_swigregister = _softposit.ui32_pX2_swigregister
ui32_pX2_swigregister(ui32_pX2)
class ui64_double(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, ui64_double, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, ui64_double, name)
__repr__ = _swig_repr
__swig_setmethods__["ui"] = _softposit.ui64_double_ui_set
__swig_getmethods__["ui"] = _softposit.ui64_double_ui_get
if _newclass:
ui = _swig_property(_softposit.ui64_double_ui_get, _softposit.ui64_double_ui_set)
__swig_setmethods__["d"] = _softposit.ui64_double_d_set
__swig_getmethods__["d"] = _softposit.ui64_double_d_get
if _newclass:
d = _swig_property(_softposit.ui64_double_d_get, _softposit.ui64_double_d_set)
def __init__(self):
this = _softposit.new_ui64_double()
try:
self.this.append(this)
except __builtin__.Exception:
self.this = this
__swig_destroy__ = _softposit.delete_ui64_double
__del__ = lambda self: None
ui64_double_swigregister = _softposit.ui64_double_swigregister
ui64_double_swigregister(ui64_double)
class ui32_q8(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, ui32_q8, name, value)
......@@ -1459,11 +1485,24 @@ def convertToColor(i, ps, es):
return colored+"\033[0m"
class posit8:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p8(value)
else:
self.v = _softposit.convertDoubleToP8(value)
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit8_t()
self.v.v = bits & 0xFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit8_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p8(value)
else:
self.v = _softposit.convertDoubleToP8(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit8'
def __add__(self, other):
......@@ -1686,8 +1725,9 @@ class posit8:
self.v = _softposit.p8_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p8_sqrt(self.v)
return self
a = posit8(0)
a.v = _softposit.p8_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP8ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -1707,8 +1747,9 @@ class posit8:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit8(0)
a.v.toNaR();
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -1803,11 +1844,24 @@ class quire8:
self.v.toHex()
class posit16:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p16(value);
else:
self.v = _softposit.convertDoubleToP16(value);
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit16_t()
self.v.v = bits & 0xFFFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit16_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p16(value)
else:
self.v = _softposit.convertDoubleToP16(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit16'
def __add__(self, other):
......@@ -2027,8 +2081,9 @@ class posit16:
self.v = _softposit.p16_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p16_sqrt(self.v)
return self
a = posit16(0)
a.v = _softposit.p16_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP16ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -2048,8 +2103,9 @@ class posit16:
def isNaR(self):
return self.v.isNaR()
def toNaR(self):
self.v.toNaR()
return self
a = posit16(0)
a.v.toNaR()
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -2145,11 +2201,24 @@ class quire16:
self.v.toHex()
class posit32:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p32(value);
else:
self.v = _softposit.convertDoubleToP32(value);
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit32_t()
self.v.v = bits&0xFFFFFFFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit32_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p32(value)
else:
self.v = _softposit.convertDoubleToP32(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit32'
def __add__(self, other):
......@@ -2235,9 +2304,9 @@ class posit32:
def __eq__(self, other):
try:
if isinstance(other, (int)):
a.v = _softposit.p32_eq(self.v, _softposit.i64_to_p32(other))
return _softposit.p32_eq(self.v, _softposit.i64_to_p32(other))
elif isinstance(other, (float)):
a.v = _softposit.p32_eq(self.v, _softposit.convertDoubleToP32(other))
return _softposit.p32_eq(self.v, _softposit.convertDoubleToP32(other))
else:
return _softposit.p32_eq(self.v, other.v)
except TypeError:
......@@ -2373,8 +2442,9 @@ class posit32:
self.v = _softposit.p32_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p32_sqrt(self.v)
return self
a = posit32(0)
a.v = _softposit.p32_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP32ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -2394,8 +2464,9 @@ class posit32:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit32(0)
a.v.toNaR();
return a
def toBinary(self):
self.v.toBits()
def fromBits(self, value):
......@@ -2491,12 +2562,27 @@ class quire32:
class posit_2:
def __init__(self, value, x):
def __init__(self, value=None, x=None, bits=None):
self.x = x
if isinstance(value, (int)):
self.v = _softposit.i64_to_pX2(value, x)
else:
self.v = _softposit.convertDoubleToPX2(value, x)
try:
if x is None:
raise Exception("Needs to specific posit size, x")
if bits is not None:
if isinstance(bits, (int)):
self.v = posit_2_t()
self.v.v = bits<<(32-x)
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit_2_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_pX2(value, x)
else:
self.v = _softposit.convertDoubleToPX2(value, x)
except Exception as error:
print(repr(error))
def type(self):
return 'posit' + str(self.x) +'_2'
def __add__(self, other):
......@@ -2748,8 +2834,9 @@ class posit_2:
self.v = _softposit.pX2_roundToInt(self.v, self.x)
return self
def sqrt(self):
self.v = _softposit.pX2_sqrt(self.v, self.x)
return self
a = posit_2(0, self.x)
a.v = _softposit.pX2_sqrt(self.v, self.x)
return a
def __repr__(self):
a = float(_softposit.convertPX2ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -2769,8 +2856,9 @@ class posit_2:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit_2(0)
a.v.toNaR();
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -2866,8 +2954,6 @@ class quire_2:
self.v.toHex()
# This file is compatible with both classic and new-style classes.
......@@ -85,11 +85,24 @@ def convertToColor(i, ps, es):
return colored+"\033[0m"
class posit8:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p8(value)
else:
self.v = _softposit.convertDoubleToP8(value)
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit8_t()
self.v.v = bits & 0xFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit8_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p8(value)
else:
self.v = _softposit.convertDoubleToP8(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit8'
def __add__(self, other):
......@@ -312,8 +325,9 @@ class posit8:
self.v = _softposit.p8_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p8_sqrt(self.v)
return self
a = posit8(0)
a.v = _softposit.p8_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP8ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -333,8 +347,9 @@ class posit8:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit8(0)
a.v.toNaR();
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -429,11 +444,24 @@ class quire8:
self.v.toHex()
class posit16:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p16(value);
else:
self.v = _softposit.convertDoubleToP16(value);
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit16_t()
self.v.v = bits & 0xFFFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit16_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p16(value)
else:
self.v = _softposit.convertDoubleToP16(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit16'
def __add__(self, other):
......@@ -653,8 +681,9 @@ class posit16:
self.v = _softposit.p16_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p16_sqrt(self.v)
return self
a = posit16(0)
a.v = _softposit.p16_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP16ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -674,8 +703,9 @@ class posit16:
def isNaR(self):
return self.v.isNaR()
def toNaR(self):
self.v.toNaR()
return self
a = posit16(0)
a.v.toNaR()
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -771,11 +801,24 @@ class quire16:
self.v.toHex()
class posit32:
def __init__(self, value):
if isinstance(value, (int)):
self.v = _softposit.i64_to_p32(value);
else:
self.v = _softposit.convertDoubleToP32(value);
def __init__(self, value=None, bits=None):
try:
if bits is not None:
if isinstance(bits, (int)):
self.v = posit32_t()
self.v.v = bits&0xFFFFFFFF
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit32_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_p32(value)
else:
self.v = _softposit.convertDoubleToP32(value)
except Exception as error:
print(repr(error))
def type(self):
return 'posit32'
def __add__(self, other):
......@@ -999,8 +1042,9 @@ class posit32:
self.v = _softposit.p32_roundToInt(self.v)
return self
def sqrt(self):
self.v = _softposit.p32_sqrt(self.v)
return self
a = posit32(0)
a.v = _softposit.p32_sqrt(self.v)
return a
def __repr__(self):
a = float(_softposit.convertP32ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -1020,8 +1064,9 @@ class posit32:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit32(0)
a.v.toNaR();
return a
def toBinary(self):
self.v.toBits()
def fromBits(self, value):
......@@ -1117,12 +1162,27 @@ class quire32:
class posit_2:
def __init__(self, value, x):
def __init__(self, value=None, x=None, bits=None):
self.x = x
if isinstance(value, (int)):
self.v = _softposit.i64_to_pX2(value, x)
else:
self.v = _softposit.convertDoubleToPX2(value, x)
try:
if x is None:
raise Exception("Needs to specific posit size, x")
if bits is not None:
if isinstance(bits, (int)):
self.v = posit_2_t()
self.v.v = bits<<(32-x)
else:
raise Exception("Bits can only be set with integer values")
else:
if value is None:
self.v = posit_2_t()
self.v.v = 0
elif isinstance(value, (int)):
self.v = _softposit.i64_to_pX2(value, x)
else:
self.v = _softposit.convertDoubleToPX2(value, x)
except Exception as error:
print(repr(error))
def type(self):
return 'posit' + str(self.x) +'_2'
def __add__(self, other):
......@@ -1374,8 +1434,9 @@ class posit_2:
self.v = _softposit.pX2_roundToInt(self.v, self.x)
return self
def sqrt(self):
self.v = _softposit.pX2_sqrt(self.v, self.x)
return self
a = posit_2(0, self.x)
a.v = _softposit.pX2_sqrt(self.v, self.x)
return a
def __repr__(self):
a = float(_softposit.convertPX2ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
......@@ -1395,8 +1456,9 @@ class posit_2:
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
a = posit_2(0)
a.v.toNaR();
return a
def fromBits(self, value):
self.v.fromBits(value)
def toBinary(self):
......@@ -1491,12 +1553,9 @@ class quire_2:
def toHex(self):
self.v.toHex()
%}
%extend posit8_t {
posit8_t init() {
posit8_t a;
......@@ -1820,23 +1879,23 @@ class quire_2:
return q;
}
void toBits() {
printBinary(((uint64_t*)&$self->v[0]), 64);
printBinary(((uint64_t*)&$self->v[0]), 64);
printBinary(((uint64_t*)&$self->v[1]), 64);
printBinary(((uint64_t*)&$self->v[2]), 64);
printBinary(((uint64_t*)&$self->v[2]), 64);
printBinary(((uint64_t*)&$self->v[3]), 64);
printBinary(((uint64_t*)&$self->v[4]), 64);
printBinary(((uint64_t*)&$self->v[4]), 64);
printBinary(((uint64_t*)&$self->v[5]), 64);
printBinary(((uint64_t*)&$self->v[6]), 64);
printBinary(((uint64_t*)&$self->v[6]), 64);
printBinary(((uint64_t*)&$self->v[7]), 64);
}
void toHex() {
printHex64($self->v[0]);
printHex64($self->v[0]);
printHex64($self->v[1]);
printHex64($self->v[2]);
printHex64($self->v[2]);
printHex64($self->v[3]);
printHex64($self->v[4]);
printHex64($self->v[4]);
printHex64($self->v[5]);
printHex64($self->v[6]);
printHex64($self->v[6]);
printHex64($self->v[7]);
}
quire32_t clr(quire32_t q) {
......@@ -1886,13 +1945,13 @@ class quire_2:
$self->v = bits & 0xFFFFFFFF;
}
void toBits(int x) {
printBinaryPX(((uint32_t*)&$self->v), x);
printBinaryPX(((uint32_t*)&$self->v), x);
}
void toHex(int x) {
printHexPX($self->v, x);
printHexPX($self->v, x);
}
int toInt() {
return $self->v;
return $self->v;
}
posit_2_t __rshift__(int n, int x) {
posit_2_t a;
......@@ -2030,4 +2089,3 @@ class quire_2:
};
This diff is collapsed.
......@@ -420,8 +420,7 @@ posit_2_t convertQuadToPX2(__float128 f32, int x){
}
else
frac = convertQuadFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
//printf("fracLength: %d reg: %d exp: %d bitNPlusOne: %d bitsMore: %d\n",fracLength, reg, exp, bitNPlusOne, bitsMore);
//printBinary(&frac,32);
if (reg>(x-2) ){
uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
}
......@@ -430,17 +429,15 @@ posit_2_t convertQuadToPX2(__float128 f32, int x){
uint_fast32_t regime = 1;
if (regS) regime = ( (1<<reg)-1 ) <<1;
//printBinary(&exp,32);
if (reg<=28) exp<<= (28-reg);
//printBinary(&exp,32);
uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
//minpos
if (uZ.ui==0 && frac>0){
uZ.ui = 0x1 << (32-x);
}
//printBinary(&uZ.ui,32);
//printf("lastbit: %d bitsMore:%d bitNPlusOne: %d\n", ((uZ.ui>>(32-x)) & 0x1), bitsMore, bitNPlusOne);
if (bitNPlusOne){
uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
}
......@@ -512,10 +509,7 @@ uint_fast32_t convertFractionP32(double f32, uint_fast16_t fracLength, bool * bi
}
posit32_t convertDoubleToP32(double f32){
//posit32 b = {.f = a };
//return convertDecToP32(b);
//}
//posit32_t convertDecToP32(posit32 a){
union ui32_p32 uZ;
bool sign, regS;
uint_fast32_t reg, frac=0;
......@@ -788,7 +782,6 @@ posit_2_t convertDoubleToPX2(double f32, int x){
}
//rounding off fraction bits
else{
uint_fast32_t regime = 1;
if (regS) regime = ( (1<<reg)-1 ) <<1;
......@@ -826,7 +819,6 @@ posit_2_t convertDoubleToPX2(double f32, int x){
exp++;
}
int fracLength = x-4-reg;
if (fracLength<0){
//in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
......@@ -869,8 +861,6 @@ posit_2_t convertDoubleToPX2(double f32, int x){
if (bitNPlusOne){
uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
}
}
if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
......
This diff is collapsed.
/*============================================================================
This C source file is part of the SoftPosit Posit Arithmetic Package
......@@ -183,7 +184,7 @@ __float128 convertPX2ToQuad(posit_2_t a){
double convertP32ToDouble(posit32_t pA){
union ui32_p32 uA;
union ui32_p32 uA;
union ui64_double uZ;
uint_fast32_t uiA, tmp=0;
uint_fast64_t expA=0, uiZ, fracA=0;
......@@ -227,6 +228,68 @@ double convertP32ToDouble(posit32_t pA){
return uZ.d;
}
}
/*double convertP32ToDoubleOld(posit32_t pA){
union ui32_p32 uZ;
double d32;
uZ.p = pA;
if (uZ.ui==0){
return 0;
}
else if(uZ.ui==0x7FFFFFFF){ //maxpos
return 1.329227995784916e+36;
}
else if (uZ.ui==0x80000001){ //-maxpos
return -1.329227995784916e+36;
}
else if (uZ.ui == 0x80000000){
return INFINITY;
}
bool regS, sign;
uint_fast32_t reg, shift=2, frac, tmp;
int_fast32_t k=0;
int_fast8_t exp;
double fraction_max;
sign = signP32UI( uZ.ui );
if (sign)
uZ.ui = -uZ.ui & 0xFFFFFFFF;
regS = signregP32UI( uZ.ui );
tmp = (uZ.ui<<2)&0xFFFFFFFF;
if (regS){
while (tmp>>31){
k++;
shift++;
tmp= (tmp<<1) & 0xFFFFFFFF;
}
reg = k+1;
}
else{
k=-1;
while (!(tmp>>31)){
k--;
shift++;
tmp= (tmp<<1) & 0xFFFFFFFF;
}
tmp&=0x7FFFFFFF;
reg =-k;
}
exp = tmp>>29;
frac = (tmp & 0x1FFFFFFF) >> shift;
(reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
d32 = (double)( pow(16, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
if (sign)
d32 = -d32;
return d32;
}*/
double convertPX2ToDouble(posit_2_t a){
......
/*============================================================================
This C source file is part of the SoftPosit Posit Arithmetic Package
by S. H. Leong (Cerlane).
Copyright 2017, 2018 A*STAR. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions, and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.