Commit a424edc7 authored by Cerlane Leong's avatar Cerlane Leong 💬

python support P16 and P32

parent 0faf6a86
V0.3.2RC V0.3.2
1) Fixed lt, le and eq. int_fast16_t types cannot represent negative numbers 1) Fixed lt, le and eq. int_fast16_t types cannot represent negative numbers
correctly. Use int16_t type instead. Thanks Shinyee for finding the bug. correctly. Use int16_t type instead. Thanks Shinyee for finding the bug.
2) Minor fixes 2) Minor fixes
3) Added CPP wrapper with operator overloading 3) Added CPP wrapper with operator overloading
3) Updated to support python
V0.3.1: V0.3.1:
......
...@@ -40,12 +40,23 @@ ...@@ -40,12 +40,23 @@
#============================================================================= #=============================================================================
SOURCE_DIR ?= ../../source SOURCE_DIR ?= ../../source
PYTHON_DIR ?= ../../python
SPECIALIZE_TYPE ?= 8086-SSE SPECIALIZE_TYPE ?= 8086-SSE
SOFTPOSIT_OPTS ?= \ SOFTPOSIT_OPTS ?= \
-DINLINE_LEVEL=5 #\ -DINLINE_LEVEL=5 #\
-DSOFTPOSIT_QUAD -lquadmath -DSOFTPOSIT_QUAD -lquadmath
COMPILE_PYTHON = \
gcc -fPIC -c $(PYTHON_DIR)/softposit_python_wrap.c \
-I/usr/include/python \
-I$(SOURCE_DIR)/include -I.
COMPILE_PYTHON3 = \
gcc -fPIC -c $(PYTHON_DIR)/softposit_python_wrap.c \
-I/usr/include/python3 \
-I$(SOURCE_DIR)/include -I.
LINK_PYTHON = \
ld -shared *.o -o $(PYTHON_DIR)/_softposit.so
DELETE = rm -f DELETE = rm -f
C_INCLUDES = -I. -I$(SOURCE_DIR)/$(SPECIALIZE_TYPE) -I$(SOURCE_DIR)/include C_INCLUDES = -I. -I$(SOURCE_DIR)/$(SPECIALIZE_TYPE) -I$(SOURCE_DIR)/include
...@@ -65,6 +76,17 @@ all: softposit$(LIB) ...@@ -65,6 +76,17 @@ all: softposit$(LIB)
quad: SOFTPOSIT_OPTS+= -DSOFTPOSIT_QUAD -lquadmath quad: SOFTPOSIT_OPTS+= -DSOFTPOSIT_QUAD -lquadmath
quad: all quad: all
python2: SOFTPOSIT_OPTS+= -fPIC
python2: all
$(COMPILE_PYTHON)
$(LINK_PYTHON)
python3: SOFTPOSIT_OPTS+= -fPIC
python3: all
$(COMPILE_PYTHON3)
$(LINK_PYTHON)
OBJS_PRIMITIVES = OBJS_PRIMITIVES =
OBJS_SPECIALIZE = OBJS_SPECIALIZE =
...@@ -153,5 +175,5 @@ softposit$(LIB): $(OBJS_ALL) ...@@ -153,5 +175,5 @@ softposit$(LIB): $(OBJS_ALL)
.PHONY: clean .PHONY: clean
clean: clean:
$(DELETE) $(OBJS_ALL) softposit$(LIB) $(DELETE) $(OBJS_ALL) softposit_python_wrap.o softposit$(LIB)
...@@ -119,6 +119,9 @@ class posit8_t(_object): ...@@ -119,6 +119,9 @@ class posit8_t(_object):
def toInt(self): def toInt(self):
return _softposit.posit8_t_toInt(self) return _softposit.posit8_t_toInt(self)
def toI64(self):
return _softposit.posit8_t_toI64(self)
def i32ToP8(self, a): def i32ToP8(self, a):
return _softposit.posit8_t_i32ToP8(self, a) return _softposit.posit8_t_i32ToP8(self, a)
...@@ -149,10 +152,24 @@ class posit8_t(_object): ...@@ -149,10 +152,24 @@ class posit8_t(_object):
def __or__(self, other): def __or__(self, other):
return _softposit.posit8_t___or__(self, other) return _softposit.posit8_t___or__(self, other)
def isNaR(self):
return _softposit.posit8_t_isNaR(self)
def toNaR(self):
return _softposit.posit8_t_toNaR(self)
def __repr__(self): def __repr__(self):
return str(_softposit.convertP8ToDouble(self)) a = float(_softposit.convertP8ToDouble(self))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self): def __str__(self):
return str(_softposit.convertP8ToDouble(self)) a = float(_softposit.convertP8ToDouble(self))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __init__(self): def __init__(self):
...@@ -186,6 +203,9 @@ class posit16_t(_object): ...@@ -186,6 +203,9 @@ class posit16_t(_object):
def toHex(self): def toHex(self):
return _softposit.posit16_t_toHex(self) return _softposit.posit16_t_toHex(self)
def toI64(self):
return _softposit.posit16_t_toI64(self)
def toInt(self): def toInt(self):
return _softposit.posit16_t_toInt(self) return _softposit.posit16_t_toInt(self)
...@@ -219,10 +239,24 @@ class posit16_t(_object): ...@@ -219,10 +239,24 @@ class posit16_t(_object):
def __or__(self, other): def __or__(self, other):
return _softposit.posit16_t___or__(self, other) return _softposit.posit16_t___or__(self, other)
def isNaR(self):
return _softposit.posit16_t_isNaR(self)
def toNaR(self):
return _softposit.posit16_t_toNaR(self)
def __repr__(self): def __repr__(self):
return str(_softposit.convertP16ToDouble(self)) a = float(_softposit.convertP16ToDouble(self))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self): def __str__(self):
return str(_softposit.convertP16ToDouble(self)) a = float(_softposit.convertP16ToDouble(self))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __init__(self): def __init__(self):
...@@ -319,6 +353,32 @@ class quire8_t(_object): ...@@ -319,6 +353,32 @@ class quire8_t(_object):
self.this.append(this) self.this.append(this)
except __builtin__.Exception: except __builtin__.Exception:
self.this = this self.this = this
def toBits(self):
return _softposit.quire8_t_toBits(self)
def toHex(self):
return _softposit.quire8_t_toHex(self)
def clr(self, q):
return _softposit.quire8_t_clr(self, q)
def isNaR(self):
return _softposit.quire8_t_isNaR(self)
def __repr__(self):
a = float(_softposit.convertP8ToDouble(_softposit.q8_to_p8(self)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self):
a = float(_softposit.convertP8ToDouble(_softposit.q8_to_p8(self)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
__swig_destroy__ = _softposit.delete_quire8_t __swig_destroy__ = _softposit.delete_quire8_t
__del__ = lambda self: None __del__ = lambda self: None
quire8_t_swigregister = _softposit.quire8_t_swigregister quire8_t_swigregister = _softposit.quire8_t_swigregister
...@@ -341,6 +401,32 @@ class quire16_t(_object): ...@@ -341,6 +401,32 @@ class quire16_t(_object):
self.this.append(this) self.this.append(this)
except __builtin__.Exception: except __builtin__.Exception:
self.this = this self.this = this
def toBits(self):
return _softposit.quire16_t_toBits(self)
def toHex(self):
return _softposit.quire16_t_toHex(self)
def clr(self, q):
return _softposit.quire16_t_clr(self, q)
def isNaR(self):
return _softposit.quire16_t_isNaR(self)
def __repr__(self):
a = float(_softposit.convertP16ToDouble(_softposit.q16_to_p16(self)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self):
a = float(_softposit.convertP16ToDouble(_softposit.q16_to_p16(self)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
__swig_destroy__ = _softposit.delete_quire16_t __swig_destroy__ = _softposit.delete_quire16_t
__del__ = lambda self: None __del__ = lambda self: None
quire16_t_swigregister = _softposit.quire16_t_swigregister quire16_t_swigregister = _softposit.quire16_t_swigregister
...@@ -576,7 +662,6 @@ class ui512_q32(_object): ...@@ -576,7 +662,6 @@ class ui512_q32(_object):
ui512_q32_swigregister = _softposit.ui512_q32_swigregister ui512_q32_swigregister = _softposit.ui512_q32_swigregister
ui512_q32_swigregister(ui512_q32) ui512_q32_swigregister(ui512_q32)
softposit_h = _softposit.softposit_h
def ui32_to_p8(arg1): def ui32_to_p8(arg1):
return _softposit.ui32_to_p8(arg1) return _softposit.ui32_to_p8(arg1)
...@@ -686,6 +771,10 @@ def q8_to_p8(arg1): ...@@ -686,6 +771,10 @@ def q8_to_p8(arg1):
return _softposit.q8_to_p8(arg1) return _softposit.q8_to_p8(arg1)
q8_to_p8 = _softposit.q8_to_p8 q8_to_p8 = _softposit.q8_to_p8
def q8Clr():
return _softposit.q8Clr()
q8Clr = _softposit.q8Clr
def convertP8ToDouble(arg1): def convertP8ToDouble(arg1):
return _softposit.convertP8ToDouble(arg1) return _softposit.convertP8ToDouble(arg1)
convertP8ToDouble = _softposit.convertP8ToDouble convertP8ToDouble = _softposit.convertP8ToDouble
...@@ -786,6 +875,10 @@ def printHex(arg1): ...@@ -786,6 +875,10 @@ def printHex(arg1):
return _softposit.printHex(arg1) return _softposit.printHex(arg1)
printHex = _softposit.printHex printHex = _softposit.printHex
def q16Clr():
return _softposit.q16Clr()
q16Clr = _softposit.q16Clr
def convertP16ToDouble(arg1): def convertP16ToDouble(arg1):
return _softposit.convertP16ToDouble(arg1) return _softposit.convertP16ToDouble(arg1)
convertP16ToDouble = _softposit.convertP16ToDouble convertP16ToDouble = _softposit.convertP16ToDouble
...@@ -817,6 +910,8 @@ class posit8: ...@@ -817,6 +910,8 @@ class posit8:
self.v = self.v.i64ToP8(a); self.v = self.v.i64ToP8(a);
else: else:
self.v = _softposit.convertDoubleToP8(value); self.v = _softposit.convertDoubleToP8(value);
def type(self):
return 'posit8'
def __add__(self, other): def __add__(self, other):
a = posit8(0) a = posit8(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -837,6 +932,8 @@ class posit8: ...@@ -837,6 +932,8 @@ class posit8:
else: else:
a.v = _softposit.p8_sub(self.v, other.v) a.v = _softposit.p8_sub(self.v, other.v)
return a return a
def __rsub__(self, other):
return self.__sub__(other)
def __mul__(self, other): def __mul__(self, other):
a = posit8(0) a = posit8(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -846,6 +943,8 @@ class posit8: ...@@ -846,6 +943,8 @@ class posit8:
else: else:
a.v = _softposit.p8_mul(self.v, other.v) a.v = _softposit.p8_mul(self.v, other.v)
return a return a
def __rmul__(self, other):
return self.__mul__(other)
def __div__(self, other): def __div__(self, other):
a = posit8(0) a = posit8(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -855,6 +954,8 @@ class posit8: ...@@ -855,6 +954,8 @@ class posit8:
else: else:
a.v = _softposit.p8_div(self.v, other.v) a.v = _softposit.p8_div(self.v, other.v)
return a return a
def __rdiv__(self, other):
return self.__div__(other)
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, (int)): if isinstance(other, (int)):
return _softposit.p8_eq(self.v, self.v.i64ToP8(other)) return _softposit.p8_eq(self.v, self.v.i64ToP8(other))
...@@ -930,19 +1031,142 @@ class posit8: ...@@ -930,19 +1031,142 @@ class posit8:
a = posit8(0) a = posit8(0)
a.v = self.v.__or__(other.v) a.v = self.v.__or__(other.v)
return a return a
def fma(self, other1, other2):
a = posit8(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
a.v = _softposit.p8_mulAdd(self.v, a.v.i64ToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p8_mulAdd(self.v, a.v.i64ToP8(other1), _softposit.convertDoubleToP8(other2))
else:
a.v = _softposit.p8_mulAdd(self.v, a.v.i64ToP8(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
a.v = _softposit.p8_mulAdd(self.v, _softposit.convertDoubleToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p8_mulAdd(self.v, _softposit.convertDoubleToP8(other1), _softposit.convertDoubleToP8(other2))
else:
a.v = _softposit.p8_mulAdd(self.v, _softposit.convertDoubleToP8(other1), other2.v)
else:
if isinstance(other2, (int)):
a.v = _softposit.p8_mulAdd(self.v, other1.v, a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p8_mulAdd(self.v, other1.v, _softposit.convertDoubleToP8(other2))
else:
a.v = _softposit.p8_mulAdd(self.v, other1.v, other2.v)
return a
def toPosit16(self):
a = posit16(0)
a.v = _softposit.p8_to_p16(self.v)
return a
def toInt(self):
return self.v.toI64()
def rint(self):
return _softposit.p8_roundToInt(self.v)
def sqrt(self):
return _softposit.p8_sqrt(self.v)
def __repr__(self): def __repr__(self):
return str(_softposit.convertP8ToDouble(self.v)) a = float(_softposit.convertP8ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self): def __str__(self):
return str(_softposit.convertP8ToDouble(self.v)) a = float(_softposit.convertP8ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __int__(self): def __int__(self):
return int(_softposit.convertP8ToDouble(self.v)) return int(_softposit.convertP8ToDouble(self.v))
def __float__(self): def __float__(self):
return float(_softposit.convertP8ToDouble(self.v)) return float(_softposit.convertP8ToDouble(self.v))
def isNaR(self):
return self.v.isNaR();
def toNaR(self):
self.v.toNaR();
return self
def toBinary(self): def toBinary(self):
self.v.toBits() self.v.toBits()
def toHex(self): def toHex(self):
self.v.toHex() self.v.toHex()
class quire8:
def __init__(self):
self.v = _softposit.q8Clr();
def type(self):
return 'quire8'
def qma(self, other1, other2):
a = posit8(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_add(self.v, a.v.i64ToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_add(self.v, a.v.i64ToP8(other1), _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_add(self.v, a.v.i64ToP8(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_add(self.v, _softposit.convertDoubleToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_add(self.v, _softposit.convertDoubleToP8(other1), _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_add(self.v, _softposit.convertDoubleToP8(other1), other2.v)
else:
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_add(self.v, other1.v, a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_add(self.v, other1.v, _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_add(self.v, other1.v, other2.v)
return self
def qms(self, other1, other2):
a = posit8(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_sub(self.v, a.v.i64ToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_sub(self.v, a.v.i64ToP8(other1), _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_sub(self.v, a.v.i64ToP8(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_sub(self.v, _softposit.convertDoubleToP8(other1), a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_sub(self.v, _softposit.convertDoubleToP8(other1), _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_sub(self.v, _softposit.convertDoubleToP8(other1), other2.v)
else:
if isinstance(other2, (int)):
self.v = _softposit.q8_fdp_sub(self.v, other1.v, a.v.i64ToP8(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q8_fdp_sub(self.v, other1.v, _softposit.convertDoubleToP8(other2))
else:
self.v = _softposit.q8_fdp_sub(self.v, other1.v, other2.v)
return self
def toPosit(self):
self.v = _softposit.q8_to_p8(self.v);
return self
def clr(self):
self.v = _softposit.q8Clr();
def isNaR(self):
return self.v.isNaR();
def __repr__(self):
a = float(_softposit.convertP8ToDouble(_softposit.q8_to_p8(self.v)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self):
a = float(_softposit.convertP8ToDouble(_softposit.q8_to_p8(self.v)))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def toBinary(self):
self.v.toBits()
def toHex(self):
self.v.toHex()
class posit16: class posit16:
def __init__(self, value): def __init__(self, value):
...@@ -950,6 +1174,8 @@ class posit16: ...@@ -950,6 +1174,8 @@ class posit16:
self.v = self.v.i64ToP16(a); self.v = self.v.i64ToP16(a);
else: else:
self.v = _softposit.convertDoubleToP16(value); self.v = _softposit.convertDoubleToP16(value);
def type(self):
return 'posit16'
def __add__(self, other): def __add__(self, other):
a = posit16(0) a = posit16(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -959,6 +1185,8 @@ class posit16: ...@@ -959,6 +1185,8 @@ class posit16:
else: else:
a.v = _softposit.p16_add(self.v, other.v) a.v = _softposit.p16_add(self.v, other.v)
return a return a
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other): def __sub__(self, other):
a = posit16(0) a = posit16(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -968,6 +1196,8 @@ class posit16: ...@@ -968,6 +1196,8 @@ class posit16:
else: else:
a.v = _softposit.p16_sub(self.v, other.v) a.v = _softposit.p16_sub(self.v, other.v)
return a return a
def __rsub__(self, other):
return self.__sub__(other)
def __mul__(self, other): def __mul__(self, other):
a = posit16(0) a = posit16(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -977,6 +1207,8 @@ class posit16: ...@@ -977,6 +1207,8 @@ class posit16:
else: else:
a.v = _softposit.p16_mul(self.v, other.v) a.v = _softposit.p16_mul(self.v, other.v)
return a return a
def __rmul__(self, other):
return self.__mul__(other)
def __div__(self, other): def __div__(self, other):
a = posit16(0) a = posit16(0)
if isinstance(other, (int)): if isinstance(other, (int)):
...@@ -986,6 +1218,8 @@ class posit16: ...@@ -986,6 +1218,8 @@ class posit16:
else: else:
a.v = _softposit.p16_div(self.v, other.v) a.v = _softposit.p16_div(self.v, other.v)
return a return a
def __rdiv__(self, other):
return self.__div__(other)
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, (int)): if isinstance(other, (int)):
return _softposit.p16_eq(self.v, self.v.i64ToP16(other)) return _softposit.p16_eq(self.v, self.v.i64ToP16(other))
...@@ -1061,19 +1295,145 @@ class posit16: ...@@ -1061,19 +1295,145 @@ class posit16:
a = posit16(0) a = posit16(0)
a.v = self.v.__or__(other.v) a.v = self.v.__or__(other.v)
return a return a
def fma(self, other1, other2):
a = posit16(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
a.v = _softposit.p16_mulAdd(self.v, a.v.i64ToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p16_mulAdd(self.v, a.v.i64ToP16(other1), _softposit.convertDoubleToP16(other2))
else:
a.v = _softposit.p16_mulAdd(self.v, a.v.i64ToP16(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
a.v = _softposit.p16_mulAdd(self.v, _softposit.convertDoubleToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p16_mulAdd(self.v, _softposit.convertDoubleToP16(other1), _softposit.convertDoubleToP16(other2))
else:
a.v = _softposit.p16_mulAdd(self.v, _softposit.convertDoubleToP16(other1), other2.v)
else:
if isinstance(other2, (int)):
a.v = _softposit.p16_mulAdd(self.v, other1.v, a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
a.v = _softposit.p16_mulAdd(self.v, other1.v, _softposit.convertDoubleToP16(other2))
else:
a.v = _softposit.p16_mulAdd(self.v, other1.v, other2.v)
return a
def toPosit8(self):
a = posit8(0)
a.v = _softposit.p16_to_p8(self.v)
return a
def toInt(self):
return self.v.toI64()
def rint(self):
return _softposit.p16_roundToInt(self.v)
def sqrt(self):
return _softposit.p16_sqrt(self.v)
def __repr__(self): def __repr__(self):
return str(_softposit.convertP16ToDouble(self.v)) a = float(_softposit.convertP16ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __str__(self): def __str__(self):
return str(_softposit.convertP16ToDouble(self.v)) a = float(_softposit.convertP16ToDouble(self.v))
if (a == float('inf')) or (a==float('-inf')) or (a==float('nan')):
return "NaR"
else:
return str(a)
def __int__(self): def __int__(self):
return int(_softposit.convertP16ToDouble(self.v)) return int(_softposit.convertP16ToDouble(self.v))
def __float__(self): def __float__(self):
return float(_softposit.convertP16ToDouble(self.v)) return float(_softposit.convertP16ToDouble(self.v))
def isNaR(self):
return self.v.isNaR()
def toNaR(self):
self.v.toNaR()
return self
def toBinary(self): def toBinary(self):
self.v.toBits() self.v.toBits()
def toHex(self): def toHex(self):
self.v.toHex() self.v.toHex()
class quire16:
def __init__(self):
self.v = _softposit.q16Clr();
def type(self):
return 'quire16'
def qma(self, other1, other2):
a = posit16(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_add(self.v, a.v.i64ToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_add(self.v, a.v.i64ToP16(other1), _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_add(self.v, a.v.i64ToP16(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_add(self.v, _softposit.convertDoubleToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_add(self.v, _softposit.convertDoubleToP16(other1), _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_add(self.v, _softposit.convertDoubleToP16(other1), other2.v)
else:
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_add(self.v, other1.v, a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_add(self.v, other1.v, _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_add(self.v, other1.v, other2.v)
return self
def qms(self, other1, other2):
a = posit16(0)
if isinstance(other1, (int)):
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_sub(self.v, a.v.i64ToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_sub(self.v, a.v.i64ToP16(other1), _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_sub(self.v, a.v.i64ToP16(other1), other2.v)
elif isinstance(other1, (float)):
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_sub(self.v, _softposit.convertDoubleToP16(other1), a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_sub(self.v, _softposit.convertDoubleToP16(other1), _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_sub(self.v, _softposit.convertDoubleToP16(other1), other2.v)
else:
if isinstance(other2, (int)):
self.v = _softposit.q16_fdp_sub(self.v, other1.v, a.v.i64ToP16(other2))
elif isinstance(other2, (float)):
self.v = _softposit.q16_fdp_sub(self.v, other1.v, _softposit.convertDoubleToP16(other2))
else:
self.v = _softposit.q16_fdp_sub(self.v, other1.v, other2.v)
return self
def toPosit(self):
self.v = _softposit.q16_to_p16(self.v);