diff options
Diffstat (limited to 'source/luametatex/source/libraries/softposit/source/include')
5 files changed, 2950 insertions, 0 deletions
diff --git a/source/luametatex/source/libraries/softposit/source/include/internals.h b/source/luametatex/source/libraries/softposit/source/include/internals.h new file mode 100644 index 000000000..c2eb1f5bf --- /dev/null +++ b/source/luametatex/source/libraries/softposit/source/include/internals.h @@ -0,0 +1,178 @@ + +/*============================================================================ + +This C header file is part of the SoftPosit Posit Arithmetic Package +by S. H. Leong (Cerlane). + +Copyright 2017, 2018 A*STAR. All rights reserved. + +This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3d, by John R. Hauser. + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the +University of California. 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. + +=============================================================================*/ + +#ifndef internals_h +#define internals_h 1 + +#ifdef __cplusplus +extern "C"{ +#endif + +#include "primitives.h" +#include "softposit.h" +#include "softposit_types.h" + +#include <stdio.h> + +#ifdef SOFTPOSIT_QUAD +#include <quadmath.h> +#endif + + + +enum { + softposit_mulAdd_subC = 1, + softposit_mulAdd_subProd = 2 +}; + + +/*---------------------------------------------------------------------------- +*----------------------------------------------------------------------------*/ +#define signP8UI( a ) ((bool) ((uint8_t) (a)>>7)) +#define signregP8UI( a ) ((bool) (((uint8_t) (a)>>6) & 0x1)) +#define packToP8UI( regime, fracA) ((uint8_t) regime + ((uint8_t)(fracA)) ) + + +posit8_t softposit_addMagsP8( uint_fast8_t, uint_fast8_t ); +posit8_t softposit_subMagsP8( uint_fast8_t, uint_fast8_t ); +posit8_t softposit_mulAddP8( uint_fast8_t, uint_fast8_t, uint_fast8_t, uint_fast8_t ); + + +/*---------------------------------------------------------------------------- +*----------------------------------------------------------------------------*/ +#define signP16UI( a ) ( (bool) ( ( uint16_t ) (a)>>15 ) ) +#define signregP16UI( a ) ( (bool) (((uint16_t) (a)>>14) & 0x1) ) +#define expP16UI( a, regA ) ((int_fast8_t) ((a)>>(13-regA) & 0x0001)) +#define packToP16UI( regime, regA, expA, fracA) ((uint16_t) regime + ((uint16_t) (expA)<< (13-regA)) + ((uint16_t)(fracA)) ) + +posit16_t softposit_addMagsP16( uint_fast16_t, uint_fast16_t ); +posit16_t softposit_subMagsP16( uint_fast16_t, uint_fast16_t ); +posit16_t softposit_mulAddP16( uint_fast16_t, uint_fast16_t, uint_fast16_t, uint_fast16_t ); + + +/*---------------------------------------------------------------------------- +*----------------------------------------------------------------------------*/ +#define signP32UI( a ) ((bool) ((uint32_t) (a)>>31)) +#define signregP32UI( a ) ((bool) (((uint32_t) (a)>>30) & 0x1)) +#define packToP32UI(regime, expA, fracA) ( (uint32_t) regime + (uint32_t) expA + ((uint32_t)(fracA)) ) + +posit32_t softposit_addMagsP32( uint_fast32_t, uint_fast32_t ); +posit32_t softposit_subMagsP32( uint_fast32_t, uint_fast32_t ); +posit32_t softposit_mulAddP32( uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast32_t ); + + +/*---------------------------------------------------------------------------- +*----------------------------------------------------------------------------*/ + +posit_2_t softposit_addMagsPX2( uint_fast32_t, uint_fast32_t, int ); +posit_2_t softposit_subMagsPX2( uint_fast32_t, uint_fast32_t, int ); +posit_2_t softposit_mulAddPX2( uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast32_t, int ); + +/*---------------------------------------------------------------------------- +*----------------------------------------------------------------------------*/ + +posit_1_t softposit_addMagsPX1( uint_fast32_t, uint_fast32_t, int); +posit_1_t softposit_subMagsPX1( uint_fast32_t, uint_fast32_t, int); +posit_1_t softposit_mulAddPX1( uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast32_t, int ); + +/*uint_fast16_t reglengthP32UI (uint32_t); +int_fast16_t regkP32UI(bool, uint_fast32_t); +#define expP32UI( a, regA ) ((int_fast16_t) ((a>>(28-regA)) & 0x2)) +#define regP32UI( a, regLen ) ( ((( uint_fast32_t ) (a) & (0x7FFFFFFF)) >> (30-regLen))) ) +#define isNaRP32UI( a ) ( ((a) ^ 0x80000000) == 0 ) +#define useed32P 16; +//int_fast16_t expP32UI(uint32_t); +#define expP32sizeUI 2; +uint_fast32_t fracP32UI(uint_fast32_t, uint_fast16_t);*/ + + + +/*posit32_t convertDecToP32(posit32); +posit32_t convertfloatToP32(float); +posit32_t convertdoubleToP32(double ); +//posit32_t convertQuadToP32(__float128); +//__float128 convertP32ToQuadDec(posit32_t); + + +//posit32_t c_roundPackToP32( bool, bool, int_fast16_t, int_fast16_t, uint_fast16_t, bool, bool ); + +//#define isNaNP32UI( a ) (((~(a) & 0x7F800000) == 0) && ((a) & 0x007FFFFF)) + + +//posit32_t softposit_roundPackToP32( bool, int_fast16_t, uint_fast32_t ); +//posit32_t softposit_normRoundPackToP32( bool, int_fast16_t, uint_fast32_t ); + +posit32_t softposit_addMagsP32( uint_fast32_t, uint_fast32_t ); +posit32_t softposit_subMagsP32( uint_fast32_t, uint_fast32_t ); +posit32_t softposit_mulAddP32(uint_fast32_t, uint_fast32_t, uint_fast32_t, uint_fast16_t); + + +//quire32_t quire32_add(quire32_t, quire32_t); +//quire32_t quire32_sub(quire32_t, quire32_t); +quire32_t quire32_mul(posit32_t, posit32_t); +quire32_t q32_fdp_add(quire32_t, posit32_t, posit32_t); +quire32_t q32_fdp_sub(quire32_t, posit32_t, posit32_t); +posit32_t convertQ32ToP32(quire32_t); +#define isNaRQ32( q ) ( q.v[0]==0x8000000000000000ULL && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +#define isQ32Zero(q) (q.v[0]==0 && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +quire32_t q32_TwosComplement(quire32_t); +#define q32_clr(q) ({\ + q.v[0]=0;\ + q.v[1]=0;\ + q.v[2]=0;\ + q.v[3]=0;\ + q.v[4]=0;\ + q.v[5]=0;\ + q.v[6]=0;\ + q.v[7]=0;\ + q;\ +}) +*/ + +#ifdef __cplusplus +} +#endif + + +#endif + + + diff --git a/source/luametatex/source/libraries/softposit/source/include/primitives.h b/source/luametatex/source/libraries/softposit/source/include/primitives.h new file mode 100644 index 000000000..76f714210 --- /dev/null +++ b/source/luametatex/source/libraries/softposit/source/include/primitives.h @@ -0,0 +1,52 @@ + +/*============================================================================ + +This C header file is part of the SoftPosit Posit Arithmetic Package +by S. H. Leong (Cerlane). + +Copyright 2017, 2018 A*STAR. All rights reserved. + +This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3d, by John R. Hauser. + + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the +University of California. 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. + +=============================================================================*/ + +#ifndef primitives_h +#define primitives_h 1 + +#include <stdbool.h> +#include <stdint.h> + +extern const uint_fast16_t softposit_approxRecipSqrt0[16]; +extern const uint_fast16_t softposit_approxRecipSqrt1[16]; + +#endif diff --git a/source/luametatex/source/libraries/softposit/source/include/softposit.h b/source/luametatex/source/libraries/softposit/source/include/softposit.h new file mode 100644 index 000000000..70dae1dbc --- /dev/null +++ b/source/luametatex/source/libraries/softposit/source/include/softposit.h @@ -0,0 +1,653 @@ +/*============================================================================ + +This C header file is part of the SoftPosit Posit Arithmetic Package +by S. H. Leong (Cerlane). + +Copyright 2017, 2018 A*STAR. All rights reserved. + +This C header file was based on SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3d, by John R. Hauser. + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the +University of California. 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. + +=============================================================================*/ + + +/*============================================================================ +| Note: If SoftPosit is modified from SoftFloat and is made available as a +| general library for programs to use, it is strongly recommended that a +| platform-specific version of this header, "softposit.h", be created that +| folds in "softposit_types.h" and that eliminates all dependencies on +| compile-time macros. +*============================================================================*/ + + +#ifndef softposit_h +#define softposit_h 1 + +#ifdef __cplusplus +extern "C"{ +#endif + +#include <stdbool.h> +#include <stdint.h> + +#ifdef SOFTPOSIT_QUAD +#include <quadmath.h> +#endif + +#include "softposit_types.h" + +#include <stdio.h> + + +#ifndef THREAD_LOCAL +#define THREAD_LOCAL +#endif + +#define castUI( a ) ( (a).v ) + +/*---------------------------------------------------------------------------- +| Integer-to-posit conversion routines. +*----------------------------------------------------------------------------*/ +posit8_t ui32_to_p8( uint32_t ); +posit16_t ui32_to_p16( uint32_t ); +posit32_t ui32_to_p32( uint32_t ); +//posit64_t ui32_to_p64( uint32_t ); + + +posit8_t ui64_to_p8( uint64_t ); +posit16_t ui64_to_p16( uint64_t ); +posit32_t ui64_to_p32( uint64_t ); +//posit64_t ui64_to_p64( uint64_t ); + +posit8_t i32_to_p8( int32_t ); +posit16_t i32_to_p16( int32_t ); +posit32_t i32_to_p32( int32_t ); +//posit64_t i32_to_p64( int32_t ); + +posit8_t i64_to_p8( int64_t ); +posit16_t i64_to_p16( int64_t ); +posit32_t i64_to_p32( int64_t ); +//posit64_t i64_to_p64( int64_t ); + + + +/*---------------------------------------------------------------------------- +| 8-bit (quad-precision) posit operations. +*----------------------------------------------------------------------------*/ +#define isNaRP8UI( a ) ( ((a) ^ 0x80) == 0 ) + +uint_fast32_t p8_to_ui32( posit8_t ); +uint_fast64_t p8_to_ui64( posit8_t ); +int_fast32_t p8_to_i32( posit8_t); +int_fast64_t p8_to_i64( posit8_t); + +posit16_t p8_to_p16( posit8_t ); +posit32_t p8_to_p32( posit8_t ); +//posit64_t p8_to_p64( posit8_t ); + +posit_1_t p8_to_pX1( posit8_t, int ); +posit_2_t p8_to_pX2( posit8_t, int ); + +posit8_t p8_roundToInt( posit8_t ); +posit8_t p8_add( posit8_t, posit8_t ); +posit8_t p8_sub( posit8_t, posit8_t ); +posit8_t p8_mul( posit8_t, posit8_t ); +posit8_t p8_mulAdd( posit8_t, posit8_t, posit8_t ); +posit8_t p8_div( posit8_t, posit8_t ); +posit8_t p8_sqrt( posit8_t ); +bool p8_eq( posit8_t, posit8_t ); +bool p8_le( posit8_t, posit8_t ); +bool p8_lt( posit8_t, posit8_t ); + + +//Quire 8 +quire8_t q8_fdp_add(quire8_t, posit8_t, posit8_t); +quire8_t q8_fdp_sub(quire8_t, posit8_t, posit8_t); +posit8_t q8_to_p8(quire8_t); +#define isNaRQ8( q ) ( (q).v==0x80000000 ) +#define isQ8Zero(q) ( (q).v==0 ) + +int_fast64_t p8_int( posit8_t ); + +#define q8_clr(q) ({\ + (q).v=0;\ + q;\ +}) + +static inline quire8_t q8Clr(){ + quire8_t q; + q.v=0; + return q; +} + +#define castQ8(a)({\ + union ui32_q8 uA;\ + uA.ui = (a);\ + uA.q;\ +}) + + +#define castP8(a)({\ + union ui8_p8 uA;\ + uA.ui = (a);\ + uA.p;\ +}) + + +#define negP8(a)({\ + union ui8_p8 uA;\ + uA.p = (a);\ + uA.ui = -uA.ui&0xFF;\ + uA.p; \ +}) + +#define absP8(a)({\ + union ui8_p8 uA;\ + uA.p = (a);\ + int mask = uA.ui >> 7;\ + uA.ui = ((uA.ui + mask) ^ mask)&0xFF;\ + uA.p; \ +}) + +//Helper +double convertP8ToDouble(posit8_t); +posit8_t convertDoubleToP8(double); + +/*---------------------------------------------------------------------------- +| 16-bit (half-precision) posit operations. +*----------------------------------------------------------------------------*/ +#define isNaRP16UI( a ) ( ((a) ^ 0x8000) == 0 ) + +uint_fast32_t p16_to_ui32( posit16_t ); +uint_fast64_t p16_to_ui64( posit16_t ); +int_fast32_t p16_to_i32( posit16_t); +int_fast64_t p16_to_i64( posit16_t ); +posit8_t p16_to_p8( posit16_t ); +posit32_t p16_to_p32( posit16_t ); +//posit64_t p16_to_p64( posit16_t ); + +posit_1_t p16_to_pX1( posit16_t, int ); +posit_2_t p16_to_pX2( posit16_t, int ); + +posit16_t p16_roundToInt( posit16_t); +posit16_t p16_add( posit16_t, posit16_t ); +posit16_t p16_sub( posit16_t, posit16_t ); +posit16_t p16_mul( posit16_t, posit16_t ); +posit16_t p16_mulAdd( posit16_t, posit16_t, posit16_t ); +posit16_t p16_div( posit16_t, posit16_t ); +posit16_t p16_sqrt( posit16_t ); +bool p16_eq( posit16_t, posit16_t ); +bool p16_le( posit16_t, posit16_t ); +bool p16_lt( posit16_t, posit16_t ); + + +#ifdef SOFTPOSIT_QUAD + __float128 convertP16ToQuadDec(posit16_t); + posit16_t convertQuadToP16(__float128); +#endif + +//Quire 16 +quire16_t q16_fdp_add(quire16_t, posit16_t, posit16_t); +quire16_t q16_fdp_sub(quire16_t, posit16_t, posit16_t); +posit16_t convertQ16ToP16(quire16_t); +posit16_t q16_to_p16(quire16_t); +#define isNaRQ16( q ) ( (q).v[0]==0x8000000000000000ULL && (q).v[1]==0 ) +#define isQ16Zero(q) (q.v[0]==0 && q.v[1]==0) +quire16_t q16_TwosComplement(quire16_t); + + +int_fast64_t p16_int( posit16_t); + +void printBinary(uint64_t*, int); +void printBinaryPX(uint32_t*, int); +void printHex(uint64_t); +void printHex64(uint64_t); +void printHexPX(uint32_t, int); + +#define q16_clr(q) ({\ + (q).v[0]=0;\ + (q).v[1]=0;\ + q;\ +}) + +static inline quire16_t q16Clr(){ + quire16_t q; + q.v[0]=0; + q.v[1]=0; + return q; +} + +#define castQ16(l, r)({\ + union ui128_q16 uA;\ + uA.ui[0] = l; \ + uA.ui[1] = r; \ + uA.q;\ +}) + + +#define castP16(a)({\ + union ui16_p16 uA;\ + uA.ui = (a);\ + uA.p;\ +}) + + + +#define negP16(a)({\ + union ui16_p16 uA;\ + uA.p = (a);\ + uA.ui = -uA.ui&0xFFFF;\ + uA.p; \ +}) + +#define absP16(a)({\ + union ui16_p16 uA;\ + uA.p = (a);\ + int mask = uA.ui >> 15;\ + uA.ui = ((uA.ui + mask) ^ mask)&0xFFFF;\ + uA.p; \ +}) + +//Helper + +double convertP16ToDouble(posit16_t); +posit16_t convertFloatToP16(float); +posit16_t convertDoubleToP16(double); + +/*---------------------------------------------------------------------------- +| 32-bit (single-precision) posit operations. +*----------------------------------------------------------------------------*/ +uint_fast32_t p32_to_ui32( posit32_t ); +uint_fast64_t p32_to_ui64( posit32_t); +int_fast32_t p32_to_i32( posit32_t ); +int_fast64_t p32_to_i64( posit32_t ); + +posit8_t p32_to_p8( posit32_t ); +posit16_t p32_to_p16( posit32_t ); +//posit64_t p32_to_p64( posit32_t ); + + +posit32_t p32_roundToInt( posit32_t ); +posit32_t p32_add( posit32_t, posit32_t ); +posit32_t p32_sub( posit32_t, posit32_t ); +posit32_t p32_mul( posit32_t, posit32_t ); +posit32_t p32_mulAdd( posit32_t, posit32_t, posit32_t ); +posit32_t p32_div( posit32_t, posit32_t ); +posit32_t p32_sqrt( posit32_t ); +bool p32_eq( posit32_t, posit32_t ); +bool p32_le( posit32_t, posit32_t ); +bool p32_lt( posit32_t, posit32_t ); + +posit_1_t p32_to_pX1( posit32_t, int); +posit_2_t p32_to_pX2( posit32_t, int ); + +#define isNaRP32UI( a ) ( ((a) ^ 0x80000000) == 0 ) + +int64_t p32_int( posit32_t); + +#ifdef SOFTPOSIT_QUAD + __float128 convertP32ToQuad(posit32_t); + posit32_t convertQuadToP32(__float128); +#endif + + +quire32_t q32_fdp_add(quire32_t, posit32_t, posit32_t); +quire32_t q32_fdp_sub(quire32_t, posit32_t, posit32_t); +posit32_t q32_to_p32(quire32_t); +#define isNaRQ32( q ) ( q.v[0]==0x8000000000000000ULL && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +#define isQ32Zero(q) (q.v[0]==0 && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +quire32_t q32_TwosComplement(quire32_t); + +#define q32_clr(q) ({\ + q.v[0]=0;\ + q.v[1]=0;\ + q.v[2]=0;\ + q.v[3]=0;\ + q.v[4]=0;\ + q.v[5]=0;\ + q.v[6]=0;\ + q.v[7]=0;\ + q;\ +}) + +static inline quire32_t q32Clr(){ + quire32_t q; + q.v[0]=0; + q.v[1]=0; + q.v[2]=0; + q.v[3]=0; + q.v[4]=0; + q.v[5]=0; + q.v[6]=0; + q.v[7]=0; + return q; +} + +#define castQ32(l0, l1, l2, l3, l4, l5, l6, l7)({\ + union ui512_q32 uA;\ + uA.ui[0] = l0; \ + uA.ui[1] = l1; \ + uA.ui[2] = l2; \ + uA.ui[3] = l3; \ + uA.ui[4] = l4; \ + uA.ui[5] = l5; \ + uA.ui[6] = l6; \ + uA.ui[7] = l7; \ + uA.q;\ +}) + + +#define castP32(a)({\ + posit32_t pA = {.v = (a)};\ + pA; \ +}) + + + +#define negP32(a)({\ + union ui32_p32 uA;\ + uA.p = (a);\ + uA.ui = -uA.ui&0xFFFFFFFF;\ + uA.p; \ +}) + +#define absP32(a)({\ + union ui32_p32 uA;\ + uA.p = (a);\ + int mask = uA.ui >> 31; \ + uA.ui = ((uA.ui + mask) ^ mask)&0xFFFFFFFF; \ + uA.p; \ +}) + +//Helper + +double convertP32ToDouble(posit32_t); +posit32_t convertFloatToP32(float); +posit32_t convertDoubleToP32(double); + + +/*---------------------------------------------------------------------------- +| Dyanamic 2 to 32-bit Posits for es = 2 +*----------------------------------------------------------------------------*/ + +posit_2_t pX2_add( posit_2_t, posit_2_t, int); +posit_2_t pX2_sub( posit_2_t, posit_2_t, int); +posit_2_t pX2_mul( posit_2_t, posit_2_t, int); +posit_2_t pX2_div( posit_2_t, posit_2_t, int); +posit_2_t pX2_mulAdd( posit_2_t, posit_2_t, posit_2_t, int); +posit_2_t pX2_roundToInt( posit_2_t, int ); +posit_2_t ui32_to_pX2( uint32_t, int ); +posit_2_t ui64_to_pX2( uint64_t, int ); +posit_2_t i32_to_pX2( int32_t, int ); +posit_2_t i64_to_pX2( int64_t, int ); +posit_2_t pX2_sqrt( posit_2_t, int ); + +uint_fast32_t pX2_to_ui32( posit_2_t ); +uint_fast64_t pX2_to_ui64( posit_2_t ); +int_fast32_t pX2_to_i32( posit_2_t ); +int_fast64_t pX2_to_i64( posit_2_t ); +int64_t pX2_int( posit_2_t ); + +bool pX2_eq( posit_2_t, posit_2_t); +bool pX2_le( posit_2_t, posit_2_t); +bool pX2_lt( posit_2_t, posit_2_t); + +posit8_t pX2_to_p8( posit_2_t ); +posit16_t pX2_to_p16( posit_2_t ); +posit_2_t pX2_to_pX2( posit_2_t, int); +posit_1_t pX2_to_pX1( posit_2_t, int); +static inline posit32_t pX2_to_p32(posit_2_t pA){ + posit32_t p32 = {.v = pA.v}; + return p32; +} + +#define isNaRPX2UI( a ) ( ((a) ^ 0x80000000) == 0 ) + +//Helper +posit_2_t convertDoubleToPX2(double, int); + +double convertPX2ToDouble(posit_2_t); + +#ifdef SOFTPOSIT_QUAD + __float128 convertPX2ToQuad(posit_2_t); + posit_2_t convertQuadToPX2(__float128, int); +#endif + + +quire_2_t qX2_fdp_add( quire_2_t q, posit_2_t pA, posit_2_t ); +quire_2_t qX2_fdp_sub( quire_2_t q, posit_2_t pA, posit_2_t ); +posit_2_t qX2_to_pX2(quire_2_t, int); +#define isNaRQX2( q ) ( q.v[0]==0x8000000000000000ULL && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +#define isQX2Zero(q) (q.v[0]==0 && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +quire_2_t qX2_TwosComplement(quire_2_t); + +#define qX2_clr(q) ({\ + q.v[0]=0;\ + q.v[1]=0;\ + q.v[2]=0;\ + q.v[3]=0;\ + q.v[4]=0;\ + q.v[5]=0;\ + q.v[6]=0;\ + q.v[7]=0;\ + q;\ +}) + +static inline quire_2_t qX2Clr(){ + quire_2_t q; + q.v[0]=0; + q.v[1]=0; + q.v[2]=0; + q.v[3]=0; + q.v[4]=0; + q.v[5]=0; + q.v[6]=0; + q.v[7]=0; + return q; +} + +#define castQX2(l0, l1, l2, l3, l4, l5, l6, l7)({\ + union ui512_qX2 uA;\ + uA.ui[0] = l0; \ + uA.ui[1] = l1; \ + uA.ui[2] = l2; \ + uA.ui[3] = l3; \ + uA.ui[4] = l4; \ + uA.ui[5] = l5; \ + uA.ui[6] = l6; \ + uA.ui[7] = l7; \ + uA.q;\ +}) + + +#define castPX2(a)({\ + posit_2_t pA = {.v = (a)};\ + pA; \ +}) + + + +#define negPX2(a)({\ + union ui32_pX2 uA;\ + uA.p = (a);\ + uA.ui = -uA.ui&0xFFFFFFFF;\ + uA.p; \ +}) + +#define absPX2(a)({\ + union ui32_pX2 uA;\ + uA.p = (a);\ + int mask = uA.ui >> 31; \ + uA.ui = ((uA.ui + mask) ^ mask)&0xFFFFFFFF; \ + uA.p; \ +}) + +/*---------------------------------------------------------------------------- +| Dyanamic 2 to 32-bit Posits for es = 1 +*----------------------------------------------------------------------------*/ + +posit_1_t pX1_add( posit_1_t, posit_1_t, int); +posit_1_t pX1_sub( posit_1_t, posit_1_t, int); +posit_1_t pX1_mul( posit_1_t, posit_1_t, int); +posit_1_t pX1_div( posit_1_t, posit_1_t, int); +posit_1_t pX1_mulAdd( posit_1_t, posit_1_t, posit_1_t, int); +posit_1_t pX1_roundToInt( posit_1_t, int ); +posit_1_t ui32_to_pX1( uint32_t, int ); +posit_1_t ui64_to_pX1( uint64_t, int ); +posit_1_t i32_to_pX1( int32_t, int ); +posit_1_t i64_to_pX1( int64_t, int ); +posit_1_t pX1_sqrt( posit_1_t, int ); + +uint_fast32_t pX1_to_ui32( posit_1_t ); +uint_fast64_t pX1_to_ui64( posit_1_t ); +int_fast32_t pX1_to_i32( posit_1_t ); +int_fast64_t pX1_to_i64( posit_1_t ); +int64_t pX1_int( posit_1_t ); + +bool pX1_eq( posit_1_t, posit_1_t); +bool pX1_le( posit_1_t, posit_1_t); +bool pX1_lt( posit_1_t, posit_1_t); + +posit8_t pX1_to_p8( posit_1_t ); +posit16_t pX1_to_p16( posit_1_t ); +posit32_t pX1_to_p32( posit_1_t ); +posit_1_t pX1_to_pX1( posit_1_t, int); +posit_2_t pX1_to_pX2( posit_1_t, int); + + +#define isNaRpX1UI( a ) ( ((a) ^ 0x80000000) == 0 ) + +//Helper +posit_1_t convertDoubleToPX1(double, int); +double convertPX1ToDouble(posit_1_t); + +#ifdef SOFTPOSIT_QUAD + __float128 convertPX1ToQuad(posit_1_t); + posit_1_t convertQuadToPX1(__float128, int); +#endif + + +quire_1_t qX1_fdp_add( quire_1_t q, posit_1_t pA, posit_1_t ); +quire_1_t qX1_fdp_sub( quire_1_t q, posit_1_t pA, posit_1_t ); +posit_1_t qX1_to_pX1(quire_1_t, int); +#define isNaRqX1( q ) ( q.v[0]==0x8000000000000000ULL && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +#define isqX1Zero(q) (q.v[0]==0 && q.v[1]==0 && q.v[2]==0 && q.v[3]==0 && q.v[4]==0 && q.v[5]==0 && q.v[6]==0 && q.v[7]==0) +quire_1_t qX1_TwosComplement(quire_1_t); + +#define qX1_clr(q) ({\ + q.v[0]=0;\ + q.v[1]=0;\ + q.v[2]=0;\ + q.v[3]=0;\ + q.v[4]=0;\ + q.v[5]=0;\ + q.v[6]=0;\ + q.v[7]=0;\ + q;\ +}) + +static inline quire_1_t qX1Clr(){ + quire_1_t q; + q.v[0]=0; + q.v[1]=0; + q.v[2]=0; + q.v[3]=0; + q.v[4]=0; + q.v[5]=0; + q.v[6]=0; + q.v[7]=0; + return q; +} + +#define castqX1(l0, l1, l2, l3, l4, l5, l6, l7)({\ + union ui512_qX1 uA;\ + uA.ui[0] = l0; \ + uA.ui[1] = l1; \ + uA.ui[2] = l2; \ + uA.ui[3] = l3; \ + uA.ui[4] = l4; \ + uA.ui[5] = l5; \ + uA.ui[6] = l6; \ + uA.ui[7] = l7; \ + uA.q;\ +}) + + +#define castpX1(a)({\ + posit_1_t pA = {.v = (a)};\ + pA; \ +}) + + + +#define negpX1(a)({\ + union ui32_pX1 uA;\ + uA.p = (a);\ + uA.ui = -uA.ui&0xFFFFFFFF;\ + uA.p; \ +}) + +#define absPX1(a)({\ + union ui32_pX1 uA;\ + uA.p = (a);\ + int mask = uA.ui >> 31; \ + uA.ui = ((uA.ui + mask) ^ mask)&0xFFFFFFFF;\ + uA.p; \ +}) +/*---------------------------------------------------------------------------- +| 64-bit (double-precision) floating-point operations. +*----------------------------------------------------------------------------*/ +/*uint_fast32_t p64_to_ui32( posit64_t, uint_fast16_t, bool ); +uint_fast64_t p64_to_ui64( posit64_t, uint_fast16_t, bool ); +int_fast32_t p64_to_i32( posit64_t, uint_fast16_t, bool ); +int_fast64_t p64_to_i64( posit64_t, uint_fast16_t, bool ); + +posit8_t p64_to_p8( posit64_t ); +posit16_t p64_to_p16( posit64_t ); +posit32_t p64_to_p32( posit64_t ); + +posit64_t p64_roundToInt( posit64_t, uint_fast16_t, bool ); +posit64_t p64_add( posit64_t, posit64_t ); +posit64_t p64_sub( posit64_t, posit64_t ); +posit64_t p64_mul( posit64_t, posit64_t ); +posit64_t p64_mulAdd( posit64_t, posit64_t, posit64_t ); +posit64_t p64_div( posit64_t, posit64_t ); +posit64_t p64_rem( posit64_t, posit64_t ); +posit64_t p64_sqrt( posit64_t ); +bool p64_eq( posit64_t, posit64_t ); +bool p64_le( posit64_t, posit64_t );*/ + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/source/luametatex/source/libraries/softposit/source/include/softposit_cpp.h b/source/luametatex/source/libraries/softposit/source/include/softposit_cpp.h new file mode 100644 index 000000000..bafdbe188 --- /dev/null +++ b/source/luametatex/source/libraries/softposit/source/include/softposit_cpp.h @@ -0,0 +1,1928 @@ +/* +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 <iostream> +#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<<x)&0xFF; + return ans; + } + + posit8& operator<<=(const int &x) { + value = (value<<x)&0xFF; + return *this; + } + + + //Negate + posit8 operator-() const{ + posit8 ans; + ans.value = -value; + return ans; + } + + //NOT + posit8 operator~() { + posit8 ans; + ans.value = ~value; + return ans; + } + + //AND + posit8 operator&(const posit8 &a) const{ + posit8 ans; + ans.value = (value & a.value); + return *this; + } + + //AND equal + posit8& operator&=(const posit8 &a) { + value = (value & a.value); + return *this; + } + + //OR + posit8 operator|(const posit8 &a) const{ + posit8 ans; + ans.value = (value | a.value); + return ans; + } + + + //OR equal + posit8& operator|=(const posit8 &a) { + value = (value | a.value); + return *this; + } + + //XOR + posit8 operator^(const posit8 &a) const{ + posit8 ans; + ans.value = (value ^ a.value); + return ans; + } + + //XOR equal + posit8& operator^=(const posit8 &a) { + value = (value ^ a.value); + return *this; + } + + //Logical Operator + //! + bool operator!()const{ + return !value; + } + + //&& + bool operator&&(const posit8 &a) const{ + return (value && a.value); + } + + //|| + bool operator||(const posit8 &a) const{ + return (value || a.value); + } + + bool isNaR(){ + return isNaRP8UI(value); + } + + double toDouble()const{ + return convertP8ToDouble(castP8(value)); + } + + long long int toInt()const{ + return p8_int(castP8(value)); + } + + long long int toRInt()const{ + return p8_to_i64(castP8(value)); + } + posit8& sqrt(){ + value = castUI( p8_sqrt(castP8(value)) ); + return *this; + } + posit8& rint(){ + value = castUI( p8_roundToInt(castP8(value)) ); + return *this; + } + posit8 fma(posit8 a, posit8 b){ // + (a*b) + posit8 ans; + ans.value = castUI(p8_mulAdd(castP8(a.value), castP8(b.value), castP8(value))); + return ans; + } + posit8& toNaR(){ + value = 0x80; + return *this; + } +}; + + +struct posit16{ + uint16_t value; + posit16(double x=0) : value(castUI(convertDoubleToP16(x))) { + } + + //Equal + posit16& operator=(const double a) { + value = castUI(convertDoubleToP16(a)); + return *this; + } + posit16& operator=(const int a) { + value = castUI(i32_to_p16(a)); + return *this; + } + + //Add + posit16 operator+(const posit16 &a) const{ + posit16 ans; + ans.value = castUI(p16_add(castP16(value), castP16(a.value))); + return ans; + } + + //Add equal + posit16& operator+=(const posit16 &a) { + value = castUI(p16_add(castP16(value), castP16(a.value))); + return *this; + } + + //Subtract + posit16 operator-(const posit16 &a) const{ + posit16 ans; + ans.value = castUI(p16_sub(castP16(value), castP16(a.value))); + return ans; + } + + //Subtract equal + posit16& operator-=(const posit16 &a) { + value = castUI(p16_sub(castP16(value), castP16(a.value))); + return *this; + } + + //Multiply + posit16 operator*(const posit16 &a) const{ + posit16 ans; + ans.value = castUI(p16_mul(castP16(value), castP16(a.value))); + return ans; + } + + //Multiply equal + posit16& operator*=(const posit16 &a) { + value = castUI(p16_mul(castP16(value), castP16(a.value))); + return *this; + } + + + //Divide + posit16 operator/(const posit16 &a) const{ + posit16 ans; + ans.value = castUI(p16_div(castP16(value), castP16(a.value))); + return ans; + } + + //Divide equal + posit16& operator/=(const posit16 &a) { + value = castUI(p16_div(castP16(value), castP16(a.value))); + return *this; + } + + //less than + bool operator<(const posit16 &a) const{ + return p16_lt(castP16(value), castP16(a.value)); + } + + //less than equal + bool operator<=(const posit16 &a) const{ + return p16_le(castP16(value), castP16(a.value)); + } + + //equal + bool operator==(const posit16 &a) const{ + return p16_eq(castP16(value), castP16(a.value)); + } + + + //Not equal + bool operator!=(const posit16 &a) const{ + return !p16_eq(castP16(value), castP16(a.value)); + } + + //greater than + bool operator>(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<<x)&0xFFFF; + return ans; + } + + posit16& operator<<=(const int &x) { + value = (value<<x)&0xFFFF; + return *this; + } + + //Negate + posit16 operator-() const{ + posit16 ans; + ans.value = -value; + return ans; + } + + //Binary NOT + posit16 operator~() { + posit16 ans; + ans.value = ~value; + return ans; + } + + //AND + posit16 operator&(const posit16 &a) const{ + posit16 ans; + ans.value = (value & a.value); + return ans; + } + + //AND equal + posit16& operator&=(const posit16 &a) { + value = (value & a.value); + return *this; + } + + //OR + posit16 operator|(const posit16 &a) const{ + posit16 ans; + ans.value = (value | a.value); + return ans; + } + + + //OR equal + posit16& operator|=(const posit16 &a) { + value = (value | a.value); + return *this; + } + + //XOR + posit16 operator^(const posit16 &a) const{ + posit16 ans; + ans.value = (value ^ a.value); + return ans; + } + + //XOR equal + posit16& operator^=(const posit16 &a) { + value = (value ^ a.value); + return *this; + } + + //Logical operator + //! + bool operator!()const{ + return !value; + } + + //&& + bool operator&&(const posit16 &a) const{ + return (value && a.value); + } + + //|| + bool operator||(const posit16 &a) const{ + return (value || a.value); + } + + bool isNaR(){ + return isNaRP16UI(value); + } + + double toDouble()const{ + return convertP16ToDouble(castP16(value)); + } + + long long int toInt()const{ + return p16_int(castP16(value)); + } + + long long int toRInt()const{ + return p16_to_i64(castP16(value)); + } + posit16& sqrt(){ + value = castUI( p16_sqrt(castP16(value)) ); + return *this; + } + posit16& rint(){ + value = castUI( p16_roundToInt(castP16(value)) ); + return *this; + } + posit16 fma(posit16 a, posit16 b){ // + (a*b) + posit16 ans; + ans.value = castUI(p16_mulAdd(castP16(a.value), castP16(b.value), castP16(value))); + return ans; + } + posit16& toNaR(){ + value = 0x8000; + return *this; + } + + +}; + +struct posit32{ + uint32_t value; + posit32(double x=0) : value(castUI(convertDoubleToP32(x))) { + } + + //Equal + posit32& operator=(const double a) { + value = castUI(convertDoubleToP32(a)); + return *this; + } + posit32& operator=(const int a) { + value = castUI(i32_to_p32(a)); + return *this; + } + + //Add + posit32 operator+(const posit32 &a) const{ + posit32 ans; + ans.value = castUI(p32_add(castP32(value), castP32(a.value))); + return ans; + } + + //Add equal + posit32& operator+=(const posit32 &a) { + value = castUI(p32_add(castP32(value), castP32(a.value))); + return *this; + } + + //Subtract + posit32 operator-(const posit32 &a) const{ + posit32 ans; + ans.value = castUI(p32_sub(castP32(value), castP32(a.value))); + return ans; + } + + //Subtract equal + posit32& operator-=(const posit32 &a) { + value = castUI(p32_sub(castP32(value), castP32(a.value))); + return *this; + } + + //Multiply + posit32 operator*(const posit32 &a) const{ + posit32 ans; + ans.value = castUI(p32_mul(castP32(value), castP32(a.value))); + return ans; + } + + //Multiply equal + posit32& operator*=(const posit32 &a) { + value = castUI(p32_mul(castP32(value), castP32(a.value))); + return *this; + } + + + //Divide + posit32 operator/(const posit32 &a) const{ + posit32 ans; + ans.value = castUI(p32_div(castP32(value), castP32(a.value))); + return ans; + } + + //Divide equal + posit32& operator/=(const posit32 &a) { + value = castUI(p32_div(castP32(value), castP32(a.value))); + return *this; + } + + //less than + bool operator<(const posit32 &a) const{ + return p32_lt(castP32(value), castP32(a.value)); + } + + //less than equal + bool operator<=(const posit32 &a) const{ + return p32_le(castP32(value), castP32(a.value)); + } + + //equal + bool operator==(const posit32 &a) const{ + return p32_eq(castP32(value), castP32(a.value)); + } + + + //Not equalCPP + bool operator!=(const posit32 &a) const{ + return !p32_eq(castP32(value), castP32(a.value)); + } + + //greater than + bool operator>(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<<x)&0xFFFFFFFF; + return ans; + } + + posit32& operator<<=(const int &x) { + value = (value<<x)&0xFFFFFFFF; + return *this; + } + + + //Negate + posit32 operator-() const{ + posit32 ans; + ans.value = -value; + return ans; + } + + //NOT + posit32 operator~() { + posit32 ans; + ans.value = ~value; + return ans; + } + + //AND + posit32 operator&(const posit32 &a) const{ + posit32 ans; + ans.value = (value & a.value); + return *this; + } + + //AND equal + posit32& operator&=(const posit32 &a) { + value = (value & a.value); + return *this; + } + + //OR + posit32 operator|(const posit32 &a) const{ + posit32 ans; + ans.value = (value | a.value); + return ans; + } + + + //OR equal + posit32& operator|=(const posit32 &a) { + value = (value | a.value); + return *this; + } + + //XOR + posit32 operator^(const posit32 &a) const{ + posit32 ans; + ans.value = (value ^ a.value); + return ans; + } + + //XOR equal + posit32& operator^=(const posit32 &a) { + value = (value ^ a.value); + return *this; + } + + //Logical Operator + //! + bool operator!()const{ + return !value; + } + + //&& + bool operator&&(const posit32 &a) const{ + return (value && a.value); + } + + //|| + bool operator||(const posit32 &a) const{ + return (value || a.value); + } + + bool isNaR(){ + return isNaRP32UI(value); + } + + double toDouble()const{ + return convertP32ToDouble(castP32(value)); + } + + long long int toInt()const{ + return p32_int(castP32(value)); + } + + long long int toRInt()const{ + return p32_to_i64(castP32(value)); + } + posit32& sqrt(){ + value = castUI( p32_sqrt(castP32(value)) ); + return *this; + } + posit32& rint(){ + value = castUI( p32_roundToInt(castP32(value)) ); + return *this; + } + posit32 fma(posit32 a, posit32 b){ // + (a*b) + posit32 ans; + ans.value = castUI(p32_mulAdd(castP32(a.value), castP32(b.value), castP32(value))); + return ans; + } + + posit32& toNaR(){ + value = 0x80000000; + return *this; + } + + +}; + +struct posit_2{ + uint32_t value; + int x; + posit_2(double v=0, int x=32) : value(castUI(convertDoubleToPX2(v, x))), x(x) { + } + + //Equal + posit_2& operator=(const double a) { + value = castUI(convertDoubleToPX2(a, x)); + return *this; + } + posit_2& operator=(const int a) { + value = castUI(i32_to_pX2(a, x)); + return *this; + } + + //Add + posit_2 operator+(const posit_2 &a) const{ + posit_2 ans; + ans.value = castUI(pX2_add(castPX2(value), castPX2(a.value), x)); + ans.x = x; + return ans; + } + + //Add equal + posit_2& operator+=(const posit_2 &a) { + value = castUI(pX2_add(castPX2(value), castPX2(a.value), x)); + return *this; + } + + //Subtract + posit_2 operator-(const posit_2 &a) const{ + posit_2 ans; + ans.value = castUI(pX2_sub(castPX2(value), castPX2(a.value), x)); + ans.x = x; + return ans; + } + + //Subtract equal + posit_2& operator-=(const posit_2 &a) { + value = castUI(pX2_sub(castPX2(value), castPX2(a.value), x)); + return *this; + } + + //Multiply + posit_2 operator*(const posit_2 &a) const{ + posit_2 ans; + ans.value = castUI(pX2_mul(castPX2(value), castPX2(a.value), x)); + ans.x = x; + return ans; + } + + //Multiply equal + posit_2& operator*=(const posit_2 &a) { + value = castUI(pX2_mul(castPX2(value), castPX2(a.value), x)); + return *this; + } + + + //Divide + posit_2 operator/(const posit_2 &a) const{ + posit_2 ans; + ans.value = castUI(pX2_div(castPX2(value), castPX2(a.value), x)); + ans.x = x; + return ans; + } + + //Divide equal + posit_2& operator/=(const posit_2 &a) { + value = castUI(pX2_div(castPX2(value), castPX2(a.value), x)); + return *this; + } + + //less than + bool operator<(const posit_2 &a) const{ + return pX2_lt(castPX2(value), castPX2(a.value)); + } + + //less than equal + bool operator<=(const posit_2 &a) const{ + return pX2_le(castPX2(value), castPX2(a.value)); + } + + //equal + bool operator==(const posit_2 &a) const{ + return pX2_eq(castPX2(value), castPX2(a.value)); + } + + + //Not equalCPP + bool operator!=(const posit_2 &a) const{ + return !pX2_eq(castPX2(value), castPX2(a.value)); + } + + //greater than + bool operator>(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<<x)&0xFFFFFFFF; + ans.x = x; + return ans; + } + + posit_2& operator<<=(const int &x) { + value = (value<<x)&0xFFFFFFFF; + return *this; + } + + + //Negate + posit_2 operator-() const{ + posit_2 ans; + ans.value = -value; + ans.x = x; + return ans; + } + + //NOT + posit_2 operator~() { + posit_2 ans; + ans.value = ~value; + ans.x = x; + return ans; + } + + //AND + posit_2 operator&(const posit_2 &a) const{ + posit_2 ans; + ans.value = (value & a.value); + return *this; + } + + //AND equal + posit_2& operator&=(const posit_2 &a) { + value = (value & a.value); + return *this; + } + + //OR + posit_2 operator|(const posit_2 &a) const{ + posit_2 ans; + ans.value = (value | a.value); + return ans; + } + + + //OR equal + posit_2& operator|=(const posit_2 &a) { + value = (value | a.value); + return *this; + } + + //XOR + posit_2 operator^(const posit_2 &a) const{ + posit_2 ans; + ans.value = (value ^ a.value); + return ans; + } + + //XOR equal + posit_2& operator^=(const posit_2 &a) { + value = (value ^ a.value); + return *this; + } + + //Logical Operator + //! + bool operator!()const{ + return !value; + } + + //&& + bool operator&&(const posit_2 &a) const{ + return (value && a.value); + } + + //|| + bool operator||(const posit_2 &a) const{ + return (value || a.value); + } + + bool isNaR(){ + return isNaRPX2UI(value); + } + + double toDouble()const{ + return convertPX2ToDouble(castPX2(value)); + } + + long long int toInt()const{ + return pX2_int(castPX2(value)); + } + + long long int toRInt()const{ + return pX2_to_i64(castPX2(value)); + } + posit_2& sqrt(){ + value = castUI( pX2_sqrt(castPX2(value), x) ); + return *this; + } + posit_2& rint(){ + value = castUI( pX2_roundToInt(castPX2(value), x) ); + return *this; + } + posit_2 fma(posit_2 a, posit_2 b){ // + (a*b) + posit_2 ans; + ans.value = castUI(pX2_mulAdd(castPX2(a.value), castPX2(b.value), castPX2(value), x)); + ans.x = x; + return ans; + } + + posit_2 toPositX2(int x){ + posit_2 ans; + ans.value = pX2_to_pX2(castPX2(value), x).v; + ans.x = x; + return ans; + } + posit_2& toNaR(){ + value = 0x80000000; + return *this; + } + + +}; + +struct quire8{ + uint32_t value; + + quire8 (uint32_t value=0) : value(value){ + } + + quire8& clr(){ + value = 0; + return *this; + } + + bool isNaR(){ + return isNaRQ8(castQ8(value)); + } + + quire8& qma(posit8 a, posit8 b){ // q += a*b + quire8_t q = q8_fdp_add(castQ8(value), castP8(a.value), castP8(b.value)); + value = q.v; + return *this; + } + quire8& qms(posit16 a, posit16 b){ // q -= a*b + quire8_t q = q8_fdp_sub(castQ8(value), castP8(a.value), castP8(b.value)); + value = q.v; + return *this; + } + posit8 toPosit(){ + posit8 a; + a.value = castUI(q8_to_p8(castQ8(value))); + return a; + } + +}; +struct quire16{ + uint64_t lvalue; + uint64_t rvalue; + + quire16 (uint64_t lvalue=0, uint64_t rvalue=0) : lvalue(lvalue), rvalue(rvalue){ + } + + quire16& clr(){ + lvalue = 0; + rvalue = 0; + return *this; + } + + bool isNaR(){ + return isNaRQ16(castQ16(lvalue, rvalue)); + } + + quire16& qma(posit16 a, posit16 b){ // q += a*b + quire16_t q = q16_fdp_add(castQ16(lvalue, rvalue), castP16(a.value), castP16(b.value)); + lvalue = q.v[0]; + rvalue = q.v[1]; + return *this; + } + quire16& qms(posit16 a, posit16 b){ // q -= a*b + quire16_t q = q16_fdp_sub(castQ16(lvalue, rvalue), castP16(a.value), castP16(b.value)); + lvalue = q.v[0]; + rvalue = q.v[1]; + return *this; + } + posit16 toPosit(){ + posit16 a; + a.value = castUI(q16_to_p16(castQ16(lvalue, rvalue))); + return a; + } + +}; + +struct quire32{ + uint64_t v0; + uint64_t v1; + uint64_t v2; + uint64_t v3; + uint64_t v4; + uint64_t v5; + uint64_t v6; + uint64_t v7; + + quire32 (uint64_t v0=0, uint64_t v1=0, uint64_t v2=0, uint64_t v3=0, uint64_t v4=0, uint64_t v5=0, uint64_t v6=0, uint64_t v7=0) : + v0(v0), v1(v1), v2(v2), v3(v3), v4(v4), v5(v5), v6(v6), v7(v7){ + } + + quire32& clr(){ + v0 = 0; + v1 = 0; + v2 = 0; + v3 = 0; + v4 = 0; + v5 = 0; + v6 = 0; + v7 = 0; + return *this; + } + + bool isNaR(){ + return isNaRQ32(castQ32(v0, v1, v2, v3, v4, v5, v6, v7)); + } + + quire32& qma(posit32 a, posit32 b){ // q += a*b + quire32_t q = q32_fdp_add(castQ32(v0, v1, v2, v3, v4, v5, v6, v7), + castP32(a.value), castP32(b.value)); + v0 = q.v[0]; + v1 = q.v[1]; + v2 = q.v[2]; + v3 = q.v[3]; + v4 = q.v[4]; + v5 = q.v[5]; + v6 = q.v[6]; + v7 = q.v[7]; + return *this; + } + quire32& qms(posit32 a, posit32 b){ // q -= a*b + quire32_t q = q32_fdp_sub(castQ32(v0, v1, v2, v3, v4, v5, v6, v7), castP32(a.value), castP32(b.value)); + v0 = q.v[0]; + v1 = q.v[1]; + v2 = q.v[2]; + v3 = q.v[3]; + v4 = q.v[4]; + v5 = q.v[5]; + v6 = q.v[6]; + v7 = q.v[7]; + return *this; + } + posit32 toPosit(){ + posit32 a; + a.value = castUI(q32_to_p32(castQ32(v0, v1, v2, v3, v4, v5, v6, v7))); + return a; + } + +}; + +struct quire_2{ + uint64_t v0; + uint64_t v1; + uint64_t v2; + uint64_t v3; + uint64_t v4; + uint64_t v5; + uint64_t v6; + uint64_t v7; + int x; + + quire_2 (uint64_t v0=0, uint64_t v1=0, uint64_t v2=0, uint64_t v3=0, uint64_t v4=0, uint64_t v5=0, uint64_t v6=0, uint64_t v7=0, int x=32) : + v0(v0), v1(v1), v2(v2), v3(v3), v4(v4), v5(v5), v6(v6), v7(v7), x(x){ + } + + quire_2& clr(){ + v0 = 0; + v1 = 0; + v2 = 0; + v3 = 0; + v4 = 0; + v5 = 0; + v6 = 0; + v7 = 0; + return *this; + } + + bool isNaR(){ + return isNaRQX2(castQX2(v0, v1, v2, v3, v4, v5, v6, v7)); + } + + quire_2& qma(posit_2 a, posit_2 b){ // q += a*b + quire_2_t q = qX2_fdp_add(castQX2(v0, v1, v2, v3, v4, v5, v6, v7), + castPX2(a.value), castPX2(b.value)); + v0 = q.v[0]; + v1 = q.v[1]; + v2 = q.v[2]; + v3 = q.v[3]; + v4 = q.v[4]; + v5 = q.v[5]; + v6 = q.v[6]; + v7 = q.v[7]; + return *this; + } + quire_2& qms(posit_2 a, posit_2 b){ // q -= a*b + quire_2_t q = qX2_fdp_sub(castQX2(v0, v1, v2, v3, v4, v5, v6, v7), castPX2(a.value), castPX2(b.value)); + v0 = q.v[0]; + v1 = q.v[1]; + v2 = q.v[2]; + v3 = q.v[3]; + v4 = q.v[4]; + v5 = q.v[5]; + v6 = q.v[6]; + v7 = q.v[7]; + return *this; + } + posit_2 toPosit(){ + posit_2 a; + a.value = castUI(qX2_to_pX2(castQX2(v0, v1, v2, v3, v4, v5, v6, v7), x)); + a.x = x; + return a; + } + +}; + +inline posit8 operator+(int a, posit8 b){ + b.value = castUI(p8_add(i32_to_p8(a), castP8(b.value))); + return b; +} +inline posit16 operator+(int a, posit16 b){ + b.value = castUI(p16_add(i32_to_p16(a), castP16(b.value))); + return b; +} +inline posit32 operator+(int a, posit32 b){ + b.value = castUI(p32_add(i32_to_p32(a), castP32(b.value))); + return b; +} +inline posit32 operator+(long long int a, posit32 b){ + b.value = castUI(p32_add(i64_to_p32(a), castP32(b.value))); + return b; +} +inline posit_2 operator+(int a, posit_2 b){ + b.value = castUI(pX2_add(i32_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} +inline posit_2 operator+(long long int a, posit_2 b){ + b.value = castUI(pX2_add(i64_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + +inline posit8 operator+(double a, posit8 b){ + b.value = castUI(p8_add(convertDoubleToP8(a), castP8(b.value))); + return b; +} +inline posit16 operator+(double a, posit16 b){ + b.value = castUI(p16_add(convertDoubleToP16(a), castP16(b.value))); + return b; +} +inline posit32 operator+(double a, posit32 b){ + b.value = castUI(p32_add(convertDoubleToP32(a), castP32(b.value))); + return b; +} +inline posit_2 operator+(double a, posit_2 b){ + b.value = castUI(pX2_add(convertDoubleToPX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + +inline posit8 operator-(int a, posit8 b){ + b.value = castUI(p8_sub(i32_to_p8(a), castP8(b.value))); + return b; +} +inline posit16 operator-(int a, posit16 b){ + b.value = castUI(p16_sub(i32_to_p16(a), castP16(b.value))); + return b; +} +inline posit32 operator-(int a, posit32 b){ + b.value = castUI(p32_sub(i32_to_p32(a), castP32(b.value))); + return b; +} +inline posit32 operator-(long long int a, posit32 b){ + b.value = castUI(p32_sub(i64_to_p32(a), castP32(b.value))); + return b; +} +inline posit_2 operator-(int a, posit_2 b){ + b.value = castUI(pX2_sub(i32_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} +inline posit_2 operator-(long long int a, posit_2 b){ + b.value = castUI(pX2_sub(i64_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + +inline posit8 operator-(double a, posit8 b){ + b.value = castUI(p8_sub(convertDoubleToP8(a), castP8(b.value))); + return b; +} +inline posit16 operator-(double a, posit16 b){ + b.value = castUI(p16_sub(convertDoubleToP16(a), castP16(b.value))); + return b; +} +inline posit32 operator-(double a, posit32 b){ + b.value = castUI(p32_sub(convertDoubleToP32(a), castP32(b.value))); + return b; +} +inline posit_2 operator-(double a, posit_2 b){ + b.value = castUI(pX2_sub(convertDoubleToPX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + + +inline posit8 operator/(int a, posit8 b){ + b.value = castUI(p8_div(i32_to_p8(a), castP8(b.value))); + return b; +} +inline posit16 operator/(int a, posit16 b){ + b.value = castUI(p16_div(i32_to_p16(a), castP16(b.value))); + return b; +} +inline posit32 operator/(int a, posit32 b){ + b.value = castUI(p32_div(i32_to_p32(a), castP32(b.value))); + return b; +} +inline posit32 operator/(long long int a, posit32 b){ + b.value = castUI(p32_div(i64_to_p32(a), castP32(b.value))); + return b; +} +inline posit_2 operator/(int a, posit_2 b){ + b.value = castUI(pX2_div(i32_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} +inline posit_2 operator/(long long int a, posit_2 b){ + b.value = castUI(pX2_div(i64_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + + +inline posit8 operator/(double a, posit8 b){ + b.value = castUI(p8_div(convertDoubleToP8(a), castP8(b.value))); + return b; +} +inline posit16 operator/(double a, posit16 b){ + b.value = castUI(p16_div(convertDoubleToP16(a), castP16(b.value))); + return b; +} +inline posit32 operator/(double a, posit32 b){ + b.value = castUI(p32_div(convertDoubleToP32(a), castP32(b.value))); + return b; +} +inline posit_2 operator/(double a, posit_2 b){ + b.value = castUI(pX2_div(convertDoubleToPX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + + +inline posit8 operator*(int a, posit8 b){ + b.value = castUI(p8_mul(i32_to_p8(a), castP8(b.value))); + return b; +} +inline posit16 operator*(int a, posit16 b){ + posit16 ans; + ans.value = castUI(p16_mul(i32_to_p16(a), castP16(b.value))); + return ans; +} +inline posit32 operator*(int a, posit32 b){ + b.value = castUI(p32_mul(i32_to_p32(a), castP32(b.value))); + return b; +} +inline posit32 operator*(long long int a, posit32 b){ + b.value = castUI(p32_mul(i64_to_p32(a), castP32(b.value))); + return b; +} +inline posit_2 operator*(int a, posit_2 b){ + b.value = castUI(pX2_mul(i32_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} +inline posit_2 operator*(long long int a, posit_2 b){ + b.value = castUI(pX2_mul(i64_to_pX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + +inline posit8 operator*(double a, posit8 b){ + b.value = castUI(p8_mul(convertDoubleToP8(a), castP8(b.value))); + return b; +} +inline posit16 operator*(double a, posit16 b){ + posit16 ans; + ans.value = castUI(p16_mul(convertDoubleToP16(a), castP16(b.value))); + return ans; +} +inline posit32 operator*(double a, posit32 b){ + b.value = castUI(p32_mul(convertDoubleToP32(a), castP32(b.value))); + return b; +} +inline posit_2 operator*(double a, posit_2 b){ + b.value = castUI(pX2_mul(convertDoubleToPX2(a, b.x), castPX2(b.value), b.x)); + return b; +} + + + +//fused-multiply-add +inline posit8 fma(posit8 a, posit8 b, posit8 c){ // (a*b) + c + posit8 ans; + ans.value = castUI(p8_mulAdd(castP8(a.value), castP8(b.value), castP8(c.value))); + return ans; +} +inline posit16 fma(posit16 a, posit16 b, posit16 c){ // (a*b) + c + posit16 ans; + ans.value = castUI(p16_mulAdd(castP16(a.value), castP16(b.value), castP16(c.value))); + return ans; +} +inline posit32 fma(posit32 a, posit32 b, posit32 c){ // (a*b) + c + posit32 ans; + ans.value = castUI(p32_mulAdd(castP32(a.value), castP32(b.value), castP32(c.value))); + return ans; +} +inline posit_2 fma(posit_2 a, posit_2 b, posit_2 c){ // (a*b) + c + posit_2 ans; + ans.value = castUI(pX2_mulAdd(castPX2(a.value), castPX2(b.value), castPX2(c.value), c.x)); + ans.x = c.x; + return ans; +} + + +//Round to nearest integer +inline posit8 rint(posit8 a){ + posit8 ans; + ans.value = castUI( p8_roundToInt(castP8(a.value)) ); + return ans; +} +inline posit16 rint(posit16 a){ + posit16 ans; + ans.value = castUI( p16_roundToInt(castP16(a.value)) ); + return ans; +} +inline posit32 rint(posit32 a){ + posit32 ans; + ans.value = castUI( p32_roundToInt(castP32(a.value)) ); + return ans; +} +inline posit_2 rint(posit_2 a){ + posit_2 ans; + ans.value = castUI( pX2_roundToInt(castPX2(a.value), a.x) ); + ans.x = a.x; + return ans; +} + +//Square root +inline posit8 sqrt(posit8 a){ + posit8 ans; + ans.value = castUI( p8_sqrt(castP8(a.value)) ); + return ans; +} +inline posit16 sqrt(posit16 a){ + posit16 ans; + ans.value = castUI( p16_sqrt(castP16(a.value)) ); + return ans; +} +inline posit32 sqrt(posit32 a){ + posit32 ans; + ans.value = castUI( p32_sqrt(castP32(a.value)) ); + return ans; +} +inline posit_2 sqrt(posit_2 a){ + posit_2 ans; + ans.value = castUI( pX2_sqrt(castPX2(a.value), a.x) ); + ans.x = a.x; + return ans; +} + + + +// Convert to integer + +inline uint32_t uint32 (posit8 a){ + return p8_to_ui32(castP8(a.value)); +} +inline uint32_t uint32 (posit16 a){ + return p16_to_ui32(castP16(a.value)); +} +inline uint32_t uint32 (posit32 a){ + return p32_to_ui32(castP32(a.value)); +} +inline uint32_t uint32 (posit_2 a){ + return pX2_to_ui32(castPX2(a.value)); +} + + + +inline int32_t int32(posit8 a){ + return p8_to_i32(castP8(a.value)); +} +inline int32_t int32(posit16 a){ + return p16_to_i32(castP16(a.value)); +} +inline int32_t int32 (posit32 a){ + return p32_to_i32(castP32(a.value)); +} +inline int32_t int32 (posit_2 a){ + return pX2_to_i32(castPX2(a.value)); +} + + + +inline uint64_t uint64(posit8 a){ + return p8_to_ui64(castP8(a.value)); +} +inline uint64_t uint64(posit16 a){ + return p16_to_ui64(castP16(a.value)); +} +inline uint64_t uint64 (posit32 a){ + return p32_to_ui64(castP32(a.value)); +} +inline uint64_t uint64 (posit_2 a){ + return pX2_to_ui64(castPX2(a.value)); +} + + + +inline int64_t int64(posit8 a){ + return p8_to_i64(castP8(a.value)); +} +inline int64_t int64(posit16 a){ + return p16_to_i64(castP16(a.value)); +} +inline int64_t int64 (posit32 a){ + return p32_to_i64(castP32(a.value)); +} +inline int64_t int64 (posit_2 a){ + return pX2_to_i64(castPX2(a.value)); +} + + +//Convert To Posit +inline posit8 p8(posit16 a){ + posit8 b; + b.value = castUI(p16_to_p8(castP16(a.value))); + return b; +} +inline posit8 p8(posit32 a){ + posit8 b; + b.value = castUI(p32_to_p8(castP32(a.value))); + return b; +} +inline posit8 p8(posit_2 a){ + posit8 b; + b.value = castUI(pX2_to_p8(castPX2(a.value))); + return b; +} + + +inline posit16 p16(posit8 a){ + posit16 b; + b.value = castUI(p8_to_p16(castP8(a.value))); + return b; +} +inline posit16 p16(posit32 a){ + posit16 b; + b.value = castUI(p32_to_p16(castP32(a.value))); + return b; +} +inline posit16 p16(posit_2 a){ + posit16 b; + b.value = castUI(pX2_to_p16(castPX2(a.value))); + return b; +} + + +inline posit32 p32(posit8 a){ + posit32 b; + b.value = castUI(p8_to_p32(castP8(a.value))); + return b; +} +inline posit32 p32(posit16 a){ + posit32 b; + b.value = castUI(p16_to_p32(castP16(a.value))); + return b; +} +inline posit32 p32(posit_2 a){ + posit32 b; + b.value = castUI(pX2_to_p32(castPX2(a.value))); + return b; +} + + +inline posit_2 pX2(posit8 a, int x){ + posit_2 b; + b.value = castUI(p8_to_pX2(castP8(a.value), x)); + b.x = x; + return b; +} +inline posit_2 pX2(posit16 a, int x){ + posit_2 b; + b.value = castUI(p16_to_pX2(castP16(a.value), x)); + b.x = x; + return b; +} +inline posit_2 pX2(posit32 a, int x){ + posit_2 b; + b.value = castUI(p32_to_pX2(castP32(a.value), x)); + b.x = x; + return b; +} +inline posit_2 pX2(posit_2 a, int x){ + posit_2 b; + b.value = castUI(pX2_to_pX2(castPX2(a.value), x)); + b.x = x; + return b; +} + + + +inline posit8 p8(uint32_t a){ + posit8 b; + b.value = castUI(ui32_to_p8(a)); + return b; +} +inline posit16 p16(uint32_t a){ + posit16 b; + b.value = castUI(ui32_to_p16(a)); + return b; +} +inline posit32 p32(uint32_t a){ + posit32 b; + b.value = castUI(ui32_to_p32(a)); + return b; +} +inline posit_2 pX2(uint32_t a, int x){ + posit_2 b; + b.value = castUI(ui32_to_pX2(a, x)); + b.x = x; + return b; +} + + +inline posit8 p8(int32_t a){ + posit8 b; + b.value = castUI(i32_to_p8(a)); + return b; +} +inline posit16 p16(int32_t a){ + posit16 b; + b.value = castUI(i32_to_p16(a)); + return b; +} +inline posit32 p32(int32_t a){ + posit32 b; + b.value = castUI(i32_to_p32(a)); + return b; +} +inline posit_2 pX2(int32_t a, int x){ + posit_2 b; + b.value = castUI(i32_to_pX2(a, x)); + b.x = x; + return b; +} + + + +inline posit8 p8(uint64_t a){ + posit8 b; + b.value = castUI(ui64_to_p8(a)); + return b; +} +inline posit16 p16(uint64_t a){ + posit16 b; + b.value = castUI(ui64_to_p16(a)); + return b; +} +inline posit32 p32(uint64_t a){ + posit32 b; + b.value = castUI(ui64_to_p32(a)); + return b; +} +inline posit_2 pX2(uint64_t a, int x){ + posit_2 b; + b.value = castUI(ui64_to_pX2(a, x)); + b.x = x; + return b; +} + + +inline posit8 p8(int64_t a){ + posit8 b; + b.value = castUI(i64_to_p8(a)); + return b; +} +inline posit16 p16(int64_t a){ + posit16 b; + b.value = castUI(i64_to_p16(a)); + return b; +} +inline posit32 p32(int64_t a){ + posit32 b; + b.value = castUI(i64_to_p32(a)); + return b; +} +inline posit_2 p32(int64_t a, int x){ + posit_2 b; + b.value = castUI(i64_to_pX2(a, x)); + b.x = x; + return b; +} + + +inline posit8 p8(double a){ + posit8 b; + b.value = castUI(convertDoubleToP8(a)); + return b; +} +inline posit16 p16(double a){ + posit16 b; + b.value = castUI(convertDoubleToP16(a)); + return b; +} +inline posit32 p32(double a){ + posit32 b; + b.value = castUI(convertDoubleToP32(a)); + return b; +} +inline posit_2 pX2(double a, int x){ + posit_2 b; + b.value = castUI(convertDoubleToPX2(a, x)); + b.x = x; + return b; +} + + + +inline posit8 p8(quire8 a){ + posit8 b; + b.value = castUI(q8_to_p8(castQ8(a.value))); + return b; +} +inline posit16 p16(quire16 a){ + posit16 b; + b.value = castUI(q16_to_p16(castQ16(a.lvalue, a.rvalue))); + return b; +} +inline posit32 p32(quire32 a){ + posit32 b; + b.value = castUI(q32_to_p32(castQ32(a.v0, a.v1, a.v2, a.v3, a.v4, a.v5, a.v6, a.v7))); + return b; +} +inline posit_2 pX2(quire_2 a){ + posit_2 b; + b.value = castUI(qX2_to_pX2(castQX2(a.v0, a.v1, a.v2, a.v3, a.v4, a.v5, a.v6, a.v7), a.x)); + b.x = a.x; + return b; +} +inline posit_2 pX2(quire_2 a, int x){ + posit_2 b; + b.value = castUI(qX2_to_pX2(castQX2(a.v0, a.v1, a.v2, a.v3, a.v4, a.v5, a.v6, a.v7), x)); + b.x = x; + return b; +} + + +//cout helper functions + +inline std::ostream& operator<<(std::ostream& os, const posit8& p) { + os << p.toDouble(); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const posit16& p) { + os << p.toDouble(); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const posit32& p) { + os << p.toDouble(); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const posit_2& p) { + os << p.toDouble(); + return os; +} + +//Math lib + +/*inline posit8 abs(posit8 a){ + a.value = castUI(p8_abs(castP8(a.value))); + return a; +} + +inline posit16 abs(posit16 a){ + a.value = castUI(p16_abs(castP16(a.value))); + return a; +} + + +inline posit32 abs(posit32 a){ + a.value = castUI(p32_abs(castP32(a.value))); + return a; +} + + +inline posit8 ceil(posit8 a){ + a.value = castUI(p8_ceil(castP8(a.value))); + return a; +} + +inline posit16 ceil(posit16 a){ + a.value = castUI(p16_ceil(castP16(a.value))); + return a; +} + +inline posit32 ceil(posit32 a){ + a.value = castUI(p32_ceil(castP32(a.value))); + return a; +} + + +inline posit8 floor(posit8 a){ + a.value = castUI(p8_floor(castP8(a.value))); + return a; +} +inline posit16 floor(posit16 a){ + a.value = castUI(p16_floor(castP16(a.value))); + return a; +} +inline posit32 floor(posit32 a){ + a.value = castUI(p32_floor(castP32(a.value))); + return a; +} + + +inline posit8 exp(posit8 a){ + a.value = castUI(p8_exp(castP8(a.value))); + return a; +} +inline posit16 exp(posit16 a){ + a.value = castUI(p16_exp(castP16(a.value))); + return a; +} +inline posit32 exp(posit32 a){ + a.value = castUI(convertDoubleToP32(exp(convertP32ToDouble(castP32(a.value))))); + return a; +} + + + +inline posit8 pow(posit8 a, posit8 b){ + a.value = castUI(convertDoubleToP8(pow(convertP8ToDouble(castP8(a.value)), convertP8ToDouble(castP8(b.value))))); + return a; +} +inline posit16 pow(posit16 a, posit16 b){ + a.value = castUI(convertDoubleToP16(pow(convertP16ToDouble(castP16(a.value)), convertP16ToDouble(castP16(b.value))))); + return a; +} +inline posit32 pow(posit32 a, posit32 b){ + a.value = castUI(convertDoubleToP32(pow(convertP32ToDouble(castP32(a.value)), convertP32ToDouble(castP32(b.value))))); + return a; +} + + +inline posit8 log(posit8 a){ + a.value = castUI(convertDoubleToP8(log(convertP8ToDouble(castP8(a.value))))); + return a; +} +inline posit16 log(posit16 a){ + a.value = castUI(convertDoubleToP16(log(convertP16ToDouble(castP16(a.value))))); + return a; +} +inline posit32 log(posit32 a){ + a.value = castUI(convertDoubleToP32(log(convertP32ToDouble(castP32(a.value))))); + return a; +} + + +inline posit8 log2(posit8 a){ + a.value = castUI(convertDoubleToP8(log2(convertP8ToDouble(castP8(a.value))))); + return a; +} +inline posit16 log2(posit16 a){ + a.value = castUI(convertDoubleToP16(log2(convertP16ToDouble(castP16(a.value))))); + return a; +} +inline posit32 log2(posit32 a){ + a.value = castUI(convertDoubleToP32(log2(convertP32ToDouble(castP32(a.value))))); + return a; +} + + +inline posit8 cos(posit8 a){ + a.value = castUI(convertDoubleToP8(cos(convertP8ToDouble(castP8(a.value))))); + return a; +} +inline posit16 cos(posit16 a){ + a.value = castUI(convertDoubleToP16(cos(convertP16ToDouble(castP16(a.value))))); + return a; +} +inline posit32 cos(posit32 a){ + a.value = castUI(convertDoubleToP32(cos(convertP32ToDouble(castP32(a.value))))); + return a; +} + + +inline posit8 sin(posit8 a){ + a.value = castUI(convertDoubleToP8(sin(convertP8ToDouble(castP8(a.value))))); + return a; +} +inline posit16 sin(posit16 a){ + a.value = castUI(convertDoubleToP16(sin(convertP16ToDouble(castP16(a.value))))); + return a; +} +inline posit32 sin(posit32 a){ + a.value = castUI(convertDoubleToP32(sin(convertP32ToDouble(castP32(a.value))))); + return a; +} + + +inline posit8 acos(posit8 a){ + a.value = castUI(convertDoubleToP8(acos(convertP8ToDouble(castP8(a.value))))); + return a; +} +inline posit16 acos(posit16 a){ + a.value = castUI(convertDoubleToP16(acos(convertP16ToDouble(castP16(a.value))))); + return a; +} +inline posit32 acos(posit32 a){ + a.value = castUI(convertDoubleToP32(acos(convertP32ToDouble(castP32(a.value))))); + return a; +}*/ + + +#endif //CPLUSPLUS + +#endif /* INCLUDE_SOFTPOSIT_CPP_H_ */ diff --git a/source/luametatex/source/libraries/softposit/source/include/softposit_types.h b/source/luametatex/source/libraries/softposit/source/include/softposit_types.h new file mode 100644 index 000000000..fe862ca36 --- /dev/null +++ b/source/luametatex/source/libraries/softposit/source/include/softposit_types.h @@ -0,0 +1,139 @@ + +/*============================================================================ + +This C header file is part of the SoftPosit Posit Arithmetic Package +by S. H. Leong (Cerlane). + +Copyright 2017, 2018 A*STAR. All rights reserved. + +This C header file was based on SoftFloat IEEE Floating-Point Arithmetic +Package, Release 3d, by John R. Hauser. + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the +University of California. 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. + +=============================================================================*/ + +#ifndef softposit_types_h +#define softposit_types_h 1 + +#include <stdint.h> + +/*---------------------------------------------------------------------------- +| Types used to pass 16-bit, 32-bit, 64-bit, and 128-bit floating-point +| arguments and results to/from functions. These types must be exactly +| 16 bits, 32 bits, 64 bits, and 128 bits in size, respectively. Where a +| platform has "native" support for IEEE-Standard floating-point formats, +| the types below may, if desired, be defined as aliases for the native types +| (typically 'float' and 'double', and possibly 'long double'). +*----------------------------------------------------------------------------*/ + +#ifdef SOFTPOSIT_EXACT + typedef struct { uint8_t v; bool exact; } posit8_t; + typedef struct { uint_fast16_t v; bool exact; } posit16_t; + typedef struct { uint32_t v; bool exact; } posit32_t; + typedef struct { uint64_t v; bool exact; } posit64_t; + typedef struct { uint64_t v[2]; bool exact; } posit128_t; + + typedef struct { uint64_t v[2]; bool exact; } quire16_t; +#else + typedef struct { uint8_t v; } posit8_t; + typedef struct { uint16_t v; } posit16_t; + typedef struct { uint32_t v; } posit32_t; + typedef struct { uint64_t v; } posit64_t; + typedef struct { uint64_t v[2]; } posit128_t; + + typedef struct { uint32_t v; } quire8_t; + typedef struct { uint64_t v[2]; } quire16_t; + typedef struct { uint64_t v[8]; } quire32_t; + + typedef struct { uint32_t v; } posit_2_t; + typedef struct { uint32_t v; } posit_1_t; + typedef struct { uint32_t v; } posit_0_t; + + typedef struct { uint64_t v[8]; } quire_2_t; + typedef struct { uint64_t v[8]; } quire_1_t; + typedef struct { uint64_t v[8]; } quire_0_t; + +#endif + + +#ifdef SOFTPOSIT_EXACT + typedef struct { uint8_t v; bool exact; } uint8e_t; + typedef struct { uint16_t v; bool exact; } uint16e_t; + typedef struct { uint32_t v; bool exact; } uint32e_t; + typedef struct { uint64_t v; bool exact; } uint64e_t; + typedef struct { uint64_t v[2]; bool exact; } uint128e_t; + + union ui8_p8 { uint8e_t ui; posit8_t p; }; + union ui16_p16 { uint16e_t ui; posit16_t p; }; + union ui32_p32 { uint32e_t ui; posit32_t p; }; + union ui64_p64 { uint64e_t ui; posit64_t p; }; + + union ui128_q16 { uint64_t ui[2]; quire16_t q; }; +#else + union ui8_p8 { uint8_t ui; posit8_t p; }; + union ui16_p16 { uint16_t ui; posit16_t p; }; + union ui32_p32 { uint32_t ui; posit32_t p; }; + union ui64_p64 { uint64_t ui; posit64_t p; }; + union ui128_p128c {uint64_t ui[2]; posit128_t p;}; //c to differentiate from original implementation + + union ui32_pX2 { uint32_t ui; posit_2_t p; }; + union ui32_pX1 { uint32_t ui; posit_1_t p; }; + union ui32_pX0 { uint32_t ui; posit_1_t p; }; + + union ui64_double { uint64_t ui; double d; }; + + union ui32_q8 { + uint32_t ui; // =0; // patched by HH because the compilers don't like this + quire8_t q; + }; + union ui128_q16 { + uint64_t ui[2]; // ={0,0}; // idem + quire16_t q; + }; + + union ui512_q32 { + uint64_t ui[8]; // ={0,0,0,0, 0,0,0,0}; // idme + quire32_t q; + }; + + union ui512_qX2 { + uint64_t ui[8]; // ={0,0,0,0, 0,0,0,0}; // idem + quire_2_t q; + }; + + union ui512_qX1 { + uint64_t ui[8]; // ={0,0,0,0, 0,0,0,0}; // idem + quire_1_t q; + }; +#endif + + +#endif + |