/* Author: S.H. Leong (Cerlane) Copyright (c) 2018 Next Generation Arithmetic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef INCLUDE_SOFTPOSIT_CPP_H_ #define INCLUDE_SOFTPOSIT_CPP_H_ #include #include "softposit.h" #include "math.h" //#include "positMath.h" #ifdef __cplusplus struct posit8{ uint8_t value; posit8(double x=0) : value(castUI(convertDoubleToP8(x))) { } //Equal posit8& operator=(const double a) { value = castUI(convertDoubleToP8(a)); return *this; } posit8& operator=(const int a) { value = castUI(i32_to_p8(a)); return *this; } //Add posit8 operator+(const posit8 &a) const{ posit8 ans; ans.value = castUI(p8_add(castP8(value), castP8(a.value))); return ans; } //Add equal posit8& operator+=(const posit8 &a) { value = castUI(p8_add(castP8(value), castP8(a.value))); return *this; } //Subtract posit8 operator-(const posit8 &a) const{ posit8 ans; ans.value = castUI(p8_sub(castP8(value), castP8(a.value))); return ans; } //Subtract equal posit8& operator-=(const posit8 &a) { value = castUI(p8_sub(castP8(value), castP8(a.value))); return *this; } //Multiply posit8 operator*(const posit8 &a) const{ posit8 ans; ans.value = castUI(p8_mul(castP8(value), castP8(a.value))); return ans; } //Multiply equal posit8& operator*=(const posit8 &a) { value = castUI(p8_mul(castP8(value), castP8(a.value))); return *this; } //Divide posit8 operator/(const posit8 &a) const{ posit8 ans; ans.value = castUI(p8_div(castP8(value), castP8(a.value))); return ans; } //Divide equal posit8& operator/=(const posit8 &a) { value = castUI(p8_div(castP8(value), castP8(a.value))); return *this; } //less than bool operator<(const posit8 &a) const{ return p8_lt(castP8(value), castP8(a.value)); } //less than equal bool operator<=(const posit8 &a) const{ return p8_le(castP8(value), castP8(a.value)); } //equal bool operator==(const posit8 &a) const{ return p8_eq(castP8(value), castP8(a.value)); } //Not equalCPP bool operator!=(const posit8 &a) const{ return !p8_eq(castP8(value), castP8(a.value)); } //greater than bool operator>(const posit8 &a) const{ return p8_lt(castP8(a.value), castP8(value)); } //greater than equal bool operator>=(const posit8 &a) const{ return p8_le(castP8(a.value), castP8(value)); } //plus plus posit8& operator++() { value = castUI(p8_add(castP8(value), castP8(0x40))); return *this; } //minus minus posit8& operator--() { value = castUI(p8_sub(castP8(value), castP8(0x40))); return *this; } //Binary operators posit8 operator>>(const int &x) { posit8 ans; ans.value = value>>x; return ans; } posit8& operator>>=(const int &x) { value = value>>x; return *this; } posit8 operator<<(const int &x) { posit8 ans; ans.value = (value<(const posit16 &a) const{ return p16_lt(castP16(a.value), castP16(value)); } //greater than equal bool operator>=(const posit16 &a) const{ return p16_le(castP16(a.value), castP16(value)); } //plus plus posit16& operator++() { value = castUI(p16_add(castP16(value), castP16(0x4000))); return *this; } //minus minus posit16& operator--() { value = castUI(p16_sub(castP16(value), castP16(0x4000))); return *this; } //Binary operators posit16 operator>>(const int &x) { posit16 ans; ans.value = value>>x; return ans; } posit16& operator>>=(const int &x) { value = value>>x; return *this; } posit16 operator<<(const int &x) { posit16 ans; ans.value = (value<(const posit32 &a) const{ return p32_lt(castP32(a.value), castP32(value)); } //greater than equal bool operator>=(const posit32 &a) const{ return p32_le(castP32(a.value), castP32(value)); } //plus plus posit32& operator++() { value = castUI(p32_add(castP32(value), castP32(0x40000000))); return *this; } //minus minus posit32& operator--() { value = castUI(p32_sub(castP32(value), castP32(0x40000000))); return *this; } //Binary operators posit32 operator>>(const int &x) { posit32 ans; ans.value = value>>x; return ans; } posit32& operator>>=(const int &x) { value = value>>x; return *this; } posit32 operator<<(const int &x) { posit32 ans; ans.value = (value<(const posit_2 &a) const{ return pX2_lt(castPX2(a.value), castPX2(value)); } //greater than equal bool operator>=(const posit_2 &a) const{ return pX2_le(castPX2(a.value), castPX2(value)); } //plus plus posit_2& operator++() { value = castUI(pX2_add(castPX2(value), castPX2(0x40000000), x)); return *this; } //minus minus posit_2& operator--() { value = castUI(pX2_sub(castPX2(value), castPX2(0x40000000), x)); return *this; } //Binary operators posit_2 operator>>(const int &x) { posit_2 ans; ans.value = (value>>x) & ((int32_t)0x80000000>>(x-1)); ans.x = x; return ans; } posit_2& operator>>=(const int &x) { value = (value>>x) & ((int32_t)0x80000000>>(x-1)); return *this; } posit_2 operator<<(const int &x) { posit_2 ans; ans.value = (value<