summaryrefslogtreecommitdiff
path: root/source/luametatex/source/libraries/softposit
diff options
context:
space:
mode:
Diffstat (limited to 'source/luametatex/source/libraries/softposit')
-rw-r--r--source/luametatex/source/libraries/softposit/LICENSE34
-rw-r--r--source/luametatex/source/libraries/softposit/README.md551
-rw-r--r--source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/Makefile245
-rw-r--r--source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/platform.h48
-rw-r--r--source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/softposit_test.abin0 -> 129840 bytes
-rw-r--r--source/luametatex/source/libraries/softposit/source/8086-SSE/specialize.h50
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertDecToPosit16.c447
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertDecToPosit32.c878
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertDecToPosit8.c211
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertDecToPositX1.c461
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertPosit16ToDec.c172
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertPosit32ToDec.c360
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertPosit8ToDec.c98
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertPositX1ToDec.c184
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertQuire16ToPosit16.c159
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertQuire32ToPosit32.c183
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertQuire8ToPosit8.c123
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_convertQuireX2ToPositX2.c191
-rw-r--r--source/luametatex/source/libraries/softposit/source/c_int.c155
-rw-r--r--source/luametatex/source/libraries/softposit/source/i32_to_p16.c90
-rw-r--r--source/luametatex/source/libraries/softposit/source/i32_to_p32.c87
-rw-r--r--source/luametatex/source/libraries/softposit/source/i32_to_p8.c88
-rw-r--r--source/luametatex/source/libraries/softposit/source/i32_to_pX1.c115
-rw-r--r--source/luametatex/source/libraries/softposit/source/i32_to_pX2.c121
-rw-r--r--source/luametatex/source/libraries/softposit/source/i64_to_p16.c90
-rw-r--r--source/luametatex/source/libraries/softposit/source/i64_to_p32.c89
-rw-r--r--source/luametatex/source/libraries/softposit/source/i64_to_p8.c90
-rw-r--r--source/luametatex/source/libraries/softposit/source/i64_to_pX2.c119
-rw-r--r--source/luametatex/source/libraries/softposit/source/include/internals.h178
-rw-r--r--source/luametatex/source/libraries/softposit/source/include/primitives.h52
-rw-r--r--source/luametatex/source/libraries/softposit/source/include/softposit.h653
-rw-r--r--source/luametatex/source/libraries/softposit/source/include/softposit_cpp.h1928
-rw-r--r--source/luametatex/source/libraries/softposit/source/include/softposit_types.h139
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_add.c75
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_div.c186
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_eq.c58
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_le.c58
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_lt.c60
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_mul.c171
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_mulAdd.c63
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_roundToInt.c98
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_sqrt.c143
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_sub.c93
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_i32.c97
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_i64.c98
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_p32.c109
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_p8.c120
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_pX1.c92
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_pX2.c137
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_ui32.c95
-rw-r--r--source/luametatex/source/libraries/softposit/source/p16_to_ui64.c96
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_add.c86
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_div.c201
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_eq.c60
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_le.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_lt.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_mul.c206
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_mulAdd.c56
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_roundToInt.c105
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_sqrt.c137
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_sub.c84
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_i32.c102
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_i64.c107
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_p16.c122
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_p8.c123
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_pX1.c131
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_pX2.c92
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_ui32.c108
-rw-r--r--source/luametatex/source/libraries/softposit/source/p32_to_ui64.c117
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_add.c81
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_div.c167
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_eq.c52
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_le.c52
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_lt.c53
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_mul.c167
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_mulAdd.c56
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_roundToInt.c95
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_sqrt.c63
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_sub.c88
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_i32.c96
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_i64.c97
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_p16.c107
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_p32.c111
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_pX1.c130
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_pX2.c128
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_ui32.c93
-rw-r--r--source/luametatex/source/libraries/softposit/source/p8_to_ui64.c94
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_add.c77
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_div.c210
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_eq.c60
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_le.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_lt.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_mul.c213
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_mulAdd.c56
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_roundToInt.c110
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_sub.c88
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_i32.c105
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_i64.c100
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_p16.c78
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_p32.c113
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_p8.c118
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_pX1.c88
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_pX2.c145
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_ui32.c100
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX1_to_ui64.c97
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_add.c77
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_div.c218
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_eq.c60
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_le.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_lt.c59
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_mul.c228
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_mulAdd.c56
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_roundToInt.c113
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_sqrt.c154
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_sub.c87
-rw-r--r--source/luametatex/source/libraries/softposit/source/pX2_to_pX1.c128
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire16_fdp_add.c185
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire16_fdp_sub.c191
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire32_fdp_add.c202
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire32_fdp_sub.c205
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire8_fdp_add.c137
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire8_fdp_sub.c136
-rw-r--r--source/luametatex/source/libraries/softposit/source/quire_helper.c194
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_addMagsP16.c172
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_addMagsP32.c173
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_addMagsP8.c145
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_addMagsPX1.c190
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_addMagsPX2.c198
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_approxRecipSqrt_1Ks.c52
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_mulAddP16.c275
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_mulAddP32.c296
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_mulAddP8.c243
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_mulAddPX1.c341
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_mulAddPX2.c345
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_subMagsP16.c175
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_subMagsP32.c194
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_subMagsP8.c160
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_subMagsPX1.c202
-rw-r--r--source/luametatex/source/libraries/softposit/source/s_subMagsPX2.c213
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui32_to_p16.c74
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui32_to_p32.c77
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui32_to_p8.c75
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui32_to_pX2.c113
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui64_to_p16.c74
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui64_to_p32.c82
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui64_to_p8.c73
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui64_to_pX1.c124
-rw-r--r--source/luametatex/source/libraries/softposit/source/ui64_to_pX2.c115
148 files changed, 22469 insertions, 0 deletions
diff --git a/source/luametatex/source/libraries/softposit/LICENSE b/source/luametatex/source/libraries/softposit/LICENSE
new file mode 100644
index 000000000..88173ab8d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/LICENSE
@@ -0,0 +1,34 @@
+BSD 3-Clause License
+
+This is part of the SoftPosit Posit Arithmetic Package by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+Many of the C source files design were based on SoftFloat IEEE Floating-Point
+Arithmetic Package, Release 3d, by John R. Hauser.
+
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* 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.
+
+* Neither the name of the copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
diff --git a/source/luametatex/source/libraries/softposit/README.md b/source/luametatex/source/libraries/softposit/README.md
new file mode 100644
index 000000000..b50b271c5
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/README.md
@@ -0,0 +1,551 @@
+# <img src="https://posithub.org/images/NGA_logo.png" width="90" height="50"/> SoftPosit
+
+This version (0.4.1) supports:
+
+ 32-bit with two exponent bit (posit32_t). -> Not exhaustively tested
+
+ 16-bit with one exponent bit (posit16_t).
+
+ 8-bit with zero exponent bit (posit8_t).
+
+ 2-bit to 32-bit with two exponent bits (posit_2_t) -> Not fast : Using 32-bits in the background to store all sizes.
+ Exhaustively tested for X=(2:32) : pX2_rint, pX2_to_pX2, pX2_to_i32/64, pX2_to_ui32/64, pX2_sqrt, ui/i32_to_pX2
+ Exhaustively tested for X=(2:13) : ui64_to_pX2, i64_to_pX2
+ Exhaustively tested for X=(2:20) : pX2_add, pX2_sub, pX2_mul, pX2_div
+ Exhaustively tested for X=(2:21) : pX2_mul
+ Exhaustively tested for X=(2:15) : pX2_mulAdd
+ Exhaustively tested for X=(2:14) : quireX2_fdp_add, quireX2_fdp_sub (using quire32 as the underlying code)
+
+
+
+This code is tested on
+
+* GNU gcc (SUSE Linux) 4.8.5
+* Apple LLVM version 9.1.0 (clang-902.0.39.2)
+* Windows 10 (Mingw-w64)
+
+Please note that the same Makefile in build/Linux-x86_64-GCC is used for all 3 operating systems.
+
+
+ All posit8_t and posit16_t operations are exhaustively tested with exception of p16_mulAdd and q16_fdp_add/sub operations.
+
+ **posit32_t operations are still being tested exhaustively for correctness. It will take weeks to months before these tests complete.**
+
+
+ Versions are offered
+
+ * [Fast C version](#cversion) : The main source code where all other versions are based on.
+ * [User friendly C++ version](#cppversion) : Documentation can be found below.
+ * [User friendly Python version](https://gitlab.com/cerlane/SoftPosit-Python/) : https://gitlab.com/cerlane/SoftPosit-Python/
+ * [Julia](#jversion) : Currently only simple .so support. Documentation can be found below.
+ * [Others](#known)
+
+
+## <a name="cversion"/>Fast C version
+
+
+### Examples
+
+#### A 8-bit example on how to use the code to add:
+
+
+```
+#include "softposit.h"
+
+int main (int argc, char *argv[]){
+
+ posit8_t pA, pB, pZ;
+ pA = castP8(0xF2);
+ pB = castP8(0x23);
+
+ pZ = p8_add(pA, pB);
+
+ //To check answer by converting it to double
+ double dZ = convertP8ToDouble(pZ);
+ printf("dZ: %.15f\n", dZ);
+
+ //To print result in binary
+ uint8_t uiZ = castUI(pZ);
+ printBinary((uint64_t*)&uiZ, 8);
+
+ return 0;
+
+}
+```
+
+
+#### A 16-bit example on how to use the code to multiply:
+
+```
+#include "softposit.h"
+
+int main (int argc, char *argv[]){
+
+ posit16_t pA, pB, pZ;
+ pA = castP16(0x0FF2);
+ pB = castP16(0x2123);
+
+ pZ = p16_mul(pA, pB);
+
+ //To check answer by converting it to double
+ double dZ = convertP16ToDouble(pZ);
+ printf("dZ: %.15f\n", dZ);
+
+ //To print result in binary
+ uint16_t uiZ = castUI(pZ);
+ printBinary((uint64_t*)&uiZ, 16);
+
+ return 0;
+}
+```
+
+#### A 24-bit (es=2) example on how to use the code:
+
+
+```
+#include "softposit.h"
+
+int main (int argc, char *argv[]){
+
+ posit_2_t pA, pB, pZ;
+ pA.v = 0xF2; //this is to set the bits (method 1)
+ pB = castPX2(0x23); //this is to set the bits (method 2)
+
+ pZ = pX2_add(pA, pB, 24);
+
+ //To check answer by converting it to double
+ double dZ = convertPX2ToDouble(pZ);
+ printf("dZ: %.40f\n", dZ);
+
+ //To print result in binary
+ printBinaryPX((uint32_t*)&pZ.v, 24);
+
+ //To print result as double
+ printf("result: %.40f\n", convertPX2ToDouble(pZ));
+
+ return 0;
+
+}
+```
+
+#### For deep learning, please use quire.
+
+
+```
+//Convert double to posit
+posit16_t pA = convertDoubleToP16(1.02783203125 );
+posit16_t pB = convertDoubleToP16(0.987060546875);
+posit16_t pC = convertDoubleToP16(0.4998779296875);
+posit16_t pD = convertDoubleToP16(0.8797607421875);
+
+quire16_t qZ;
+
+//Set quire to 0
+qZ = q16_clr(qZ);
+
+//accumulate products without roundings
+qZ = q16_fdp_add(qZ, pA, pB);
+qZ = q16_fdp_add(qZ, pC, pD);
+
+//Convert back to posit
+posit16_t pZ = q16_to_p16(qZ);
+
+//To check answer
+double dZ = convertP16ToDouble(pZ);
+```
+
+### Build and link
+
+#### Build - softposit.a
+
+
+Please note that only 64-bit systems are supported. For Mac OSX and Linux, the same Makefile is used.
+
+Note that architecture specific optimisation is removed. To get maximum speed, please update OPTIMISATION flag in build/Linux-x86_64-GCC/Makefile.
+
+
+```
+cd SoftPosit/build/Linux-x86_64-GCC
+make -j6 all
+
+```
+
+#### Link - softposit.a
+
+
+If your source code is for example "main.c" and you want to create an executable "main".
+Assume that SoftPosit is installed and installed in the same directory (installing in the same directory is NOT recommended).
+
+```
+gcc -lm -o main \
+ main.c SoftPosit/build/Linux-x86_64-GCC/softposit.a -ISoftPosit/source/include -O2
+
+```
+
+### Features
+
+
+#### Main Posit Functionalities:
+
+
+Add :
+
+ posit16_t p16_add(posit16_t, posit16_t)
+
+ posit8_t p8_add(posit8_t, posit8_t)
+
+Subtract :
+
+ posit16_t p16_sub(posit16_t, posit16_t)
+
+ posit8_t p8_sub(posit8_t, posit8_t)
+
+
+Divide :
+
+ posit16_t p16_div(posit16_t, posit16_t)
+
+ posit8_t p8_div(posit8_t, posit8_t)
+
+Multiply :
+
+ posit16_t p16_mul(posit16_t, posit16_t)
+
+ posit8_t p8_mul(posit8_t, posit8_t)
+
+
+Fused Multiply Add :
+
+ posit16_t p16_mulAdd(posit16_t, posit16_t, posit16_t)
+
+ posit8_t p8_mulAdd(posit8_t, posit8_t, posit8_t)
+
+
+ Note: p16_mulAdd(a, b, c) <=> a*b + c
+
+
+#### Main Quire Functionalities
+
+
+Fused dot product-add :
+
+ quire16_t q16_fdp_add(quire16_t, posit16_t, posit16_t)
+
+ quire8_t q16_fdp_add(quire8_t, posit8_t, posit8_t)
+
+ Note: q8_fdp_add (a, b, c) <=> a + b*c
+
+Fused dot product-subtract :
+
+ quire16_t q16_fdp_sub(quire16_t, posit16_t, posit16_t)
+
+ quire8_t q8_fdp_sub(quire8_t, posit8_t, posit8_t)
+
+Set quire variable to zero :
+
+ quire16_t q16_clr(quire16_t)
+
+ quire8_t q8_clr(quire8_t)
+
+Convert quire to posit :
+
+ posit16_t q16_to_p16(quire16_t)
+
+ posit8_t q8_to_p8(quire8_t)
+
+
+#### Functionalites in Posit Standard
+
+
+Square root :
+
+ posit16_t p16_sqrt(posit16_t)
+
+ posit8_t p8_sqrt(posit8_t)
+
+Round to nearest integer :
+
+ posit16_t p16_roundToInt(posit16_t)
+
+ posit8_t p8_roundToInt(posit8_t)
+
+Check equal :
+
+ bool p16_eq( posit16_t, posit16_t )
+
+ bool p8_eq( posit8_t, posit8_t )
+
+Check less than equal :
+
+ bool p16_le( posit16_t, posit16_t )
+
+ bool p8_le( posit8_t, posit8_t )
+
+Check less than :
+
+ bool p16_lt( posit16_t, posit16_t )
+
+ bool p8_lt( posit8_t, posit8_t )
+
+Convert posit to integer (32 bits) :
+
+ int_fast32_t p16_to_i32( posit16_t )
+
+ int_fast32_t p8_to_i32( posit8_t )
+
+Convert posit to long long integer (64 bits) :
+
+ int_fast64_t p16_to_i64( posit16_t)
+
+ int_fast64_t p8_to_i64( posit8_t)
+
+Convert unsigned integer (32 bits) to posit:
+
+ posit16_t ui32_to_p16( uint32_t a )
+
+ posit8_t ui32_to_p8( uint32_t a )
+
+Convert unsigned long long int (64 bits) to posit:
+
+ posit16_t ui64_to_p16( uint64_t a )
+
+ posit8_t ui64_to_p8( uint64_t a )
+
+Convert integer (32 bits) to posit:
+
+ posit16_t i32_to_p16( int32_t a )
+
+ posit8_t i32_to_p8( uint32_t a )
+
+Convert long integer (64 bits) to posit:
+
+ posit16_t i64_to_p16( int64_t a )
+
+ posit8_t i64_to_p8( uint64_t a )
+
+Convert posit to unsigned integer (32 bits) :
+
+ uint_fast32_t p16_to_ui32( posit16_t )
+
+ uint_fast32_t p8_to_ui32( posit8_t )
+
+Convert posit to unsigned long long integer (64 bits) :
+
+ uint_fast64_t p16_to_ui64( posit16_t)
+
+ uint_fast64_t p8_to_ui64( posit8_t)
+
+Convert posit to integer (32 bits) :
+
+ uint_fast32_t p16_to_i32( posit16_t )
+
+ uint_fast32_t p8_to_i32( posit8_t )
+
+Convert posit to long long integer (64 bits) :
+
+ uint_fast64_t p16_to_i64( posit16_t)
+
+ uint_fast64_t p8_to_i64( posit8_t)
+
+Convert posit to posit of another size :
+
+ posit8_t p16_to_p8( posit16_t )
+
+ posit32_t p16_to_p32( posit16_t )
+
+ posit16_t p8_to_p16( posit8_t )
+
+ posit32_t p8_to_p32( posit8_t )
+
+
+
+#### Helper Functionalites (NOT in Posit Standard)
+
+Convert posit to double (64 bits) :
+
+ double convertP16ToDouble(posit16_t)
+
+ double convertP8ToDouble(posit8_t)
+
+Convert double (64 bits) to posit :
+
+ posit16_t convertDoubleToP16(double)
+
+ posit8_t convertDoubleToP8(double)
+
+Cast binary expressed in unsigned integer to posit :
+
+ posit16_t castP16(uint16_t)
+
+ posit8_t castP8(uint8_t)
+
+Cast posit into binary expressed in unsigned integer
+
+ uint16_t castUI(posit16_t)
+
+ uint8_t castUI(posit8_t)
+
+
+## <a name="cppversion"/>Easy to use C++ version
+
+
+### Build and Link
+
+**Build and link your C++ program to SoftPosit.a (C)**
+
+Please compile your executable with g++ and not gcc.
+
+```
+g++ -std=gnu++11 -o main \
+ ../source/testmain.cpp \
+ ../../SoftPosit/source/../build/Linux-x86_64-GCC/softposit.a \
+ -I../../SoftPosit/source/../build/Linux-x86_64-GCC -O2
+```
+
+### Example
+
+#### Example of testmain.cpp
+
+```
+#include "softposit_cpp.h"
+
+int main(int argc, char *argv[]){
+ posit16 x = 1;
+ posit16 y = 1.5;
+ posit8 x8 = 1;
+ quire16 q;
+ quire8 q8;
+
+ x += p16(1.5)*5.1;
+
+ printf("%.13f sizeof: %d\n", x.toDouble(), sizeof(posit16));
+
+ x = q.qma(4, 1.2).toPosit();
+ printf("%.13f sizeof: %d\n", x.toDouble(), sizeof(quire16));
+
+ x8 = q8.qma(4, 1.2).toPosit();
+ printf("%.13f sizeof: %d\n", x8.toDouble(), sizeof(quire8));
+
+ std::cout << x;
+
+ return 0;
+}
+
+
+```
+
+### Functionalities
+
+#### Main functionalities
+
+* Posit types: posit16, posit8
+* Fused-multiply-add:
+ * posit16 fma(posit16, posit16, posit16)
+ * posit18 fma(posit18, posit18, posit8)
+* Square root:
+ * posit16 sqrt(posit16)
+ * posit8 sqrt(posit8)
+* roundToInt:
+ * posit16 rint(posit16)
+ * posit8 rint(posit8)
+* Supported operators
+ * \+
+ * +=
+ * \-
+ * \-=
+ * &ast;
+ * &ast;=
+ * /
+ * /=
+ * <<
+ * <<=
+ * &#62;&#62;
+ * &#62;&#62;=
+ * &
+ * &=
+ * |
+ * |=
+ * ^
+ * ^=
+ * &&
+ * ||
+ * ++
+ * --
+ * ==
+ * ~
+ * !
+ * !=
+ * &ast;
+ * <
+ * &ast;=
+ * <=
+* Posit to Double:
+ * double (instance of posit).toDouble()
+* Double to Posit:
+ * posit16 p16(double)
+ * posit8 p8(double)
+* Posit to NaR:
+ * posit16 (instance of posit16).toNaR()
+ * posit8 (instance of posit8).toNaR()
+
+#### Quire functionalities (particularly for deep learning)
+
+* Quire types: quire16, quire8 (when declared, quire is initiated to zero)
+* Clear quire to zero:
+ * (instance of quire16).clr()
+* Quire multiply add (fused)
+ * (instance of quire16).fma(quire16)
+ * (instance of quire8).fma(quire8)
+* Quire multiply subtract (fused)
+ * (instance of quire16).fms(quire16)
+ * (instance of quire8).fms(quire8)
+* Convert quire to Posit
+ * posit16 (instance of quire16).toPosit()
+ * posit8 (instance of quire8).toPosit()
+* Check if quire is NaR
+ * bool (instance of quire).isNaR()
+
+## <a name="jversion"/>Julia
+
+* [Julia implementation] (https://github.com/milankl/SoftPosit.jl) on top of SoftPosit
+
+### Install via Julia package manager
+
+```
+> add https://github.com/milankl/SoftPosit.jl
+
+```
+
+Credits to Milan Klöwer.
+
+### Behind the scene
+
+#### Build shared library
+
+```
+cd SoftPosit/build/Linux_x86_64_GCC/
+make -j6 julia
+```
+
+#### Simple Tests
+
+```
+julia> t = ccall((:convertDoubleToP16, "/path/to/SoftPosit/build/Linux-x86_64-GCC/softposit.so"), UInt16, (Float64,),1.0)
+0x4000
+
+julia> t = ccall((:convertDoubleToP16, "/path/to/SoftPosit/build/Linux-x86_64-GCC/softposit.so"), UInt16, (Float64,),-1.0)
+0xc000
+
+```
+
+## <a href="known"/>Known implementations on top of SoftPosit
+
+* [Andrey Zgarbul's Rust implementation](https://crates.io/crates/softposit)
+* [Milan Klöwer's Julia implementation](https://github.com/milankl/SoftPosit.jl)
+* [SpeedGo Computing's TensorFlow](https://github.com/xman/tensorflow/tree/posit)
+* [SpeedGo Computing's Numpy](https://github.com/xman/numpy-posit)
+* [Cerlane Leong's SoftPosit-Python](https://gitlab.com/cerlane/SoftPosit-Python)
+* [David Thien's SoftPosit bindings Racket](https://github.com/DavidThien/softposit-rkt)
+* [Bill Zorn's SoftPosit and SoftFloat Python](https://pypi.org/project/sfpy/)
+
+
diff --git a/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/Makefile b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/Makefile
new file mode 100644
index 000000000..d8e76b157
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/Makefile
@@ -0,0 +1,245 @@
+#============================================================================
+#
+#This C source file is part of the SoftPosit Posit Arithmetic Package
+#by S. H. Leong (Cerlane).
+#
+#Copyright 2017, 2018 A*STAR. All rights reserved.
+#
+#This C source 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.
+#
+#=============================================================================
+
+SOURCE_DIR ?= ../../source
+PYTHON_DIR ?= ../../python
+SPECIALIZE_TYPE ?= 8086-SSE
+COMPILER ?= gcc
+
+SOFTPOSIT_OPTS ?= \
+ -DINLINE_LEVEL=5 #\
+ -DSOFTPOSIT_QUAD -lquadmath
+
+COMPILE_PYTHON = \
+ $(COMPILER) -fPIC -c $(PYTHON_DIR)/softposit_python_wrap.c \
+ -I/usr/include/python \
+ -I$(SOURCE_DIR)/include -I.
+COMPILE_PYTHON3 = \
+ $(COMPILER) -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
+
+
+ifeq ($(OS),Windows_NT)
+ DELETE = del /Q /F
+else
+ DELETE = rm -f
+endif
+
+C_INCLUDES = -I. -I$(SOURCE_DIR)/$(SPECIALIZE_TYPE) -I$(SOURCE_DIR)/include
+OPTIMISATION = -O2 #-march=core-avx2
+COMPILE_C = \
+ $(COMPILER) -c -Werror-implicit-function-declaration -DSOFTPOSIT_FAST_INT64 \
+ $(SOFTPOSIT_OPTS) $(C_INCLUDES) $(OPTIMISATION) \
+ -o $@
+MAKELIB = ar crs $@
+MAKESLIB = $(COMPILER) -shared $^
+
+OBJ = .o
+LIB = .a
+SLIB = .so
+
+.PHONY: all
+all: softposit$(LIB)
+
+quad: SOFTPOSIT_OPTS+= -DSOFTPOSIT_QUAD -lquadmath
+quad: all
+
+python2: SOFTPOSIT_OPTS+= -fPIC
+python2: all
+ $(COMPILE_PYTHON)
+ $(LINK_PYTHON)
+
+python3: SOFTPOSIT_OPTS+= -fPIC
+python3: all
+ $(COMPILE_PYTHON3)
+ $(LINK_PYTHON)
+
+julia: SOFTPOSIT_OPTS+= -fPIC
+julia: softposit$(SLIB)
+
+
+
+OBJS_PRIMITIVES =
+
+OBJS_SPECIALIZE =
+
+OBJS_OTHERS = \
+ s_addMagsP8$(OBJ) \
+ s_subMagsP8$(OBJ) \
+ s_mulAddP8$(OBJ) \
+ p8_add$(OBJ) \
+ p8_sub$(OBJ) \
+ p8_mul$(OBJ) \
+ p8_div$(OBJ) \
+ p8_sqrt$(OBJ) \
+ p8_to_p16$(OBJ) \
+ p8_to_p32$(OBJ) \
+ p8_to_pX2$(OBJ) \
+ p8_to_i32$(OBJ) \
+ p8_to_i64$(OBJ) \
+ p8_to_ui32$(OBJ) \
+ p8_to_ui64$(OBJ) \
+ p8_roundToInt$(OBJ) \
+ p8_mulAdd$(OBJ) \
+ p8_eq$(OBJ) \
+ p8_le$(OBJ) \
+ p8_lt$(OBJ) \
+ quire8_fdp_add$(OBJ) \
+ quire8_fdp_sub$(OBJ) \
+ ui32_to_p8$(OBJ) \
+ ui64_to_p8$(OBJ) \
+ i32_to_p8$(OBJ) \
+ i64_to_p8$(OBJ) \
+ s_addMagsP16$(OBJ) \
+ s_subMagsP16$(OBJ) \
+ s_mulAddP16$(OBJ) \
+ p16_to_ui32$(OBJ) \
+ p16_to_ui64$(OBJ) \
+ p16_to_i32$(OBJ) \
+ p16_to_i64$(OBJ) \
+ p16_to_p8$(OBJ) \
+ p16_to_p32$(OBJ) \
+ p16_to_pX2$(OBJ) \
+ p16_roundToInt$(OBJ) \
+ p16_add$(OBJ) \
+ p16_sub$(OBJ) \
+ p16_mul$(OBJ) \
+ p16_mulAdd$(OBJ) \
+ p16_div$(OBJ) \
+ p16_eq$(OBJ) \
+ p16_le$(OBJ) \
+ p16_lt$(OBJ) \
+ p16_sqrt$(OBJ) \
+ quire16_fdp_add$(OBJ) \
+ quire16_fdp_sub$(OBJ) \
+ quire_helper$(OBJ) \
+ ui32_to_p16$(OBJ) \
+ ui64_to_p16$(OBJ) \
+ i32_to_p16$(OBJ) \
+ i64_to_p16$(OBJ) \
+ s_addMagsP32$(OBJ) \
+ s_subMagsP32$(OBJ) \
+ s_mulAddP32$(OBJ) \
+ p32_to_ui32$(OBJ) \
+ p32_to_ui64$(OBJ) \
+ p32_to_i32$(OBJ) \
+ p32_to_i64$(OBJ) \
+ p32_to_p8$(OBJ) \
+ p32_to_p16$(OBJ) \
+ p32_to_pX2$(OBJ) \
+ p32_roundToInt$(OBJ) \
+ p32_add$(OBJ) \
+ p32_sub$(OBJ) \
+ p32_mul$(OBJ) \
+ p32_mulAdd$(OBJ) \
+ p32_div$(OBJ) \
+ p32_eq$(OBJ) \
+ p32_le$(OBJ) \
+ p32_lt$(OBJ) \
+ p32_sqrt$(OBJ) \
+ quire32_fdp_add$(OBJ) \
+ quire32_fdp_sub$(OBJ) \
+ ui32_to_p32$(OBJ) \
+ ui64_to_p32$(OBJ) \
+ i32_to_p32$(OBJ) \
+ i64_to_p32$(OBJ) \
+ s_approxRecipSqrt_1Ks$(OBJ) \
+ c_convertDecToPosit8$(OBJ) \
+ c_convertPosit8ToDec$(OBJ) \
+ c_convertDecToPosit16$(OBJ) \
+ c_convertPosit16ToDec$(OBJ) \
+ c_convertQuire8ToPosit8$(OBJ) \
+ c_convertQuire16ToPosit16$(OBJ) \
+ c_convertQuire32ToPosit32$(OBJ) \
+ c_convertDecToPosit32$(OBJ) \
+ c_convertPosit32ToDec$(OBJ) \
+ c_int$(OBJ) \
+ s_addMagsPX2$(OBJ) \
+ s_subMagsPX2$(OBJ) \
+ s_mulAddPX2$(OBJ) \
+ pX2_add$(OBJ) \
+ pX2_sub$(OBJ) \
+ pX2_mul$(OBJ) \
+ pX2_div$(OBJ) \
+ pX2_mulAdd$(OBJ) \
+ pX2_roundToInt$(OBJ) \
+ pX2_sqrt$(OBJ) \
+ pX2_eq$(OBJ) \
+ pX2_le$(OBJ) \
+ pX2_lt$(OBJ) \
+ ui32_to_pX2$(OBJ) \
+ ui64_to_pX2$(OBJ) \
+ i32_to_pX2$(OBJ) \
+ i64_to_pX2$(OBJ) \
+ c_convertQuireX2ToPositX2$(OBJ)
+
+
+OBJS_ALL := $(OBJS_PRIMITIVES) $(OBJS_SPECIALIZE) $(OBJS_OTHERS)
+
+$(OBJS_ALL): \
+ platform.h \
+ $(SOURCE_DIR)/include/primitives.h
+
+$(OBJS_SPECIALIZE) $(OBJS_OTHERS): \
+ $(SOURCE_DIR)/include/softposit_types.h $(SOURCE_DIR)/include/internals.h \
+ $(SOURCE_DIR)/$(SPECIALIZE_TYPE)/specialize.h \
+ $(SOURCE_DIR)/include/softposit.h
+
+$(OBJS_PRIMITIVES) $(OBJS_OTHERS): %$(OBJ): $(SOURCE_DIR)/%.c
+ $(COMPILE_C) $(SOURCE_DIR)/$*.c
+
+$(OBJS_SPECIALIZE): %$(OBJ): $(SOURCE_DIR)/$(SPECIALIZE_TYPE)/%.c
+ $(COMPILE_C) $(SOURCE_DIR)/$(SPECIALIZE_TYPE)/$*.c
+
+softposit$(LIB): $(OBJS_ALL)
+ $(MAKELIB) $^
+
+softposit$(SLIB): $(OBJS_ALL)
+ $(MAKESLIB) -o $@
+
+
+.PHONY: clean
+clean:
+ $(DELETE) $(OBJS_ALL) softposit_python_wrap.o softposit$(LIB) softposit$(SLIB)
+
diff --git a/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/platform.h b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/platform.h
new file mode 100644
index 000000000..c6c752ffc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/platform.h
@@ -0,0 +1,48 @@
+
+/*============================================================================
+
+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 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.
+
+=============================================================================*/
+
+/*----------------------------------------------------------------------------
+*----------------------------------------------------------------------------*/
+#define LITTLEENDIAN 1
+
+/*----------------------------------------------------------------------------
+*----------------------------------------------------------------------------*/
+#ifdef __GNUC_STDC_INLINE__
+#define INLINE inline
+#else
+#define INLINE extern inline
+#endif
+
diff --git a/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/softposit_test.a b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/softposit_test.a
new file mode 100644
index 000000000..7bab40bc7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/build/Linux-x86_64-GCC/softposit_test.a
Binary files differ
diff --git a/source/luametatex/source/libraries/softposit/source/8086-SSE/specialize.h b/source/luametatex/source/libraries/softposit/source/8086-SSE/specialize.h
new file mode 100644
index 000000000..fcf057113
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/8086-SSE/specialize.h
@@ -0,0 +1,50 @@
+
+/*============================================================================
+
+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 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 specialize_h
+#define specialize_h 1
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "softposit_types.h"
+
+#endif
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit16.c b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit16.c
new file mode 100644
index 000000000..86a8383bc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit16.c
@@ -0,0 +1,447 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+void checkExtraTwoBitsP16(double f16, double temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f16){
+ *bitsNPlusOne = 1;
+ f16-=temp;
+ }
+ if (f16>0)
+ *bitsMore = 1;
+}
+uint_fast16_t convertFractionP16(double f16, uint_fast8_t fracLength, bool * bitsNPlusOne, bool * bitsMore ){
+
+ uint_fast16_t frac=0;
+
+ if(f16==0) return 0;
+ else if(f16==INFINITY) return 0x8000;
+
+ f16 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkExtraTwoBitsP16(f16, 1.0, bitsNPlusOne, bitsMore);
+ else{
+ double temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f16){
+ f16-=temp;
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+ if (f16==0){
+ //put in the rest of the bits
+ frac <<= (uint_fast8_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkExtraTwoBitsP16(f16, temp, bitsNPlusOne, bitsMore);
+
+ break;
+ }
+ }
+ else{
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkExtraTwoBitsP16(f16, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ }
+ }
+
+ return frac;
+}
+posit16_t convertFloatToP16(float a){
+ return convertDoubleToP16((double) a);
+}
+
+posit16_t convertDoubleToP16(double f16){
+ union ui16_p16 uZ;
+ bool sign, regS;
+ uint_fast16_t reg, frac=0;
+ int_fast8_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f16>=0) ? (sign=0) : (sign=1);
+
+ if (f16 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f16 == INFINITY || f16 == -INFINITY || f16 == NAN){
+ uZ.ui = 0x8000;
+ return uZ.p;
+ }
+ else if (f16 == 1) {
+ uZ.ui = 16384;
+ return uZ.p;
+ }
+ else if (f16 == -1){
+ uZ.ui = 49152;
+ return uZ.p;
+ }
+ else if (f16 >= 268435456){
+ //maxpos
+ uZ.ui = 32767;
+ return uZ.p;
+ }
+ else if (f16 <= -268435456){
+ // -maxpos
+ uZ.ui = 32769;
+ return uZ.p;
+ }
+ else if(f16 <= 3.725290298461914e-9 && !sign){
+ //minpos
+ uZ.ui = 1;
+ return uZ.p;
+ }
+ else if(f16 >= -3.725290298461914e-9 && sign){
+ //-minpos
+ uZ.ui = 65535;
+ return uZ.p;
+ }
+ else if (f16>1 || f16<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f16 = -f16;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (f16 <= 3.725290298461914e-9){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f16>=4){
+ f16 *=0.25;
+ reg++;
+ }
+ if (f16>=2){
+ f16*=0.5;
+ exp++;
+ }
+
+ int fracLength = 13-reg;
+
+ if (fracLength<0){
+ //reg == 14, means rounding bits is exp and just the rest.
+ if (f16>1) bitsMore = 1;
+
+ }
+ else
+ frac = convertFractionP16 (f16, fracLength, &bitNPlusOne, &bitsMore);
+
+
+ if (reg==14 && frac>0) {
+ bitsMore = 1;
+ frac=0;
+ }
+ if (reg>14)
+ (regS) ? (uZ.ui= 32767): (uZ.ui=0x1);
+ else{
+ uint_fast16_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint16_t) (regime) << (14-reg)) + ((uint16_t) (exp)<< (13-reg)) + ((uint16_t)(frac));
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (reg==14 && exp) bitNPlusOne = 1;
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ }
+ }
+ else if (f16 < 1 || f16 > -1 ){
+
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f16 = -f16;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f16<1){
+ f16 *= 4;
+ reg++;
+ }
+ if (f16>=2){
+ f16/=2;
+ exp++;
+ }
+ if (reg==14){
+ bitNPlusOne = exp;
+ if (frac>1) bitsMore = 1;
+ }
+ else{
+ //only possible combination for reg=15 to reach here is 7FFF (maxpos) and FFFF (-minpos)
+ //but since it should be caught on top, so no need to handle
+ int_fast8_t fracLength = 13-reg;
+ frac = convertFractionP16 (f16, fracLength, &bitNPlusOne, &bitsMore);
+ }
+
+ if (reg==14 && frac>0) {
+ bitsMore = 1;
+ frac=0;
+ }
+ if (reg>14)
+ (regS) ? (uZ.ui= 32767): (uZ.ui=0x1);
+ else{
+ uint_fast16_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint16_t) (regime) << (14-reg)) + ((uint16_t) (exp)<< (13-reg)) + ((uint16_t)(frac));
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (reg==14 && exp) bitNPlusOne = 1;
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x8000;
+ }
+ return uZ.p;
+}
+
+
+#ifdef SOFTPOSIT_QUAD
+ void checkQuadExtraTwoBitsP16(__float128 f16, double temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f16){
+ *bitsNPlusOne = 1;
+ f16-=temp;
+ }
+ if (f16>0)
+ *bitsMore = 1;
+ }
+ uint_fast16_t convertQuadFractionP16(__float128 f16, uint_fast8_t fracLength, bool * bitsNPlusOne, bool * bitsMore ){
+
+ uint_fast16_t frac=0;
+
+ if(f16==0) return 0;
+ else if(f16==INFINITY) return 0x8000;
+
+ f16 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkQuadExtraTwoBitsP16(f16, 1.0, bitsNPlusOne, bitsMore);
+ else{
+ __float128 temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f16){
+ f16-=temp;
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+ if (f16==0){
+ //put in the rest of the bits
+ frac <<= (uint_fast8_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkQuadExtraTwoBitsP16(f16, temp, bitsNPlusOne, bitsMore);
+
+ break;
+ }
+ }
+ else{
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkQuadExtraTwoBitsP16(f16, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ }
+ }
+
+ return frac;
+ }
+
+
+ posit16_t convertQuadToP16(__float128 f16){
+ union ui16_p16 uZ;
+ bool sign, regS;
+ uint_fast16_t reg, frac=0;
+ int_fast8_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f16>=0) ? (sign=0) : (sign=1);
+
+ if (f16 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f16 == INFINITY || f16 == -INFINITY || f16 == NAN){
+ uZ.ui = 0x8000;
+ return uZ.p;
+ }
+ else if (f16 == 1) {
+ uZ.ui = 16384;
+ return uZ.p;
+ }
+ else if (f16 == -1){
+ uZ.ui = 49152;
+ return uZ.p;
+ }
+ else if (f16 >= 268435456){
+ //maxpos
+ uZ.ui = 32767;
+ return uZ.p;
+ }
+ else if (f16 <= -268435456){
+ // -maxpos
+ uZ.ui = 32769;
+ return uZ.p;
+ }
+ else if(f16 <= 3.725290298461914e-9 && !sign){
+ //minpos
+ uZ.ui = 1;
+ return uZ.p;
+ }
+ else if(f16 >= -3.725290298461914e-9 && sign){
+ //-minpos
+ uZ.ui = 65535;
+ return uZ.p;
+ }
+ else if (f16>1 || f16<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f16 = -f16;
+ }
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (f16 <= 3.725290298461914e-9){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f16>=4){
+ f16 *=0.25;
+ reg++;
+ }
+ if (f16>=2){
+ f16*=0.5;
+ exp++;
+ }
+
+ int8_t fracLength = 13-reg;
+ if (fracLength<0){
+ //reg == 14, means rounding bits is exp and just the rest.
+ if (f16>1) bitsMore = 1;
+ }
+ else
+ frac = convertQuadFractionP16 (f16, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg==14 && frac>0) {
+ bitsMore = 1;
+ frac=0;
+ }
+
+ if (reg>14)
+ (regS) ? (uZ.ui= 32767): (uZ.ui=0x1);
+ else{
+ uint_fast16_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint16_t) (regime) << (14-reg)) + ((uint16_t) (exp)<< (13-reg)) + ((uint16_t)(frac));
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (reg==14 && exp) bitNPlusOne = 1;
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ }
+ }
+ else if (f16 < 1 || f16 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f16 = -f16;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f16<1){
+ f16 *= 4;
+ reg++;
+ }
+ if (f16>=2){
+ f16/=2;
+ exp++;
+ }
+ if (reg==14){
+ bitNPlusOne = exp;
+ if (frac>1) bitsMore = 1;
+ }
+ else{
+ //only possible combination for reg=15 to reach here is 7FFF (maxpos) and FFFF (-minpos)
+ //but since it should be caught on top, so no need to handle
+ int_fast8_t fracLength = 13-reg;
+ frac = convertQuadFractionP16 (f16, fracLength, &bitNPlusOne, &bitsMore);
+ }
+
+ if (reg==14 && frac>0) {
+ bitsMore = 1;
+ frac=0;
+ }
+ if (reg>14)
+ (regS) ? (uZ.ui= 32767): (uZ.ui=0x1);
+ else{
+ uint_fast16_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint16_t) (regime) << (14-reg)) + ((uint16_t) (exp)<< (13-reg)) + ((uint16_t)(frac));
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (reg==14 && exp) bitNPlusOne = 1;
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x8000;
+ }
+ return uZ.p;
+ }
+#endif
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit32.c b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit32.c
new file mode 100644
index 000000000..9fdf83b7d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit32.c
@@ -0,0 +1,878 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#ifdef SOFTPOSIT_QUAD
+#include <quadmath.h>
+#endif
+
+#include "platform.h"
+#include "internals.h"
+
+#ifdef SOFTPOSIT_QUAD
+
+void checkQuadExtraP32TwoBits(__float128 f32, __float128 temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f32){
+ *bitsNPlusOne = 1;
+ f32-=temp;
+ }
+ if (f32>0)
+ *bitsMore = 1;
+}
+
+uint_fast32_t convertQuadFractionP32(__float128 f32, uint_fast16_t fracLength, bool * bitNPlusOne, bool * bitsMore ){
+
+ uint_fast32_t frac=0;
+
+ if(f32==0) return 0;
+ else if(f32==INFINITY) return 0x80000000;
+
+ f32 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkQuadExtraP32TwoBits(f32, 1.0, bitNPlusOne, bitsMore);
+ else{
+ __float128 temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f32){
+
+ f32-=temp;
+
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+
+ if (f32==0){
+ frac <<= (uint_fast32_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkQuadExtraP32TwoBits(f32, temp, bitNPlusOne, bitsMore);
+ break;
+ }
+ }
+ else{
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkQuadExtraP32TwoBits(f32, temp, bitNPlusOne, bitsMore);
+ break;
+ }
+ }
+
+ }
+ }
+
+ return frac;
+}
+posit32_t convertQuadToP32(__float128 f32){
+ union ui32_p32 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ else if (f32 >= 1.329227995784916e+36){
+ //maxpos
+ uZ.ui = 0x7FFFFFFF;
+ return uZ.p;
+ }
+ else if (f32 <= -1.329227995784916e+36){
+ // -maxpos
+ uZ.ui = 0x80000001;
+ return uZ.p;
+ }
+ else if(f32 <= 7.52316384526264e-37 && !sign){
+ //minpos
+ uZ.ui = 0x1;
+ return uZ.p;
+ }
+ else if(f32 >= -7.52316384526264e-37 && sign){
+ //-minpos
+ uZ.ui = 0xFFFFFFFF;
+ return uZ.p;
+ }
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (f32 <= 7.52316384526264e-37){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=16){
+ f32 *=0.0625; // f32/=16;
+ reg++;
+ }
+
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = 28-reg;
+ if (fracLength<0){
+ //remove hidden bit
+
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==29){
+ bitNPlusOne = exp&0x1;
+ exp>>=1; //taken care of by the pack algo
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertQuadFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg>30 ){
+ (regS) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ //rounding off fraction bits
+ else{
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ if (reg<=28) exp<<= (28-reg);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac));
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ /*if(sign)
+ uZ.ui = -uZ.ui;*/
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+
+
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ while (f32<1){
+ f32 *= 16;
+ reg++;
+ }
+
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ //only possible combination for reg=15 to reach here is 7FFF (maxpos) and FFFF (-minpos)
+ //but since it should be caught on top, so no need to handle
+ int fracLength = 28-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==29){
+ bitNPlusOne = exp&0x1;
+ exp>>=1; //taken care of by the pack algo
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertQuadFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg>30 ){
+ (regS) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ //rounding off fraction bits
+ else{
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ if (reg<=28) exp<<= (28-reg);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac));
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+
+}
+
+posit_2_t convertQuadToPX2(__float128 f32, int x){
+
+ union ui32_pX2 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ /*else if (f32 >= 1.329227995784916e+36){
+ //maxpos
+ uZ.ui = 0x7FFFFFFF;
+ return uZ.p;
+ }
+ else if (f32 <= -1.329227995784916e+36){
+ // -maxpos
+ uZ.ui = 0x80000001;
+ return uZ.p;
+ }
+ else if(f32 <= 7.52316384526264e-37 && !sign){
+ //minpos
+ uZ.ui = 0x1;
+ return uZ.p;
+ }
+ else if(f32 >= -7.52316384526264e-37 && sign){
+ //-minpos
+ uZ.ui = 0xFFFFFFFF;
+ return uZ.p;
+ }*/
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (x==32 && f32 <= 7.52316384526264e-37){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=16){
+ f32 *=0.0625; // f32/=16;
+ reg++;
+ }
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = x-4-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==x-3){
+ bitNPlusOne = exp&0x1;
+ //exp>>=1; //taken care of by the pack algo
+ exp&=0x2;
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertQuadFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ if (reg<=28) exp<<= (28-reg);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f32<1){
+ f32 *= 16;
+ reg++;
+ }
+
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+
+ int fracLength = x-4-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==x-3){
+ bitNPlusOne = exp&0x1;
+ //exp>>=1; //taken care of by the pack algo
+ exp&=0x2;
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertQuadFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+
+ if (reg<=28) exp<<= (28-reg);
+
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+
+ if (bitNPlusOne){
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+
+
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+}
+
+#endif
+
+void checkExtraP32TwoBits(double f32, double temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f32){
+ *bitsNPlusOne = 1;
+ f32-=temp;
+ }
+ if (f32>0)
+ *bitsMore = 1;
+}
+uint_fast32_t convertFractionP32(double f32, uint_fast16_t fracLength, bool * bitsNPlusOne, bool * bitsMore ){
+
+ uint_fast32_t frac=0;
+
+ if(f32==0) return 0;
+ else if(f32==INFINITY) return 0x80000000;
+
+ f32 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkExtraP32TwoBits(f32, 1.0, bitsNPlusOne, bitsMore);
+ else{
+ double temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f32){
+ f32-=temp;
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+ if (f32==0){
+ frac <<= (uint_fast16_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkExtraP32TwoBits(f32, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ else{
+
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkExtraP32TwoBits(f32, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ }
+ }
+
+ return frac;
+}
+
+posit32_t convertDoubleToP32(double f32){
+
+ union ui32_p32 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ else if (f32 >= 1.329227995784916e+36){
+ //maxpos
+ uZ.ui = 0x7FFFFFFF;
+ return uZ.p;
+ }
+ else if (f32 <= -1.329227995784916e+36){
+ // -maxpos
+ uZ.ui = 0x80000001;
+ return uZ.p;
+ }
+ else if(f32 <= 7.52316384526264e-37 && !sign){
+ //minpos
+ uZ.ui = 0x1;
+ return uZ.p;
+ }
+ else if(f32 >= -7.52316384526264e-37 && sign){
+ //-minpos
+ uZ.ui = 0xFFFFFFFF;
+ return uZ.p;
+ }
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (f32 <= 7.52316384526264e-37){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=16){
+ f32 *=0.0625; // f32/=16;
+ reg++;
+ }
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int8_t fracLength = 28-reg;
+
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==29){
+ bitNPlusOne = exp&0x1;
+ exp>>=1; //taken care of by the pack algo
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+
+ if (reg>30 ){
+ (regS) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ if (reg<=28) exp<<= (28-reg);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac));
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f32<1){
+ f32 *= 16;
+ reg++;
+ }
+
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+
+ //only possible combination for reg=15 to reach here is 7FFF (maxpos) and FFFF (-minpos)
+ //but since it should be caught on top, so no need to handle
+ int_fast8_t fracLength = 28-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==29){
+ bitNPlusOne = exp&0x1;
+ exp>>=1; //taken care of by the pack algo
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+
+ if (reg>30 ){
+ (regS) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ if (reg<=28) exp<<= (28-reg);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac));
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+}
+
+
+posit32_t convertFloatToP32(float a){
+ return convertDoubleToP32((double) a );
+}
+
+
+posit_2_t convertDoubleToPX2(double f32, int x){
+
+ union ui32_pX2 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ /*else if (f32 >= 1.329227995784916e+36){
+ //maxpos
+ uZ.ui = 0x7FFFFFFF;
+ return uZ.p;
+ }
+ else if (f32 <= -1.329227995784916e+36){
+ // -maxpos
+ uZ.ui = 0x80000001;
+ return uZ.p;
+ }
+ else if(f32 <= 7.52316384526264e-37 && !sign){
+ //minpos
+ uZ.ui = 0x1;
+ return uZ.p;
+ }
+ else if(f32 >= -7.52316384526264e-37 && sign){
+ //-minpos
+ uZ.ui = 0xFFFFFFFF;
+ return uZ.p;
+ }*/
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (x==32 && f32 <= 7.52316384526264e-37){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=16){
+ f32 *=0.0625; // f32/=16;
+ reg++;
+ }
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = x-4-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==x-3){
+ bitNPlusOne = exp&0x1;
+ //exp>>=1; //taken care of by the pack algo
+ exp&=0x2;
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+
+ if (x==32 && reg==29) exp>>=1;
+ else if (reg<=28) exp<<= (28-reg);
+
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f32<1){
+ f32 *= 16;
+ reg++;
+ }
+
+ while (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = x-4-reg;
+ if (fracLength<0){
+ //in both cases, reg=29 and 30, e is n+1 bit and frac are sticky bits
+ if(reg==x-3){
+ bitNPlusOne = exp&0x1;
+ //exp>>=1; //taken care of by the pack algo
+ exp&=0x2;
+ }
+ else{//reg=30
+ bitNPlusOne=exp>>1;
+ bitsMore=exp&0x1;
+ exp=0;
+ }
+ if (f32!=1){//because of hidden bit
+ bitsMore =1;
+ frac=0;
+ }
+ }
+ else
+ frac = convertFractionP32 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+
+ if (x==32 && reg==29) exp>>=1;
+ else if (reg<=28) exp<<= (28-reg);
+
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+
+ if (bitNPlusOne){
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+}
+
+
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit8.c b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit8.c
new file mode 100644
index 000000000..16d5f527f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertDecToPosit8.c
@@ -0,0 +1,211 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+void checkExtraTwoBitsP8(double f8, double temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f8){
+ *bitsNPlusOne = 1;
+ f8-=temp;
+ }
+ if (f8>0)
+ *bitsMore = 1;
+}
+uint_fast16_t convertFractionP8(double f8, uint_fast8_t fracLength, bool * bitsNPlusOne, bool * bitsMore ){
+
+ uint_fast8_t frac=0;
+
+ if(f8==0) return 0;
+ else if(f8==INFINITY) return 0x80;
+
+ f8 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkExtraTwoBitsP8(f8, 1.0, bitsNPlusOne, bitsMore);
+ else{
+ double temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f8){
+ f8-=temp;
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+ if (f8==0){
+ //put in the rest of the bits
+ frac <<= (uint_fast8_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkExtraTwoBitsP8(f8, temp, bitsNPlusOne, bitsMore);
+
+ break;
+ }
+ }
+ else{
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkExtraTwoBitsP8(f8, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ }
+ }
+
+
+ //printf("convertfloat: frac:%d f16: %.26f bitsNPlusOne: %d, bitsMore: %d\n", frac, f16, bitsNPlusOne, bitsMore);
+
+ return frac;
+}
+posit8_t convertDoubleToP8(double f8){
+ union ui8_p8 uZ;
+ bool sign;
+ uint_fast8_t reg, frac=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f8>=0) ? (sign=0) : (sign=1);
+ // sign: 1 bit, frac: 8 bits, mantisa: 23 bits
+ //sign = a.parts.sign;
+ //frac = a.parts.fraction;
+ //exp = a.parts.exponent;
+
+ if (f8 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f8 == INFINITY || f8 == -INFINITY || f8 == NAN){
+ uZ.ui = 0x80;
+ return uZ.p;
+ }
+ else if (f8 == 1) {
+ uZ.ui = 0x40;
+ return uZ.p;
+ }
+ else if (f8 == -1){
+ uZ.ui = 0xC0;
+ return uZ.p;
+ }
+ else if (f8 >= 64){
+ //maxpos
+ uZ.ui = 0x7F;
+ return uZ.p;
+ }
+ else if (f8 <= -64){
+ // -maxpos
+ uZ.ui = 0x81;
+ return uZ.p;
+ }
+ else if(f8 <= 0.015625 && !sign){
+ //minpos
+ uZ.ui = 0x1;
+ return uZ.p;
+ }
+ else if(f8 >= -0.015625 && sign){
+ //-minpos
+ uZ.ui = 0xFF;
+ return uZ.p;
+ }
+ else if (f8>1 || f8<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f8 = -f8;
+ }
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (f8 <= 0.015625){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f8>=2){
+ f8 *=0.5;
+ reg++;
+ }
+
+ //rounding off regime bits
+ if (reg>6 )
+ uZ.ui= 0x7F;
+ else{
+ int8_t fracLength = 6-reg;
+ frac = convertFractionP8 (f8, fracLength, &bitNPlusOne, &bitsMore);
+ uint_fast8_t regime = 0x7F - (0x7F>>reg);
+ uZ.ui = packToP8UI(regime, frac);
+ if (bitNPlusOne)
+ uZ.ui += ((uZ.ui&1) | bitsMore );
+ }
+ if(sign) uZ.ui = -uZ.ui & 0xFF;
+ }
+ }
+ else if (f8 < 1 || f8 > -1 ){
+
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f8 = -f8;
+ }
+ reg = 0;
+
+ //regime
+ //printf("here we go\n");
+ while (f8<1){
+ f8 *= 2;
+ reg++;
+ }
+ //rounding off regime bits
+ if (reg>6 )
+ uZ.ui=0x1;
+ else{
+ int_fast8_t fracLength = 6-reg;
+ frac = convertFractionP8 (f8, fracLength, &bitNPlusOne, &bitsMore);
+ uint_fast8_t regime = 0x40>>reg;
+ uZ.ui = packToP8UI(regime, frac);
+ if (bitNPlusOne)
+ uZ.ui += ((uZ.ui&1) | bitsMore );
+ }
+ if(sign) uZ.ui = -uZ.ui & 0xFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80;
+ }
+ return uZ.p;
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertDecToPositX1.c b/source/luametatex/source/libraries/softposit/source/c_convertDecToPositX1.c
new file mode 100644
index 000000000..62550f141
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertDecToPositX1.c
@@ -0,0 +1,461 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#ifdef SOFTPOSIT_QUAD
+#include <quadmath.h>
+#endif
+
+#include "platform.h"
+#include "internals.h"
+
+#ifdef SOFTPOSIT_QUAD
+
+void checkQuadExtraPX1TwoBits(__float128 f32, __float128 temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f32){
+ *bitsNPlusOne = 1;
+ f32-=temp;
+ }
+ if (f32>0)
+ *bitsMore = 1;
+}
+
+uint_fast32_t convertQuadFractionPX1(__float128 f32, uint_fast16_t fracLength, bool * bitNPlusOne, bool * bitsMore ){
+
+ uint_fast32_t frac=0;
+
+ if(f32==0) return 0;
+ else if(f32==INFINITY) return 0x80000000;
+
+ f32 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkQuadExtraPX1TwoBits(f32, 1.0, bitNPlusOne, bitsMore);
+ else{
+ __float128 temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f32){
+
+ f32-=temp;
+
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+
+ if (f32==0){
+ frac <<= (uint_fast32_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkQuadExtraPX1TwoBits(f32, temp, bitNPlusOne, bitsMore);
+ break;
+ }
+ }
+ else{
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkQuadExtraPX1TwoBits(f32, temp, bitNPlusOne, bitsMore);
+ break;
+ }
+ }
+
+ }
+ }
+
+ return frac;
+}
+
+posit_1_t convertQuadToPX1(__float128 f32, int x){
+
+ union ui32_pX1 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (x==32 && f32 <= 8.673617379884035e-19){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=4){
+ f32 *=0.25; // f32/=4;
+ reg++;
+ }
+ if (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = x-3-reg;
+ if (fracLength<0){
+ if (reg==x-2){
+ bitNPlusOne=exp;
+ exp=0;
+ }
+ if(f32>1) bitsMore=1;
+ }
+ else
+ frac = convertQuadFractionPX1 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg==30 && frac>0){
+ bitsMore = 1;
+ frac = 0;
+ }
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) (exp)<< (29-reg)) + ((uint32_t)(frac<<(32-x)));
+ //uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f32<1){
+ f32 *= 4;
+ reg++;
+ }
+
+ if (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+
+ int fracLength = x-3-reg;
+ if (fracLength<0){
+ if (reg==x-2){
+ bitNPlusOne=exp;
+ exp=0;
+ }
+ if(f32>1) bitsMore=1;
+ }
+ else
+ frac = convertQuadFractionPX1 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg==30 && frac>0){
+ bitsMore = 1;
+ frac = 0;
+ }
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) (exp)<< (29-reg)) + ((uint32_t)(frac<<(32-x)));
+ //uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+}
+
+#endif
+
+void checkExtraPX1TwoBits(double f32, double temp, bool * bitsNPlusOne, bool * bitsMore ){
+ temp /= 2;
+ if (temp<=f32){
+ *bitsNPlusOne = 1;
+ f32-=temp;
+ }
+ if (f32>0)
+ *bitsMore = 1;
+}
+uint_fast32_t convertFractionPX1(double f32, uint_fast16_t fracLength, bool * bitsNPlusOne, bool * bitsMore ){
+
+ uint_fast32_t frac=0;
+
+ if(f32==0) return 0;
+ else if(f32==INFINITY) return 0x80000000;
+
+ f32 -= 1; //remove hidden bit
+ if (fracLength==0)
+ checkExtraPX1TwoBits(f32, 1.0, bitsNPlusOne, bitsMore);
+ else{
+ double temp = 1;
+ while (true){
+ temp /= 2;
+ if (temp<=f32){
+ f32-=temp;
+ fracLength--;
+ frac = (frac<<1) + 1; //shift in one
+ if (f32==0){
+ frac <<= (uint_fast16_t)fracLength;
+ break;
+ }
+
+ if (fracLength == 0){
+ checkExtraPX1TwoBits(f32, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ else{
+
+ frac <<= 1; //shift in a zero
+ fracLength--;
+ if (fracLength == 0){
+ checkExtraPX1TwoBits(f32, temp, bitsNPlusOne, bitsMore);
+ break;
+ }
+ }
+ }
+ }
+
+ return frac;
+}
+
+
+posit_1_t convertDoubleToPX1(double f32, int x){
+
+ union ui32_pX1 uZ;
+ bool sign, regS;
+ uint_fast32_t reg, frac=0;
+ int_fast32_t exp=0;
+ bool bitNPlusOne=0, bitsMore=0;
+
+ (f32>=0) ? (sign=0) : (sign=1);
+
+ if (f32 == 0 ){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else if(f32 == INFINITY || f32 == -INFINITY || f32 == NAN){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (f32 == 1) {
+ uZ.ui = 0x40000000;
+ return uZ.p;
+ }
+ else if (f32 == -1){
+ uZ.ui = 0xC0000000;
+ return uZ.p;
+ }
+ else if (f32>1 || f32<-1){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 1;
+ reg = 1; //because k = m-1; so need to add back 1
+ // minpos
+ if (x==32 && f32 <= 8.673617379884035e-19){
+ uZ.ui = 1;
+ }
+ else{
+ //regime
+ while (f32>=4){
+ f32 *=0.25; // f32/=4;
+ reg++;
+ }
+ if (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+//printf("reg: %d, exp: %d f32: %.26lf\n", reg, exp, f32);
+ int fracLength = x-3-reg;
+ if (fracLength<0){
+ if (reg==x-2){
+ bitNPlusOne=exp;
+ exp=0;
+ }
+ if(f32>1) bitsMore=1;
+ }
+ else
+ frac = convertFractionPX1(f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg==30 && frac>0){
+ bitsMore = 1;
+ frac = 0;
+ }
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+//printf("reg: %d, exp: %d bitNPlusOne: %d bitsMore: %d\n", reg, exp, bitNPlusOne, bitsMore);
+//printBinary(&regime, 32);
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) (exp)<< (29-reg)) + ((uint32_t)(frac<<(32-x)));
+ //uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+//printBinary(&uZ.ui, 32);
+//uint32_t tt = (uZ.ui>>(32-x));
+//printBinary(&tt, 32);
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ }
+ else if (f32 < 1 || f32 > -1 ){
+ if (sign){
+ //Make negative numbers positive for easier computation
+ f32 = -f32;
+ }
+ regS = 0;
+ reg = 0;
+
+ //regime
+ while (f32<1){
+ f32 *= 4;
+ reg++;
+ }
+
+ if (f32>=2){
+ f32*=0.5;
+ exp++;
+ }
+
+ int fracLength = x-3-reg;
+ if (fracLength<0){
+ if (reg==x-2){
+ bitNPlusOne=exp;
+ exp=0;
+ }
+
+ if(f32>1) bitsMore=1;
+ }
+ else
+ frac = convertFractionPX1 (f32, fracLength, &bitNPlusOne, &bitsMore);
+
+ if (reg==30 && frac>0){
+ bitsMore = 1;
+ frac = 0;
+ }
+
+ if (reg>(x-2) ){
+ uZ.ui=(regS) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ //rounding off fraction bits
+ else{
+ uint_fast32_t regime = 1;
+ if (regS) regime = ( (1<<reg)-1 ) <<1;
+ uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) (exp)<< (29-reg)) + ((uint32_t)(frac<<(32-x)));
+ //uZ.ui = ((uint32_t) (regime) << (30-reg)) + ((uint32_t) exp ) + ((uint32_t)(frac<<(32-x)));
+ //minpos
+ if (uZ.ui==0 && frac>0){
+ uZ.ui = 0x1 << (32-x);
+ }
+ if (bitNPlusOne)
+ uZ.ui += ( ((uZ.ui>>(32-x)) & 0x1) | bitsMore ) << (32-x);
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ }
+ else {
+ //NaR - for NaN, INF and all other combinations
+ uZ.ui = 0x80000000;
+ }
+ return uZ.p;
+}
+
+
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertPosit16ToDec.c b/source/luametatex/source/libraries/softposit/source/c_convertPosit16ToDec.c
new file mode 100644
index 000000000..9befa42ed
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertPosit16ToDec.c
@@ -0,0 +1,172 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+double convertP16ToDouble(posit16_t a){
+ union ui16_p16 uZ;
+ //posit16 p16;
+ double d16;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ return 0;
+ }
+ else if(uZ.ui==0x7FFF){ //maxpos -> 32767
+ return 268435456;
+ }
+ else if (uZ.ui==0x8001){ //-maxpos -> 32769
+ return -268435456;
+ }
+ else if (uZ.ui == 0x8000){ //NaR -> 32768
+ return NAN;
+ }
+
+ bool regS, sign;
+ uint_fast16_t reg, shift=2, frac;
+ int_fast16_t k=0;
+ int_fast8_t exp;
+ double fraction_max;
+
+ sign = signP16UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFF;
+ regS = signregP16UI( uZ.ui );
+
+ uint_fast16_t tmp = (uZ.ui<<2) & 0xFFFF;
+ if (regS){
+ while (tmp>>15){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>15)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ reg =-k;
+ }
+ exp = tmp>>14;
+ frac = (tmp & 0x3FFF) >> shift;
+
+
+ fraction_max = pow(2, 13-reg) ;
+ d16 = (double)( pow(4, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
+
+ if (sign)
+ d16 = -d16;
+
+ return d16;
+}
+
+#ifdef SOFTPOSIT_QUAD
+ __float128 convertP16ToQuadDec(posit16_t a){
+
+ union ui16_p16 uZ;
+ __float128 p16;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ p16 = 0;
+ return p16;
+ }
+ else if(uZ.ui==0x7FFF){ //maxpos -> 32767
+ p16 = 268435456;
+ return p16;
+ }
+ else if (uZ.ui==0x8001){ //-maxpos -> 32769
+ p16 = -268435456;
+ return p16;
+ }
+ else if (uZ.ui == 0x8000){ //NaR -> 32768
+ p16 = INFINITY;
+ return p16;
+ }
+
+ bool regS, sign;
+ uint_fast16_t reg, shift=2, frac;
+ int_fast16_t k=0;
+ int_fast8_t exp;
+ __float128 fraction_max;
+
+ sign = signP16UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFF;
+ regS = signregP16UI( uZ.ui );
+
+ uint_fast16_t tmp = (uZ.ui<<2) & 0xFFFF;
+ if (regS){
+ while (tmp>>15){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>15)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ reg =-k;
+ }
+ exp = tmp>>14;
+ frac = (tmp & 0x3FFF) >> shift;
+
+
+ fraction_max = pow(2, 13-reg) ;
+ p16 = (__float128)( pow(4, k)* pow(2, exp) * (1+((__float128)frac/fraction_max)) );
+
+ if (sign)
+ p16 = -p16;
+
+ return p16;
+
+ }
+#endif
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertPosit32ToDec.c b/source/luametatex/source/libraries/softposit/source/c_convertPosit32ToDec.c
new file mode 100644
index 000000000..f04b23d21
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertPosit32ToDec.c
@@ -0,0 +1,360 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#ifdef SOFTPOSIT_QUAD
+#include <quadmath.h>
+#endif
+
+#include "platform.h"
+#include "internals.h"
+
+
+
+#ifdef SOFTPOSIT_QUAD
+__float128 convertP32ToQuad(posit32_t a){
+
+ union ui32_p32 uZ;
+ __float128 p32;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ p32 = 0;
+ return p32;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ p32 = 1.329227995784916e+36;
+ return p32;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ p32 = -1.329227995784916e+36;
+ return p32;
+ }
+ else if (uZ.ui == 0x80000000){
+ p32 = NAN;
+ return p32;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ __float128 fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>29;
+ frac = (tmp & 0x1FFFFFFF) >> shift;
+
+ (reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
+
+ p32 = (__float128)( pow(16, k)* pow(2, exp) * (1+((__float128)frac/fraction_max)) );
+
+ if (sign)
+ p32 = -p32;
+
+ return p32;
+
+}
+
+__float128 convertPX2ToQuad(posit_2_t a){
+
+ union ui32_pX2 uZ;
+ __float128 p32;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ p32 = 0;
+ return p32;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ p32 = 1.329227995784916e+36;
+ return p32;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ p32 = -1.329227995784916e+36;
+ return p32;
+ }
+ else if (uZ.ui == 0x80000000){
+ p32 = INFINITY;
+ return p32;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ __float128 fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>29;
+ frac = (tmp & 0x1FFFFFFF) >> shift;
+
+ (reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
+
+ p32 = (__float128)( pow(16, k)* pow(2, exp) * (1+((__float128)frac/fraction_max)) );
+
+ if (sign)
+ p32 = -p32;
+
+ return p32;
+
+}
+
+
+#endif
+
+
+double convertP32ToDouble(posit32_t pA){
+ union ui32_p32 uA;
+ union ui64_double uZ;
+ uint_fast32_t uiA, tmp=0;
+ uint_fast64_t expA=0, uiZ, fracA=0;
+ bool signA=0, regSA;
+ int_fast32_t scale, kA=0;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uA.ui == 0)
+ return 0;
+ else if(uA.ui == 0x80000000)
+ return NAN;
+
+ else{
+ signA = signP32UI( uiA );
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ regSA = signregP32UI(uiA);
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+
+ fracA = (((uint64_t)tmp<<3) & 0xFFFFFFFF)<<20;
+
+ expA = (((kA<<2)+expA) + 1023) << 52;
+ uiZ = expA + fracA + (((uint64_t)signA&0x1)<<63);
+
+ uZ.ui = uiZ;
+ return uZ.d;
+ }
+}
+/*double convertP32ToDoubleOld(posit32_t pA){
+ union ui32_p32 uZ;
+ double d32;
+ uZ.p = pA;
+
+ if (uZ.ui==0){
+ return 0;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ return 1.329227995784916e+36;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ return -1.329227995784916e+36;
+ }
+ else if (uZ.ui == 0x80000000){
+ return INFINITY;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ double fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>29;
+
+ frac = (tmp & 0x1FFFFFFF) >> shift;
+
+ (reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
+
+
+ d32 = (double)( pow(16, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
+ if (sign)
+ d32 = -d32;
+
+ return d32;
+
+}*/
+
+
+double convertPX2ToDouble(posit_2_t a){
+ union ui32_pX2 uZ;
+ double d32;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ return 0;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ return 1.329227995784916e+36;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ return -1.329227995784916e+36;
+ }
+ else if (uZ.ui == 0x80000000){
+ return INFINITY;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ double fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>29;
+
+ frac = (tmp & 0x1FFFFFFF) >> shift;
+
+ (reg>28) ? (fraction_max=1) : (fraction_max = pow(2, 28-reg) ) ;
+
+
+ d32 = (double)( pow(16, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
+ if (sign)
+ d32 = -d32;
+
+ return d32;
+
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertPosit8ToDec.c b/source/luametatex/source/libraries/softposit/source/c_convertPosit8ToDec.c
new file mode 100644
index 000000000..f8af0b97f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertPosit8ToDec.c
@@ -0,0 +1,98 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+double convertP8ToDouble(posit8_t a){
+ union ui8_p8 uZ;
+ double d8;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ return 0;
+ }
+ else if(uZ.ui==0x7F){ //maxpos
+ return 64;
+ }
+ else if (uZ.ui==0x81){ //-maxpos
+ return -64;
+ }
+ else if (uZ.ui == 0x80){ //NaR
+ return NAN;
+ }
+
+ bool regS, sign;
+ uint_fast8_t reg, shift=2, frac;
+ int_fast8_t k=0;
+ double fraction_max;
+
+ sign = signP8UI( uZ.ui );
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+ regS = signregP8UI( uZ.ui );
+
+ uint_fast8_t tmp = (uZ.ui<<2) & 0xFF;
+ if (regS){
+ while (tmp>>7){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>7)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ reg =-k;
+ }
+ frac = (tmp & 0x7F) >> shift;
+
+
+ fraction_max = pow(2, 6-reg) ;
+ d8 = (double)( pow(2, k) * (1+((double)frac/fraction_max)) );
+
+ if (sign)
+ d8 = -d8;
+
+ return d8;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertPositX1ToDec.c b/source/luametatex/source/libraries/softposit/source/c_convertPositX1ToDec.c
new file mode 100644
index 000000000..cf757a321
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertPositX1ToDec.c
@@ -0,0 +1,184 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#ifdef SOFTPOSIT_QUAD
+#include <quadmath.h>
+#endif
+
+#include "platform.h"
+#include "internals.h"
+
+
+
+#ifdef SOFTPOSIT_QUAD
+
+__float128 convertPX1ToQuad(posit_1_t a){
+
+ union ui32_pX1 uZ;
+ __float128 p32;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ p32 = 0;
+ return p32;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ p32 = 1152921504606847000;
+ return p32;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ p32 = -1152921504606847000;
+ return p32;
+ }
+ else if (uZ.ui == 0x80000000){
+ p32 = NAN;
+ return p32;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ __float128 fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>30;
+ frac = (tmp & 0x1FFFFFFF) >> shift;
+
+ (reg>29) ? (fraction_max=1) : (fraction_max = pow(2, 29-reg) ) ;
+
+ p32 = (__float128)( pow(16, k)* pow(2, exp) * (1+((__float128)frac/fraction_max)) );
+
+ if (sign)
+ p32 = -p32;
+
+ return p32;
+
+}
+
+
+#endif
+
+
+
+
+double convertPX1ToDouble(posit_1_t a){
+ union ui32_pX1 uZ;
+ double d32;
+ uZ.p = a;
+
+ if (uZ.ui==0){
+ return 0;
+ }
+ else if(uZ.ui==0x7FFFFFFF){ //maxpos
+ return 1152921504606847000;
+ }
+ else if (uZ.ui==0x80000001){ //-maxpos
+ return -1152921504606847000;
+ }
+ else if (uZ.ui == 0x80000000){
+ return NAN;
+ }
+
+ bool regS, sign;
+ uint_fast32_t reg, shift=2, frac, tmp;
+ int_fast32_t k=0;
+ int_fast8_t exp;
+ double fraction_max;
+
+ sign = signP32UI( uZ.ui );
+ if (sign)
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ regS = signregP32UI( uZ.ui );
+
+ tmp = (uZ.ui<<2)&0xFFFFFFFF;
+ if (regS){
+ while (tmp>>31){
+ k++;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ reg = k+1;
+ }
+ else{
+ k=-1;
+ while (!(tmp>>31)){
+ k--;
+ shift++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ reg =-k;
+ }
+ exp = tmp>>30;
+
+ frac = (tmp & 0x3FFFFFFF) >> shift;
+
+ (reg>29) ? (fraction_max=1) : (fraction_max = pow(2, 29-reg) ) ;
+
+ d32 = (double)( pow(4, k)* pow(2, exp) * (1+((double)frac/fraction_max)) );
+ if (sign)
+ d32 = -d32;
+
+ return d32;
+
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertQuire16ToPosit16.c b/source/luametatex/source/libraries/softposit/source/c_convertQuire16ToPosit16.c
new file mode 100644
index 000000000..80deccc4c
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertQuire16ToPosit16.c
@@ -0,0 +1,159 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+//TODO DEPRECATED
+posit16_t convertQ16ToP16(quire16_t qA){
+ return q16_to_p16(qA);
+}
+
+
+posit16_t q16_to_p16(quire16_t qA){
+ union ui128_q16 uZ;
+ union ui16_p16 uA;
+ uint_fast16_t regA, fracA = 0, shift=0, regime;
+ uint_fast64_t frac64A;
+ bool sign, regSA=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t expA = 0;
+
+
+ if (isQ16Zero(qA)){
+ uA.ui=0;
+ return uA.p;
+ }
+ //handle NaR
+ else if (isNaRQ16(qA)){
+ uA.ui=0x8000;
+ return uA.p;
+ }
+
+ uZ.q = qA;
+
+ sign = uZ.ui[0]>>63;
+
+ if(sign){
+ //probably need to do two's complement here before the rest.
+ if (uZ.ui[1]==0){
+ uZ.ui[0] = -uZ.ui[0];
+ }
+ else{
+ uZ.ui[1] = -uZ.ui[1];
+ uZ.ui[0] = ~(uZ.ui[0]);
+ }
+ }
+
+
+ int noLZ =0;
+
+ if (uZ.ui[0] == 0){
+ noLZ+=64;
+ uint_fast64_t tmp = uZ.ui[1];
+
+ while(!(tmp>>63)){
+ noLZ++;
+ tmp<<=1;
+ }
+ frac64A = tmp;
+ }
+ else{
+ uint_fast64_t tmp = uZ.ui[0];
+ int noLZtmp = 0;
+
+ while(!(tmp>>63)){
+ noLZtmp++;
+ tmp<<=1;
+ }
+ noLZ+=noLZtmp;
+ frac64A = tmp;
+ frac64A+= ( uZ.ui[1]>>(64-noLZtmp) );
+ if (uZ.ui[1]<<noLZtmp)bitsMore = 1;
+ }
+
+ //default dot is between bit 71 and 72, extreme left bit is bit 0. Last right bit is bit 127.
+ //Equations derived from quire16_mult last_pos = 71 - (kA<<1) - expA and first_pos = last_pos - frac_len
+ int kA=(71-noLZ) >> 1;
+ expA = 71 - noLZ - (kA<<1) ;
+
+ if(kA<0){
+ regA = (-kA & 0xFFFF);
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+
+ if(regA>14){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uA.ui= 0x7FFF): (uA.ui=0x1);
+ }
+ else{
+
+ //remove hidden bit
+ frac64A&=0x7FFFFFFFFFFFFFFF;
+ shift = regA+50; //1 es bit, 1 sign bit and 1 r terminating bit , 16+31+3
+ fracA = frac64A>>shift;
+
+ if (regA!=14){
+ bitNPlusOne = (frac64A>>(shift-1)) & 0x1;
+ unsigned long long tmp = frac64A<<(65-shift);
+ if(frac64A<<(65-shift)) bitsMore = 1;
+ }
+ else if (frac64A>0){
+ fracA=0;
+ bitsMore=1;
+ }
+
+ if (regA==14 && expA) bitNPlusOne = 1;
+
+ uA.ui = packToP16UI(regime, regA, expA, fracA);
+
+ if (bitNPlusOne){
+ uA.ui += (uA.ui&1) | bitsMore;
+ }
+ }
+
+ if (sign) uA.ui = -uA.ui & 0xFFFF;
+ return uA.p;
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertQuire32ToPosit32.c b/source/luametatex/source/libraries/softposit/source/c_convertQuire32ToPosit32.c
new file mode 100644
index 000000000..c3de4b768
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertQuire32ToPosit32.c
@@ -0,0 +1,183 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+
+posit32_t q32_to_p32(quire32_t qA){
+
+ union ui512_q32 uZ;
+ union ui32_p32 uA;
+ uint_fast32_t regA, fracA = 0, shift=0, regime;
+ uint_fast64_t frac64A;
+ bool sign, regSA=0, bitNPlusOne=0, bitsMore=0;
+ int_fast32_t expA = 0;
+ int i;
+
+ if (isQ32Zero(qA)){
+ uA.ui=0;
+ return uA.p;
+ }
+ //handle NaR
+ else if (isNaRQ32(qA)){
+ uA.ui=0x80000000;
+ return uA.p;
+ }
+
+ uZ.q = qA;
+
+ sign = uZ.ui[0]>>63;
+
+ if(sign){
+ for (i=7; i>=0; i--){
+ if (uZ.ui[i]>0){
+ uZ.ui[i] = - uZ.ui[i];
+ i--;
+ while(i>=0){
+ uZ.ui[i] = ~uZ.ui[i];
+ i--;
+ }
+ break;
+ }
+ }
+ }
+ //minpos and maxpos
+
+ int noLZ =0;
+
+ for (i=0; i<8; i++){
+ if (uZ.ui[i]==0){
+ noLZ+=64;
+ }
+ else{
+ uint_fast64_t tmp = uZ.ui[i];
+ int noLZtmp = 0;
+
+ while (!(tmp>>63)){
+ noLZtmp++;
+ tmp= (tmp<<1);
+ }
+
+ noLZ+=noLZtmp;
+ frac64A = tmp;
+ if (i!=7 && noLZtmp!=0){
+ frac64A+= ( uZ.ui[i+1]>>(64-noLZtmp) );
+ if( uZ.ui[i+1] & (((uint64_t)0x1<<(64-noLZtmp))-1) )
+ bitsMore=1;
+ i++;
+ }
+ i++;
+ while(i<8){
+ if (uZ.ui[i]>0){
+ bitsMore = 1;;
+ break;
+ }
+ i++;
+ }
+ break;
+ }
+ }
+
+ //default dot is between bit 271 and 272, extreme left bit is bit 0. Last right bit is bit 511.
+ //Equations derived from quire32_mult last_pos = 271 - (kA<<2) - expA and first_pos = last_pos - frac_len
+ int kA=(271-noLZ) >> 2;
+ expA = 271 - noLZ - (kA<<2) ;
+
+
+ if(kA<0){
+ //regA = (-kA & 0xFFFF);
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+
+ if(regA>30){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uA.ui= 0x7FFFFFFF): (uA.ui=0x1);
+ }
+ else{
+
+ //remove hidden bit
+ frac64A&=0x7FFFFFFFFFFFFFFF;
+
+ shift = regA+35; //2 es bit, 1 sign bit and 1 r terminating bit , 31+4
+
+ fracA = frac64A>>shift;
+
+ if (regA<=28){
+ bitNPlusOne = (frac64A>>(shift-1)) & 0x1;
+ expA<<= (28-regA);
+ if (frac64A<<(65-shift)) bitsMore=1;
+
+ }
+ else {
+ if (regA==30){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==29){
+ bitNPlusOne = expA&0x1;
+ expA>>=1; //taken care of by the pack algo
+ }
+ if (frac64A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ }
+
+ uA.ui = packToP32UI(regime, expA, fracA);
+ if (bitNPlusOne)
+ uA.ui += (uA.ui&1) | bitsMore;
+
+ }
+ if (sign) uA.ui = -uA.ui & 0xFFFFFFFF;
+
+ return uA.p;
+
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertQuire8ToPosit8.c b/source/luametatex/source/libraries/softposit/source/c_convertQuire8ToPosit8.c
new file mode 100644
index 000000000..bd537320f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertQuire8ToPosit8.c
@@ -0,0 +1,123 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include <stdint.h>
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit8_t q8_to_p8(quire8_t qA){
+
+ union ui32_q8 uZ;
+ union ui8_p8 uA;
+ uint_fast8_t regA, fracA = 0, shift=0, regime;
+ uint_fast32_t frac32A;
+ bool sign=0, regSA=0, bitNPlusOne=0, bitsMore=0;
+
+ if (isQ8Zero(qA)){
+ uA.ui=0;
+ return uA.p;
+ }
+ //handle NaR
+ else if (isNaRQ8(qA)){
+ uA.ui=0x80;
+ return uA.p;
+ }
+
+ uZ.q = qA;
+
+ sign = uZ.ui>>31;
+
+ if(sign){
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ }
+
+ int noLZ =0;
+
+ uint_fast32_t tmp = uZ.ui;
+
+ while (!(tmp>>31)){//==0
+ noLZ++;
+ tmp<<=1;
+ }
+ frac32A = tmp;
+
+ //default dot is between bit 19 and 20, extreme left bit is bit 0. Last right bit is bit 31.
+ //Scale = k
+ int kA=(19-noLZ);
+
+ if(kA<0){
+ regA = (-kA & 0xFF);
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7F-(0x7F>>regA);
+ }
+
+ if(regA>6){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uA.ui= 0x7F): (uA.ui=0x1);
+ }
+ else{
+ //remove hidden bit
+ frac32A&=0x7FFFFFFF;
+ shift = regA+25; // 1 sign bit and 1 r terminating bit , 16+7+2
+ fracA = frac32A>>shift;
+
+ bitNPlusOne = (frac32A>>(shift-1))&0x1 ;
+
+ uA.ui = packToP8UI(regime, fracA);
+
+ if (bitNPlusOne){
+ ( (frac32A <<(33-shift)) & 0xFFFFFFFF ) ? (bitsMore=1) : (bitsMore=0);
+ uA.ui += (uA.ui&1) | bitsMore;
+ }
+ }
+
+ if (sign) uA.ui = -uA.ui & 0xFF;
+
+ return uA.p;
+
+
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_convertQuireX2ToPositX2.c b/source/luametatex/source/libraries/softposit/source/c_convertQuireX2ToPositX2.c
new file mode 100644
index 000000000..f8a5aaa57
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_convertQuireX2ToPositX2.c
@@ -0,0 +1,191 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <math.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+
+posit_2_t qX2_to_pX2(quire_2_t qA, int x){
+
+ union ui512_qX2 uZ;
+ union ui32_pX2 uA;
+ uint_fast32_t fracA = 0, shift=0, regime;
+ uint_fast64_t frac64A;
+ bool sign, regSA=0, bitNPlusOne=0, bitsMore=0;
+ int_fast32_t regA, expA = 0;
+ int i;
+
+ if (isQX2Zero(qA)){
+ uA.ui=0;
+ return uA.p;
+ }
+ //handle NaR
+ else if (isNaRQX2(qA)){
+ uA.ui=0x80000000;
+ return uA.p;
+ }
+
+ uZ.q = qA;
+
+ sign = uZ.ui[0]>>63;
+
+ if(sign){
+ for (i=7; i>=0; i--){
+ if (uZ.ui[i]>0){
+ uZ.ui[i] = - uZ.ui[i];
+ i--;
+ while(i>=0){
+ uZ.ui[i] = ~uZ.ui[i];
+ i--;
+ }
+ break;
+ }
+ }
+ }
+ //minpos and maxpos
+
+ int noLZ =0;
+
+ for (i=0; i<8; i++){
+ if (uZ.ui[i]==0){
+ noLZ+=64;
+ }
+ else{
+ uint_fast64_t tmp = uZ.ui[i];
+ int noLZtmp = 0;
+
+ while (!(tmp>>63)){
+ noLZtmp++;
+ tmp= (tmp<<1);
+ }
+
+ noLZ+=noLZtmp;
+ frac64A = tmp;
+ if (i!=7 && noLZtmp!=0){
+ frac64A+= ( uZ.ui[i+1]>>(64-noLZtmp) );
+ if( uZ.ui[i+1] & (((uint64_t)0x1<<(64-noLZtmp))-1) )
+ bitsMore=1;
+ i++;
+ }
+ i++;
+ while(i<8){
+ if (uZ.ui[i]>0){
+ bitsMore = 1;;
+ break;
+ }
+ i++;
+ }
+ break;
+ }
+ }
+ //default dot is between bit 271 and 272, extreme left bit is bit 0. Last right bit is bit 511.
+ //Equations derived from quire32_mult last_pos = 271 - (kA<<2) - expA and first_pos = last_pos - frac_len
+ int kA=(271-noLZ) >> 2;
+ expA = 271 - noLZ - (kA<<2) ;
+
+
+ if(kA<0){
+ //regA = (-kA & 0xFFFF);
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uA.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ //remove hidden bit
+ frac64A&=0x7FFFFFFFFFFFFFFF;
+
+ shift = regA+35; //2 es bit, 1 sign bit and 1 r terminating bit , 31+4
+
+ fracA = ((uint32_t) (frac64A>>shift));
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+
+ bitNPlusOne = (frac64A>>(shift+31-x)) & 0x1;
+ if ((frac64A<<(33-shift+x)) !=0) bitsMore=1;
+ }
+ else {
+ if (regA==(x-2)){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==(x-3)){
+ bitNPlusOne = expA&0x1;
+ //expA>>=1; //taken care of by the pack algo
+ expA &=0x2;
+ }
+ if (frac64A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+
+ expA <<= (28-regA);
+ uA.ui = packToP32UI(regime, expA, fracA) & ((int32_t)0x80000000>>(x-1));
+
+ if (bitNPlusOne)
+ uA.ui += (((uA.ui>>(32-x)) &0x1) | (uint32_t)bitsMore )<< (32-x);
+ }
+
+ if (sign) uA.ui = -uA.ui & 0xFFFFFFFF;
+
+ return uA.p;
+
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/c_int.c b/source/luametatex/source/libraries/softposit/source/c_int.c
new file mode 100644
index 000000000..16b6c756b
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/c_int.c
@@ -0,0 +1,155 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast64_t p8_int( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ int_fast64_t iZ;
+ uint_fast8_t scale = 0, uiA;
+ bool sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ //NaR
+ if (uiA==0x80) return 0x8000000000000000LL;
+
+ sign = uiA>>7;
+ if (sign) uiA = -uiA & 0xFF;
+
+ if (uiA < 0x40) return 0;
+ else if (uiA < 0x60) iZ = 1;
+ else {
+ uiA -= 0x40;
+ while (0x20 & uiA) {
+ scale ++;
+ uiA = (uiA - 0x20) << 1;
+ }
+ uiA <<= 1;
+ iZ = ((uint64_t)uiA | 0x40) >> (6 - scale);
+ }
+
+ if (sign) iZ = -iZ;
+ return iZ;
+
+}
+
+int_fast64_t p16_int( posit16_t pA ){
+ union ui16_p16 uA;
+ int_fast64_t iZ;
+ uint_fast16_t scale = 0, uiA;
+ bool sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ // NaR
+ if (uiA==0x8000) return 0x8000000000000000LL;
+
+ sign = uiA>>15;
+ if (sign) uiA = -uiA & 0xFFFF;
+
+ if (uiA < 0x4000) return 0;
+ else if (uiA < 0x5000) iZ = 1;
+ else if (uiA < 0x5800) iZ = 2;
+ else{
+ uiA -= 0x4000;
+ while (0x2000 & uiA) {
+ scale += 2;
+ uiA = (uiA - 0x2000) << 1;
+ }
+ uiA <<= 1;
+ if (0x2000 & uiA) scale++;
+ iZ = ((uint64_t)uiA | 0x2000) >> (13 - scale);
+
+ }
+ if (sign) iZ = -iZ;
+ return iZ;
+
+}
+
+
+int64_t p32_int( posit32_t pA ){
+ union ui32_p32 uA;
+ int_fast64_t iZ;
+ uint_fast32_t scale = 0, uiA;
+ bool sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000) return 0x8000000000000000;
+
+ sign = uiA>>31;
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (uiA < 0x40000000) return 0;
+ else if (uiA < 0x48000000) iZ = 1;
+ else if (uiA < 0x4C000000) iZ = 2;
+ else if(uiA>0x7FFFAFFF) iZ= 0x7FFFFFFFFFFFFFFF;
+ else{
+ uiA -= 0x40000000;
+ while (0x20000000 & uiA) {
+ scale += 4;
+ uiA = (uiA - 0x20000000) << 1;
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale+=2; // If first exponent bit is 1, increment the scale.
+ if (0x10000000 & uiA) scale++;
+ iZ = ((uiA | 0x10000000ULL)&0x1FFFFFFFULL) << 34; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ iZ = (scale<62) ? ((uiA | 0x10000000ULL)&0x1FFFFFFFULL) >> (28-scale):
+ ((uiA | 0x10000000ULL)&0x1FFFFFFFULL) << (scale-28);
+
+ }
+
+ if (sign) iZ = -iZ ;
+ return iZ;
+}
+
+int64_t pX2_int( posit_2_t pA ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_int(p32);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/i32_to_p16.c b/source/luametatex/source/libraries/softposit/source/i32_to_p16.c
new file mode 100644
index 000000000..c7ec6a24b
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i32_to_p16.c
@@ -0,0 +1,90 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t i32_to_p16( int32_t iA ){
+ int_fast8_t k, log2 = 25;
+ union ui16_p16 uZ;
+ uint_fast16_t uiA;
+ uint_fast32_t expA, mask = 0x02000000, fracA;
+ bool sign;
+
+
+ if (iA < -134217728){ //-2147483648 to -134217729 rounds to P32 value -268435456
+ uZ.ui = 0x8001; //-maxpos
+ return uZ.p;
+ }
+
+ sign = iA>>31;
+ if(sign){
+ iA = -iA &0xFFFFFFFF;
+ }
+
+ if( iA > 134217728 ) { //134217729 to 2147483647 rounds to P32 value 268435456
+ uiA = 0x7FFF; //maxpos
+ }
+ else if ( iA > 0x02FFFFFF ){
+ uiA = 0x7FFE;
+ }
+ else if ( iA < 2 ){
+ uiA = (iA << 14);
+ }
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = log2 >> 1;
+ expA = (log2 & 0x1) << (12 - k);
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7FFF ^ (0x3FFF >> k)) | expA | ( fracA >> (k + 13));
+ mask = 0x1000 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ (sign) ? (uZ.ui = -uiA &0xFFFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/i32_to_p32.c b/source/luametatex/source/libraries/softposit/source/i32_to_p32.c
new file mode 100644
index 000000000..b822d9e39
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i32_to_p32.c
@@ -0,0 +1,87 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit32_t i32_to_p32( int32_t iA ) {
+ int_fast8_t k, log2 = 31;//length of bit (e.g. 4294966271) in int (32 but because we have only 32 bits, so one bit off to accommodate that fact)
+ union ui32_p32 uZ;
+ uint_fast32_t uiA;
+ uint_fast32_t expA, mask = 0x80000000, fracA;
+ bool sign;
+
+ if (iA < -2147483135){ //-2147483648 to -2147483136 rounds to P32 value -2147483648
+ uZ.ui = 0x80500000;
+ return uZ.p;
+ }
+
+ sign = iA>>31;
+ if(sign) iA = -iA &0xFFFFFFFF;
+
+ if ( iA >2147483135)//2147483136 to 2147483647 rounds to P32 value (2147483648)=> 0x7FB00000
+ uiA = 0x7FB00000;
+ else if ( iA < 0x2 )
+ uiA = (iA << 30);
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = (log2 >> 2);
+ expA = (log2 & 0x3) << (27 - k);
+ fracA = (fracA ^ mask);
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | expA | fracA>>(k+4);
+
+ mask = 0x8 << k; //bitNPlusOne
+
+ if (mask & fracA)
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+
+ }
+ (sign) ? (uZ.ui = -uiA &0xFFFFFFFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/i32_to_p8.c b/source/luametatex/source/libraries/softposit/source/i32_to_p8.c
new file mode 100644
index 000000000..698f4b9bd
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i32_to_p8.c
@@ -0,0 +1,88 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t i32_to_p8( int32_t iA ){
+ int_fast8_t k, log2 = 6;//length of bit
+ union ui8_p8 uZ;
+ uint_fast8_t uiA;
+ uint_fast32_t mask = 0x40, fracA;
+ bool sign;
+
+ if (iA < -48){ //-48 to -MAX_INT rounds to P32 value -268435456
+ uZ.ui = 0x81; //-maxpos
+ return uZ.p;
+ }
+ sign = iA>>31;
+ if(sign){
+ iA = -iA &0xFFFFFFFF;
+ }
+ if ( iA > 48 ) {
+ uiA = 0x7F;
+ }
+ else if ( iA < 2 ){
+ uiA = (iA << 6);
+ }
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = log2;
+
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7F ^ (0x3F >> k)) | ( fracA >> (k+1) ) ;
+
+ mask = 0x1 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ (sign) ? (uZ.ui = -uiA &0xFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/i32_to_pX1.c b/source/luametatex/source/libraries/softposit/source/i32_to_pX1.c
new file mode 100644
index 000000000..5baa6e847
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i32_to_pX1.c
@@ -0,0 +1,115 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit_1_t i32_to_pX1( int32_t a, int x ) {
+ int_fast8_t k, log2 = 31;//length of bit (e.g. 2147418111) in int (32 but because we have only 32 bits, so one bit off to accommodate that fact)
+ union ui32_pX1 uZ;
+ uint_fast32_t uiA=0;
+ uint_fast32_t expA, mask = 0x80000000, fracA;
+ bool sign;
+
+ sign = a>>31;
+ if(sign) a = -a &0xFFFFFFFF;
+
+ //NaR
+ if (a == 0x80000000 || x<2 || x>32)
+ uiA = 0x80000000;
+ else if (x==2){
+ if (a>0) uiA=0x40000000;
+ }
+ else if ( a > 2147418111){
+ uiA = 0x7FFF9FFF; // 2147483648
+ //if (x<12) uiA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = (log2 >> 1);
+ expA = (log2 & 0x1) << (28-k);
+ fracA = (fracA ^ mask);
+
+ if(k>=(x-2)){//maxpos
+ uiA= 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-3)){//bitNPlusOne-> first exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+ if( (expA & 0x2) && ((expA&0x1) | fracA) ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | ((expA &0x2)<< (27 - k));
+ if(expA&0x1){
+ if( (((uint32_t)0x80000000>>(x-1)) & uiA)| fracA)
+ uiA += ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ else if (k==(x-5)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k));
+ mask = 0x8 << (k -x);
+ if (mask & fracA){ //bitNPlusOne
+ if (((mask - 1) & fracA) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = ((0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k)) | fracA>>(k+4)) & ((int32_t)0x80000000>>(x-1));;
+ mask = 0x8 << (k-x); //bitNPlusOne
+ if (mask & fracA)
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+
+ }
+ (sign) ? (uZ.ui = -uiA &0xFFFFFFFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/i32_to_pX2.c b/source/luametatex/source/libraries/softposit/source/i32_to_pX2.c
new file mode 100644
index 000000000..ac31957fb
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i32_to_pX2.c
@@ -0,0 +1,121 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit_2_t i32_to_pX2( int32_t iA, int x ) {
+ int_fast8_t k, log2 = 31;//length of bit (e.g. 4294966271) in int (32 but because we have only 32 bits, so one bit off to accomdate that fact)
+ union ui32_pX2 uZ;
+ uint_fast32_t uiA=0;
+ uint_fast32_t expA, mask = 0x80000000, fracA;
+ bool sign;
+
+ if (iA < -2147483135){
+ uZ.ui = 0x80500000;
+ return uZ.p;
+ }
+
+ sign = iA>>31;
+ if(sign) iA = -iA &0xFFFFFFFF;
+
+ //NaR
+ if (x<2 || x>32)
+ uiA = 0x80000000;
+ else if (x==2){
+ if (iA>0) uiA=0x40000000;
+ }
+ else if ( iA > 2147483135){//2147483136 to 2147483647 rounds to P32 value (2147483648)=> 0x7FB00000
+ uiA = 0x7FB00000; // 2147483648
+ if (x<10) uiA&=((int32_t)0x80000000>>(x-1));
+ else if (x<12) uiA = 0x7FF00000&((int32_t)0x80000000>>(x-1));
+ }
+ else if ( iA < 0x2 )
+ uiA = (iA << 30);
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = (log2 >> 2);
+ expA = (log2 & 0x3);
+ fracA = (fracA ^ mask);
+
+ if(k>=(x-2)){//maxpos
+ uiA= 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-3)){//bitNPlusOne-> first exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+ if( (expA & 0x2) && ((expA&0x1) | fracA) ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | ((expA &0x2)<< (27 - k));
+ if(expA&0x1){
+ if( (((uint32_t)0x80000000>>(x-1)) & uiA)| fracA)
+ uiA += ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ else if (k==(x-5)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k));
+ mask = 0x8 << (k -x);
+ if (mask & fracA){ //bitNPlusOne
+ if (((mask - 1) & fracA) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = ((0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k)) | fracA>>(k+4)) & ((int32_t)0x80000000>>(x-1));;
+ mask = 0x8 << (k-x); //bitNPlusOne
+ if (mask & fracA)
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+
+ }
+ (sign) ? (uZ.ui = -uiA &0xFFFFFFFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/i64_to_p16.c b/source/luametatex/source/libraries/softposit/source/i64_to_p16.c
new file mode 100644
index 000000000..43e61abe7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i64_to_p16.c
@@ -0,0 +1,90 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t i64_to_p16( int64_t iA ) {
+ int_fast8_t k, log2 = 25;
+ union ui16_p16 uZ;
+ uint_fast16_t uiA;
+ uint_fast64_t expA, mask = 0x0000000002000000, fracA;
+ bool sign;
+
+ if (iA < -134217728){ //-9223372036854775808 to -134217729 rounds to P32 value -268435456
+ uZ.ui = 0x8001; //-maxpos
+ return uZ.p;
+ }
+
+ sign = iA>>63;
+ if (sign) iA = -iA;
+
+ if( iA > 134217728 ) { //134217729 to 9223372036854775807 rounds to P32 value 268435456
+ uiA = 0x7FFF; //maxpos
+ }
+ else if ( iA > 0x0000000008000000 ) {
+ uiA = 0x7FFF;
+ }
+ else if ( iA > 0x0000000002FFFFFF ){
+ uiA = 0x7FFE;
+ }
+ else if ( iA < 2 ){
+ uiA = (iA << 14);
+ }
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = log2 >> 1;
+ expA = (log2 & 0x1) << (12 - k);
+ fracA = fracA ^ mask;
+ uiA = (0x7FFF ^ (0x3FFF >> k)) | expA | (fracA >> (k + 13));
+ mask = 0x1000 << k;
+ if (mask & fracA) {
+ if ( ((mask - 1) & fracA) | ((mask << 1) & fracA) ) uiA++;
+ }
+ }
+ (sign) ? (uZ.ui = -uiA) : (uZ.ui = uiA);
+ return uZ.p;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/i64_to_p32.c b/source/luametatex/source/libraries/softposit/source/i64_to_p32.c
new file mode 100644
index 000000000..5453860fc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i64_to_p32.c
@@ -0,0 +1,89 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t i64_to_p32( int64_t iA ) {
+ int_fast8_t k, log2 = 63;//length of bit (e.g. 9222809086901354496) in int (64 but because we have only 64 bits, so one bit off to accomdate that fact)
+ union ui32_p32 uZ;
+ uint_fast64_t uiA;
+ uint_fast64_t mask = 0x8000000000000000, fracA;
+ uint_fast32_t expA;
+ bool sign;
+
+ if (iA < -9222809086901354495){//-9222809086901354496 to -9223372036854775808 will be P32 value -9223372036854775808
+ uZ.ui = 0x80005000;
+ return uZ.p;
+ }
+ sign = iA>>63;
+ if(sign) iA = -iA;
+
+ if ( iA >9222809086901354495)//9222809086901354495 bcos 9222809086901354496 to 9223372036854775807 will be P32 value 9223372036854775808
+ uiA = 0x7FFFB000; // P32: 9223372036854775808
+ else if ( iA < 0x2 )
+ uiA = (iA << 30);
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = (log2 >> 2);
+
+ expA = (log2 & 0x3) << (27 - k);
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | expA | fracA>>(k+36);
+
+ mask = 0x800000000 << k; //bitNPlusOne
+
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ (sign) ? (uZ.ui = -uiA) : (uZ.ui = uiA);
+ return uZ.p;
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/i64_to_p8.c b/source/luametatex/source/libraries/softposit/source/i64_to_p8.c
new file mode 100644
index 000000000..646d7e4be
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i64_to_p8.c
@@ -0,0 +1,90 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t i64_to_p8( int64_t iA ){
+ int_fast8_t k, log2 = 6;//length of bit
+ union ui8_p8 uZ;
+ uint_fast8_t uiA;
+ uint_fast64_t mask = 0x40, fracA;
+ bool sign;
+
+ if (iA < -48){ //-48 to -MAX_INT rounds to P32 value -268435456
+ uZ.ui = 0x81; //-maxpos
+ return uZ.p;
+ }
+
+ sign = iA>>63;
+ if(sign){
+ iA = -iA;
+ }
+
+ if ( iA > 48 ) {
+ uiA = 0x7F;
+ }
+ else if ( iA < 2 ){
+ uiA = (iA << 6);
+ }
+ else {
+ fracA = iA;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = log2;
+
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7F ^ (0x3F >> k)) | ( fracA >> (k+1) ) ;
+
+ mask = 0x1 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ (sign) ? (uZ.ui = -uiA &0xFF) : (uZ.ui = uiA);
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/i64_to_pX2.c b/source/luametatex/source/libraries/softposit/source/i64_to_pX2.c
new file mode 100644
index 000000000..ba2c09ed6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/i64_to_pX2.c
@@ -0,0 +1,119 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t i64_to_pX2( int64_t a, int x ) {
+ int_fast8_t k, log2 = 63;//length of bit (e.g. 18445618173802707967) in int (64 but because we have only 64 bits, so one bit off to accomdate that fact)
+ union ui32_pX2 uZ;
+ uint_fast64_t uiA=0;
+ uint_fast64_t mask = 0x8000000000000000, frac64A;
+ uint_fast32_t expA;
+ bool sign;
+
+ sign = a>>63;
+ if(sign) a = -a;
+ //NaR
+ if (a == 0x8000000000000000 || x<2 || x>32 )
+ uiA = 0x80000000;
+ else if (x==2){
+ if (a>0) uiA=0x40000000;
+ }
+ else if ( a > 0x7FFDFFFFFFFFFFFF){//9222809086901354495
+ uiA = 0x7FFFB000; // P32: 9223372036854775808
+ if (x<18) uiA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ frac64A = a;
+ while ( !(frac64A & mask) ) {
+ log2--;
+ frac64A <<= 1;
+ }
+
+ k = (log2 >> 2);
+
+ expA = (log2 & 0x3);
+ frac64A = (frac64A ^ mask);
+
+ if(k>=(x-2)){//maxpos
+ uiA= 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-3)){//bitNPlusOne-> first exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+ if( (expA & 0x2) && ((expA&0x1) | frac64A) ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | ((expA &0x2)<< (27 - k));
+ if(expA&0x1){
+ if( (((uint32_t)0x80000000>>(x-1)) & uiA)|| frac64A)
+ uiA += ((uint32_t)0x80000000>>(x-1));
+ }
+
+ }
+ else if (k==(x-5)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k));
+ mask = (uint64_t)0x800000000 << (k + 32-x);
+ if (mask & frac64A){ //bitNPlusOne
+ if (((mask - 1) & frac64A) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k)) | ((frac64A>>(k+36)) & ((int32_t)0x80000000>>(x-1)));
+ mask = (uint64_t)0x800000000 << (k + 32-x); //bitNPlusOne position
+ if (mask & frac64A) {
+ if (((mask - 1) & frac64A) | ((mask << 1) & frac64A)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ }
+ (sign) ? (uZ.ui = -uiA) : (uZ.ui = uiA);
+ return uZ.p;
+}
+
+
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
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_add.c b/source/luametatex/source/libraries/softposit/source/p16_add.c
new file mode 100644
index 000000000..07b12bca3
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_add.c
@@ -0,0 +1,75 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t p16_add( posit16_t a, posit16_t b ){
+ union ui16_p16 uA, uB;
+ uint_fast16_t uiA, uiB;
+ union ui16_p16 uZ;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+
+ //Zero or infinity
+ if (uiA==0 || uiB==0){ // Not required but put here for speed
+ uZ.ui = uiA | uiB;
+ return uZ.p;
+ }
+ else if ( uiA==0x8000 || uiB==0x8000 ){
+ uZ.ui = 0x8000;
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>15)
+ return softposit_subMagsP16(uiA, uiB);
+ else
+ return softposit_addMagsP16(uiA, uiB);
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_div.c b/source/luametatex/source/libraries/softposit/source/p16_div.c
new file mode 100644
index 000000000..2786234ba
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_div.c
@@ -0,0 +1,186 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include <stdlib.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t p16_div( posit16_t pA, posit16_t pB ) {
+ union ui16_p16 uA, uB, uZ;
+ uint_fast16_t uiA, uiB, fracA, fracB, regA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t expA, kA=0;
+ uint_fast32_t frac32A, frac32Z, rem;
+ div_t divresult;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if ( uiA==0x8000 || uiB==0x8000 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x8000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x8000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP16UI( uiA );
+ signB = signP16UI( uiB );
+ signZ = signA ^ signB;
+ if(signA) uiA = (-uiA & 0xFFFF);
+ if(signB) uiB = (-uiB & 0xFFFF);
+ regSA = signregP16UI(uiA);
+ regSB = signregP16UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ fracA = (0x4000 | tmp);
+ frac32A = fracA<<14;
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ fracB = (0x4000 | tmp);
+ }
+ else{
+ kA++;
+ while (!(tmp>>15)){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ fracB = (0x4000 | (0x7FFF & tmp));
+ }
+ expA -= tmp>>14;
+
+ divresult = div (frac32A,fracB);
+ frac32Z = divresult.quot;
+ rem = divresult.rem;
+
+ if (expA<0){
+ expA=1;
+ kA--;
+ }
+ if (frac32Z!=0){
+ rcarry = frac32Z >> 14; // this is the hidden bit (14th bit) , extreme right bit is bit 0
+ if (!rcarry){
+ if (expA==0) kA --;
+ expA^=1;
+ frac32Z<<=1;
+ }
+ }
+ if(kA<0){
+ regA = (-kA & 0xFFFF);
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+
+ if(regA>14){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac32Z &= 0x3FFF;
+ fracA = (uint_fast16_t)frac32Z >> (regA+1);
+
+ if (regA!=14) bitNPlusOne = (frac32Z >> regA) & 0x1;
+ else if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==14 && expA) bitNPlusOne = 1;
+
+ //sign is always zero
+ uZ.ui = packToP16UI(regime, regA, expA, fracA);
+
+ if (bitNPlusOne){
+ ( ((1<<regA)-1) & frac32Z ) ? (bitsMore=1) : (bitsMore=0);
+ if (rem) bitsMore =1;
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_eq.c b/source/luametatex/source/libraries/softposit/source/p16_eq.c
new file mode 100644
index 000000000..53dd01d93
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_eq.c
@@ -0,0 +1,58 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p16_eq( posit16_t pA, posit16_t pB ){
+
+ union ui16_p16 uA, uB;
+ int16_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int16_t) uA.ui;
+ uB.p = pB;
+ uiB = (int16_t)uB.ui;
+
+ if (uiA==uiB)
+ return true;
+ else
+ return false;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/p16_le.c b/source/luametatex/source/libraries/softposit/source/p16_le.c
new file mode 100644
index 000000000..4e3e2edea
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_le.c
@@ -0,0 +1,58 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p16_le( posit16_t pA, posit16_t pB ) {
+ union ui16_p16 uA, uB;
+ int16_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int16_t) uA.ui;
+ uB.p = pB;
+ uiB = (int16_t)uB.ui;
+
+ if (uiA<=uiB)
+ return true;
+ else
+ return false;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_lt.c b/source/luametatex/source/libraries/softposit/source/p16_lt.c
new file mode 100644
index 000000000..5b47098d2
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_lt.c
@@ -0,0 +1,60 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p16_lt( posit16_t pA, posit16_t pB )
+{
+ union ui16_p16 uA, uB;
+ int16_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int16_t) uA.ui;
+ uB.p = pB;
+ uiB = (int16_t)uB.ui;
+
+ if (uiA<uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_mul.c b/source/luametatex/source/libraries/softposit/source/p16_mul.c
new file mode 100644
index 000000000..bf9c8b967
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_mul.c
@@ -0,0 +1,171 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit16_t p16_mul( posit16_t pA, posit16_t pB ){
+
+ union ui16_p16 uA, uB, uZ;
+ uint_fast16_t uiA, uiB;
+ uint_fast16_t regA, fracA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t expA;
+ int_fast8_t kA=0;
+ uint_fast32_t frac32Z;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+
+ //NaR or Zero
+ if ( uiA==0x8000 || uiB==0x8000 ){
+ uZ.ui = 0x8000;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ uZ.ui = 0;
+ return uZ.p;
+ }
+
+ signA = signP16UI( uiA );
+ signB = signP16UI( uiB );
+ signZ = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFF);
+ if(signB) uiB = (-uiB & 0xFFFF);
+
+ regSA = signregP16UI(uiA);
+ regSB = signregP16UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ fracA = (0x4000 | tmp);
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA += tmp>>14;
+ frac32Z = (uint_fast32_t) fracA * (0x4000 | tmp);
+
+ if (expA>1){
+ kA++;
+ expA ^=0x2;
+ }
+
+ rcarry = frac32Z>>29;//3rd bit of frac32Z
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac32Z>>=1;
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFFFF);
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+
+ if(regA>14){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac32Z = (frac32Z&0xFFFFFFF) >> (regA-1);
+ fracA = (uint_fast16_t) (frac32Z>>16);
+
+ if (regA!=14) bitNPlusOne |= (0x8000 & frac32Z) ;
+ else if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==14 && expA) bitNPlusOne = 1;
+
+ //sign is always zero
+ uZ.ui = packToP16UI(regime, regA, expA, fracA);
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7FFF & frac32Z) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_mulAdd.c b/source/luametatex/source/libraries/softposit/source/p16_mulAdd.c
new file mode 100644
index 000000000..03548f7e6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_mulAdd.c
@@ -0,0 +1,63 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t p16_mulAdd( posit16_t a, posit16_t b, posit16_t c )
+{
+ union ui16_p16 uA;
+ uint_fast16_t uiA;
+ union ui16_p16 uB;
+ uint_fast16_t uiB;
+ union ui16_p16 uC;
+ uint_fast16_t uiC;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+ uC.p = c;
+ uiC = uC.ui;
+ return softposit_mulAddP16( uiA, uiB, uiC, 0 );
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_roundToInt.c b/source/luametatex/source/libraries/softposit/source/p16_roundToInt.c
new file mode 100644
index 000000000..383fdb131
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_roundToInt.c
@@ -0,0 +1,98 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit16_t p16_roundToInt( posit16_t pA ) {
+
+ union ui16_p16 uA;
+ uint_fast16_t mask = 0x2000, scale=0, tmp=0, uiA;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ sign = (uiA > 0x8000);
+
+ // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFF; // A is now |A|.
+ if (uiA <= 0x3000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ uA.ui = 0;
+ return uA.p;
+ }
+ else if (uiA < 0x4800) { // 1/2 < x < 3/2 rounds to 1.
+ uA.ui = 0x4000;
+ }
+ else if (uiA <= 0x5400) { // 3/2 <= x <= 5/2 rounds to 2.
+ uA.ui = 0x5000;
+ }
+ else if (uiA >= 0x7C00) { // If |A| is 256 or greater, leave it unchanged.
+ return uA.p; // This also takes care of the NaR case, 0x8000.
+ }
+ else { // 34% of the cases, we have to decode the posit.
+ while (mask & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ mask >>= 1; // Move the mask right, to the next bit.
+ }
+ mask >>= 1; // Skip over termination bit.
+ if (mask & uiA) scale++; // If exponent is 1, increment the scale.
+
+ mask >>= scale; // Point to the last bit of the integer part.
+ bitLast = (uiA & mask); // Extract the bit, without shifting it.
+
+ mask >>= 1;
+ tmp = (uiA & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ uiA ^= tmp; // Erase the bit, if it was set.
+ tmp = uiA & (mask - 1); // tmp has any remaining bits.
+ uiA ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) uiA += (mask << 1);
+ }
+ uA.ui = uiA;
+ }
+ if (sign) uA.ui = -uA.ui & 0xFFFF; // Apply the sign of Z.
+ return uA.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_sqrt.c b/source/luametatex/source/libraries/softposit/source/p16_sqrt.c
new file mode 100644
index 000000000..24d33f9c0
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_sqrt.c
@@ -0,0 +1,143 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+extern const uint_fast16_t softposit_approxRecipSqrt0[];
+extern const uint_fast16_t softposit_approxRecipSqrt1[];
+
+posit16_t p16_sqrt( posit16_t pA ) {
+
+ union ui16_p16 uA;
+ uint_fast16_t expA, fracA, index, r0, shift, sigma0, uiA, uiZ;
+ uint_fast32_t eSqrR0, fracZ, negRem, recipSqrt, shiftedFracZ;
+ int_fast16_t kZ;
+ bool bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ // If sign bit is set, return NaR.
+ if (uiA>>15) {
+ uA.ui = 0x8000;
+ return uA.p;
+ }
+ // If the argument is zero, return zero.
+ if (uiA==0) {
+ uA.ui = 0;
+ return uA.p;
+ }
+ // Compute the square root. Here, kZ is the net power-of-2 scaling of the result.
+ // Decode the regime and exponent bit; scale the input to be in the range 1 to 4:
+ if (uiA >> 14) {
+ kZ = -1;
+ while (uiA & 0x4000) {
+ kZ++;
+ uiA= (uiA<<1) & 0xFFFF;
+ }
+ }
+ else {
+ kZ = 0;
+ while (!(uiA & 0x4000)) {
+ kZ--;
+ uiA= (uiA<<1) & 0xFFFF;
+ }
+
+ }
+ uiA &= 0x3fff;
+ expA = 1 - (uiA >> 13);
+ fracA = (uiA | 0x2000) >> 1;
+
+ // Use table look-up of first four bits for piecewise linear approx. of 1/sqrt:
+ index = ((fracA >> 8) & 0xE) + expA;
+
+ r0 = softposit_approxRecipSqrt0[index]
+ - (((uint_fast32_t) softposit_approxRecipSqrt1[index]
+ * (fracA & 0x1FF)) >> 13);
+ // Use Newton-Raphson refinement to get more accuracy for 1/sqrt:
+ eSqrR0 = ((uint_fast32_t) r0 * r0) >> 1;
+
+ if (expA) eSqrR0 >>= 1;
+ sigma0 = 0xFFFF ^ (0xFFFF & (((uint64_t)eSqrR0 * (uint64_t)fracA) >> 18));//~(uint_fast16_t) ((eSqrR0 * fracA) >> 18);
+ recipSqrt = ((uint_fast32_t) r0 << 2) + (((uint_fast32_t) r0 * sigma0) >> 23);
+
+ // We need 17 bits of accuracy for posit16 square root approximation.
+ // Multiplying 16 bits and 18 bits needs 64-bit scratch before the right shift:
+ fracZ = (((uint_fast64_t) fracA) * recipSqrt) >> 13;
+
+ // Figure out the regime and the resulting right shift of the fraction:
+ if (kZ < 0) {
+ shift = (-1 - kZ) >> 1;
+ uiZ = 0x2000 >> shift;
+ }
+ else {
+ shift = kZ >> 1;
+ uiZ = 0x7fff - (0x7FFF >> (shift + 1));
+ }
+ // Set the exponent bit in the answer, if it is nonzero:
+ if (kZ & 1) uiZ |= (0x1000 >> shift);
+
+ // Right-shift fraction bits, accounting for 1 <= a < 2 versus 2 <= a < 4:
+ fracZ = fracZ >> (expA + shift);
+
+ // Trick for eliminating off-by-one cases that only uses one multiply:
+ fracZ++;
+ if (!(fracZ & 7)) {
+ shiftedFracZ = fracZ >> 1;
+ negRem = (shiftedFracZ * shiftedFracZ) & 0x3FFFF;
+ if (negRem & 0x20000) {
+ fracZ |= 1;
+ } else {
+ if (negRem) fracZ--;
+ }
+ }
+ // Strip off the hidden bit and round-to-nearest using last 4 bits.
+ fracZ -= (0x10000 >> shift);
+ bitNPlusOne = (fracZ >> 3) & 1;
+ if (bitNPlusOne) {
+ if (((fracZ >> 4) & 1) | (fracZ & 7)) fracZ += 0x10;
+ }
+ // Assemble the result and return it.
+ uA.ui = uiZ | (fracZ >> 4);
+ return uA.p;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/p16_sub.c b/source/luametatex/source/libraries/softposit/source/p16_sub.c
new file mode 100644
index 000000000..a5b9b69fc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_sub.c
@@ -0,0 +1,93 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit16_t p16_sub( posit16_t a, posit16_t b ){
+
+ union ui16_p16 uA, uB;
+ uint_fast16_t uiA, uiB;
+ union ui16_p16 uZ;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //infinity
+ if ( uiA==0x8000 || uiB==0x8000 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x8000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x8000;
+#endif
+ return uZ.p;
+ }
+ //Zero
+ else if ( uiA==0 || uiB==0 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = (uiA | -uiB);
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = (uiA | -uiB);
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>15)
+ return softposit_addMagsP16(uiA, (-uiB & 0xFFFF));
+ else
+ return softposit_subMagsP16(uiA, (-uiB & 0xFFFF));
+
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_i32.c b/source/luametatex/source/libraries/softposit/source/p16_to_i32.c
new file mode 100644
index 000000000..d28a44c6a
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_i32.c
@@ -0,0 +1,97 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast32_t p16_to_i32( posit16_t pA ){
+ union ui16_p16 uA;
+ int_fast32_t mask, iZ, tmp;
+ uint_fast16_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ if (uiA==0x8000) return 0;
+
+ sign = (uiA > 0x8000); // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFF; // A is now |A|.
+
+ if (uiA <= 0x3000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x4800) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else if (uiA <= 0x5400) { // 3/2 <= x <= 5/2 rounds to 2.
+ iZ = 2;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x4000; // Strip off first regime bit (which is a 1).
+ while (0x2000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x2000) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x2000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ = ((uint32_t)uiA | 0x2000) << 17; // Left-justify fraction in 32-bit result (one left bit padding)
+ mask = 0x40000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+
+ iZ = (uint32_t)iZ >> (30 - scale); // Right-justify the integer.
+ }
+
+ if (sign) iZ = -iZ; // Apply the sign of the input.
+ return iZ;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_i64.c b/source/luametatex/source/libraries/softposit/source/p16_to_i64.c
new file mode 100644
index 000000000..d06859443
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_i64.c
@@ -0,0 +1,98 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast64_t p16_to_i64( posit16_t pA ){
+ union ui16_p16 uA;
+ int_fast64_t mask, tmp, iZ;
+ uint_fast16_t scale = 0, uiA;
+ bool sign, bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ // NaR
+ if (uiA==0x8000) return 0;
+
+ sign = uiA>>15;
+ if (sign) uiA = -uiA & 0xFFFF;
+
+ if (uiA <= 0x3000)
+ return 0;
+ else if (uiA < 0x4800)
+ iZ = 1;
+ else if (uiA <= 0x5400)
+ iZ = 2;
+ else{
+
+ uiA -= 0x4000;
+
+ while (0x2000 & uiA) {
+ scale += 2;
+ uiA = (uiA - 0x2000) << 1;
+ }
+ uiA <<= 1;
+ if (0x2000 & uiA) scale++;
+ iZ = ((uint64_t)uiA | 0x2000) << 49;
+
+ mask = 0x4000000000000000 >> scale;
+
+ bitLast = (iZ & mask);
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp;
+ iZ ^= tmp;
+ tmp = iZ & (mask - 1); // bitsMore
+ iZ ^= tmp;
+
+ if (bitNPlusOne)
+ if (bitLast | tmp) iZ += (mask << 1);
+
+ iZ = (uint64_t)iZ >> (62 - scale);
+
+ }
+ if (sign) iZ = -iZ;
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_p32.c b/source/luametatex/source/libraries/softposit/source/p16_to_p32.c
new file mode 100644
index 000000000..3d03cc617
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_p32.c
@@ -0,0 +1,109 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p16_to_p32( posit16_t pA ) {
+
+ union ui16_p16 uA;
+ union ui32_p32 uZ;
+ uint_fast16_t uiA, tmp;
+ uint_fast32_t exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x8000 || uiA==0 ){
+ uZ.ui = (uint32_t)uiA<<16;
+ return uZ.p;
+ }
+
+ sign = signP16UI( uiA );
+
+ if (sign) uiA = -uiA & 0xFFFF;
+ regSA = signregP16UI(uiA);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ exp_frac32A = (uint32_t) tmp<<16;
+
+
+ if(kA<0){
+ regA = -kA;
+ //if (regA&0x1) exp_frac32A |= 0x80000000;
+ exp_frac32A |= ((uint32_t)(regA&0x1)<<31);
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ((uint32_t)(kA&0x1)<<31);
+ (kA==0) ? (regA=1) : (regA = (kA+2)>>1);
+
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+
+ }
+
+ exp_frac32A >>=(regA+2); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac32A;
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_p8.c b/source/luametatex/source/libraries/softposit/source/p16_to_p8.c
new file mode 100644
index 000000000..74c198b99
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_p8.c
@@ -0,0 +1,120 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p16_to_p8( posit16_t pA ) {
+
+ union ui16_p16 uA;
+ union ui8_p8 uZ;
+ uint_fast16_t uiA, tmp, regime;
+ uint_fast16_t exp_frac16A=0;
+ bool sign, regSA, bitsMore=0;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x8000 || uiA==0 ){
+ uZ.ui = (uiA>>8) &0xFF;
+ return uZ.p;
+ }
+
+ sign = signP16UI( uiA );
+
+ if (sign) uiA = -uiA & 0xFFFF;
+ regSA = signregP16UI(uiA);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+
+ if (kA<-3 || kA>=3){
+ (kA<0) ? (uZ.ui=0x1):(uZ.ui= 0x7F);
+ }
+ else{
+ //2nd bit exp
+ exp_frac16A = tmp;
+ if(kA<0){
+ regA = ((-kA)<<1) - (exp_frac16A>>14);
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+
+ (kA==0)?(regA=1 + (exp_frac16A>>14)): (regA = ((kA+1)<<1) + (exp_frac16A>>14) -1);
+ regSA=1;
+ regime = 0x7F - (0x7F>>regA);
+ }
+ if (regA>5){
+ uZ.ui = regime;
+ }
+ else{
+ //int shift = regA+8;
+ //exp_frac16A= ((exp_frac16A)&0x3FFF) >> shift; //first 2 bits already empty (for sign and regime terminating bit)
+ uZ.ui = regime + ( ((exp_frac16A)&0x3FFF)>>(regA+8) );
+
+ }
+
+ }
+
+ if ( exp_frac16A & (0x80<<regA) ){
+ bitsMore = exp_frac16A & (0xFFFF>>(9-regA));
+ uZ.ui += (uZ.ui&1) | bitsMore;
+
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_pX1.c b/source/luametatex/source/libraries/softposit/source/p16_to_pX1.c
new file mode 100644
index 000000000..5a6f2a375
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_pX1.c
@@ -0,0 +1,92 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t p16_to_pX1( posit16_t pA, int x ) {
+
+ union ui16_p16 uA;
+ union ui32_pX1 uZ;
+ uint_fast16_t tmp;
+ uint_fast32_t uiA, exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = ((uint32_t) uA.ui<<16) &0xFFFFFFFF;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else if (x==32 || (((uint32_t)0xFFFFFFFF>>x) & uiA)==0 ){
+ uZ.ui = uiA;
+ }
+ else {
+
+ int shift = 32-x;
+ if( (uiA>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uiA){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uiA) || (((uint32_t)0x7FFFFFFF>>x) & uiA) )
+ uiA += (0x1<<shift);
+ }
+ }
+ uZ.ui = uiA & ((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_pX2.c b/source/luametatex/source/libraries/softposit/source/p16_to_pX2.c
new file mode 100644
index 000000000..c8696ff12
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_pX2.c
@@ -0,0 +1,137 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t p16_to_pX2( posit16_t pA, int x ) {
+
+ union ui16_p16 uA;
+ union ui32_pX2 uZ;
+ uint_fast16_t uiA, tmp;
+ uint_fast32_t exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x8000 || uiA==0 ){
+ uZ.ui = (uint32_t)uiA<<16;
+ return uZ.p;
+ }
+
+
+ sign = signP16UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFF;
+
+ if(x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else{
+ regSA = signregP16UI(uiA);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ exp_frac32A = (uint32_t) tmp<<16;
+
+ if(kA<0){
+ regA = -kA;
+ //if (regA&0x1) exp_frac32A |= 0x80000000;
+ exp_frac32A |= ((uint32_t)(regA&0x1)<<31);
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ((uint32_t)(kA&0x1)<<31);
+ (kA==0) ? (regA=1) : (regA = (kA+2)>>1);
+
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ exp_frac32A >>=(regA+2); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac32A;
+
+ int shift = 32-x;
+ if( (uZ.ui>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uZ.ui){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uZ.ui) || (((uint32_t)0x7FFFFFFF>>x) & uZ.ui) )
+ uZ.ui += (0x1<<shift);
+ }
+ }
+
+ uZ.ui &=((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+ }
+ }
+
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_ui32.c b/source/luametatex/source/libraries/softposit/source/p16_to_ui32.c
new file mode 100644
index 000000000..e69139548
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_ui32.c
@@ -0,0 +1,95 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast32_t p16_to_ui32( posit16_t pA ) {
+
+ union ui16_p16 uA;
+ uint_fast32_t mask, iZ, tmp;
+ uint_fast16_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ //if (uiA==0x8000) return 0;
+ if (uiA>=0x8000) return 0; //negative
+
+ if (uiA <= 0x3000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x4800) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else if (uiA <= 0x5400) { // 3/2 <= x <= 5/2 rounds to 2.
+ iZ = 2;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x4000; // Strip off first regime bit (which is a 1).
+ while (0x2000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x2000) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x2000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ = ((uint32_t)uiA | 0x2000) << 17; // Left-justify fraction in 32-bit result (one left bit padding)
+ mask = 0x40000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+ iZ = (uint32_t)iZ >> (30 - scale); // Right-justify the integer.
+ }
+
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p16_to_ui64.c b/source/luametatex/source/libraries/softposit/source/p16_to_ui64.c
new file mode 100644
index 000000000..021a38bd8
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p16_to_ui64.c
@@ -0,0 +1,96 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast64_t p16_to_ui64( posit16_t pA ) {
+ union ui16_p16 uA;
+ uint_fast64_t mask, iZ, tmp;
+ uint_fast16_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ //NaR
+ //if (uiA==0x8000) return 0;
+ //negative
+ if (uiA>=0x8000) return 0;
+
+ if (uiA <= 0x3000) {
+ return 0;
+ }
+ else if (uiA < 0x4800) {
+ iZ = 1;
+ }
+ else if (uiA <= 0x5400) {
+ iZ = 2;
+ }
+ else {
+ uiA -= 0x4000;
+ while (0x2000 & uiA) {
+ scale += 2;
+ uiA = (uiA - 0x2000) << 1;
+ }
+ uiA <<= 1;
+ if (0x2000 & uiA) scale++;
+ iZ = ((uint64_t)uiA | 0x2000) << 49;
+
+ mask = 0x4000000000000000 >> scale;
+
+ bitLast = (iZ & mask);
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp;
+ iZ ^= tmp;
+ tmp = iZ & (mask - 1); // bitsMore
+ iZ ^= tmp;
+
+ if (bitNPlusOne)
+ if (bitLast | tmp) iZ += (mask << 1);
+
+ iZ = (uint64_t)iZ >> (62 - scale);
+
+ }
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_add.c b/source/luametatex/source/libraries/softposit/source/p32_add.c
new file mode 100644
index 000000000..61b1537c0
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_add.c
@@ -0,0 +1,86 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p32_add( posit32_t a, posit32_t b ){
+ union ui32_p32 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //Zero or infinity
+ if (uiA==0 || uiB==0){ // Not required but put here for speed
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = uiA | uiB;
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#else
+ uZ.ui = uiA | uiB;
+#endif
+ return uZ.p;
+ }
+ else if ( uiA==0x80000000 || uiB==0x80000000 ){
+ //printf("in infinity\n");
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_subMagsP32(uiA, uiB);
+ else
+ return softposit_addMagsP32(uiA, uiB);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_div.c b/source/luametatex/source/libraries/softposit/source/p32_div.c
new file mode 100644
index 000000000..a163a1412
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_div.c
@@ -0,0 +1,201 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include <stdlib.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p32_div( posit32_t pA, posit32_t pB )
+{
+ union ui32_p32 uA, uB, uZ;
+ uint_fast32_t uiA, uiB, fracA, fracB, regA, regime, regB, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0;
+ int_fast32_t expA;
+ uint_fast64_t frac64A, frac64Z, rem;
+ lldiv_t divresult;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+ frac64A = (uint64_t) fracA << 30;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA++;
+ while (!(tmp>>31)){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA -= tmp>>29;
+ fracB = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+
+ divresult = lldiv (frac64A,(uint_fast64_t)fracB);
+ frac64Z = divresult.quot;
+ rem = divresult.rem;
+
+ if (expA<0){
+ expA+=4;
+ kA--;
+ }
+ if (frac64Z!=0){
+ rcarry = frac64Z >> 30; // this is the hidden bit (14th bit) , extreme right bit is bit 0
+ if (!rcarry){
+ if (expA==0){
+ kA--;
+ expA=3;
+ }
+ else
+ expA--;
+ frac64Z<<=1;
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>30){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac64Z &= 0x3FFFFFFF;
+
+ fracA = (uint_fast32_t)frac64Z >> (regA+2);
+
+ if (regA<=28){
+ bitNPlusOne = (frac64Z >> (regA +1)) & 0x1;
+ expA<<= (28-regA);
+ if (bitNPlusOne) ( ((1<<(regA+1))-1) & frac64Z ) ? (bitsMore=1) : (bitsMore=0);
+ }
+ else {
+ if (regA==30){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==29){
+ bitNPlusOne = expA&0x1;
+ expA>>=1; //taken care of by the pack algo
+ }
+ if (frac64Z>0){
+ fracA=0;
+ bitsMore =1;
+ }
+
+ }
+ if (rem) bitsMore =1;
+
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ if (bitNPlusOne) uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_eq.c b/source/luametatex/source/libraries/softposit/source/p32_eq.c
new file mode 100644
index 000000000..3091227ef
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_eq.c
@@ -0,0 +1,60 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool p32_eq( posit32_t a, posit32_t b ) {
+ union ui32_p32 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA==uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_le.c b/source/luametatex/source/libraries/softposit/source/p32_le.c
new file mode 100644
index 000000000..add7edb77
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_le.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool p32_le( posit32_t a, posit32_t b ) {
+ union ui32_p32 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<=uiB)
+ return true;
+ else
+ return false;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/p32_lt.c b/source/luametatex/source/libraries/softposit/source/p32_lt.c
new file mode 100644
index 000000000..a6dfe6437
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_lt.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p32_lt( posit32_t a, posit32_t b ) {
+ union ui32_p32 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_mul.c b/source/luametatex/source/libraries/softposit/source/p32_mul.c
new file mode 100644
index 000000000..460d81aee
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_mul.c
@@ -0,0 +1,206 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit32_t p32_mul( posit32_t pA, posit32_t pB ){
+
+
+ union ui32_p32 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+ uint_fast32_t regA, fracA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA;
+ int_fast8_t kA=0;
+ uint_fast64_t frac64Z;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+ //NaR or Zero
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+
+ while (tmp>>31){
+
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<1) | 0x40000000) & 0x7FFFFFFF);
+
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+
+ rcarry = frac64Z>>61;//3rd bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64Z>>=1;
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+
+ if(regA>30){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position (2 bits exp, so + 1 than 16 bits)
+ frac64Z = (frac64Z&0xFFFFFFFFFFFFFFF) >> regA;
+ fracA = (uint_fast32_t) (frac64Z>>32);
+ if (regA<=28){
+ bitNPlusOne |= (0x80000000 & frac64Z);
+ expA<<= (28-regA);
+ }
+ else {
+ if (regA==30){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==29){
+ bitNPlusOne = expA&0x1;
+ expA>>=1; //taken care of by the pack algo
+ }
+ if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+
+ }
+ //sign is always zero
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7FFFFFFF & frac64Z) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_mulAdd.c b/source/luametatex/source/libraries/softposit/source/p32_mulAdd.c
new file mode 100644
index 000000000..1cee8a093
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_mulAdd.c
@@ -0,0 +1,56 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p32_mulAdd( posit32_t a, posit32_t b, posit32_t c ) {
+
+ union ui32_p32 uA;
+ uint_fast32_t uiA;
+ union ui32_p32 uB;
+ uint_fast32_t uiB;
+ union ui32_p32 uC;
+ uint_fast32_t uiC;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+ uC.p = c;
+ uiC = uC.ui;
+ return softposit_mulAddP32( uiA, uiB, uiC, 0 );
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_roundToInt.c b/source/luametatex/source/libraries/softposit/source/p32_roundToInt.c
new file mode 100644
index 000000000..f2e01194e
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_roundToInt.c
@@ -0,0 +1,105 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p32_roundToInt( posit32_t pA ){
+ union ui32_p32 uA;
+ uint_fast32_t mask = 0x20000000, scale=0, tmp=0, uiA, uiZ;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ sign = uiA>>31;
+
+ // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFFFFFF; // A is now |A|.
+ if (uiA <= 0x38000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ uA.ui = 0;
+ return uA.p;
+ }
+ else if (uiA < 0x44000000) { // 1/2 < x < 3/2 rounds to 1.
+ uA.ui = 0x40000000;
+ }
+ else if (uiA <= 0x4A000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ uA.ui = 0x48000000;
+ }
+ else if (uiA >= 0x7E800000) { // If |A| is 0x7E800000 (posit is pure integer value), leave it unchanged.
+ return uA.p; // This also takes care of the NaR case, 0x80000000.
+ }
+ else { // 34% of the cases, we have to decode the posit.
+
+ while (mask & uiA) {
+ scale += 4;
+ mask >>= 1;
+ }
+ mask >>= 1;
+
+ //Exponential (2 bits)
+ if (mask & uiA) scale+=2;
+ mask >>= 1;
+ if (mask & uiA) scale++;
+ mask >>= scale;
+
+ //the rest of the bits
+ bitLast = (uiA & mask);
+ mask >>= 1;
+ tmp = (uiA & mask);
+ bitNPlusOne = tmp;
+ uiA ^= tmp; // Erase the bit, if it was set.
+ tmp = uiA & (mask - 1); // this is actually bitsMore
+
+ uiA ^= tmp;
+
+ if (bitNPlusOne) {
+ if (bitLast | tmp) uiA += (mask << 1);
+ }
+ uA.ui = uiA;
+
+
+ }
+ if (sign) uA.ui = -uA.ui & 0xFFFFFFFF;
+ return uA.p;
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_sqrt.c b/source/luametatex/source/libraries/softposit/source/p32_sqrt.c
new file mode 100644
index 000000000..e02ec5fd1
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_sqrt.c
@@ -0,0 +1,137 @@
+
+/*============================================================================
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+extern const uint_fast16_t softposit_approxRecipSqrt0[];
+extern const uint_fast16_t softposit_approxRecipSqrt1[];
+
+
+
+posit32_t p32_sqrt( posit32_t pA ) {
+
+ union ui32_p32 uA;
+ uint_fast32_t index, r0, shift, fracA, expZ, expA;
+ uint_fast32_t mask, uiA, uiZ;
+ uint_fast64_t eSqrR0, fracZ, negRem, recipSqrt, shiftedFracZ, sigma0, sqrSigma0;
+ int_fast32_t eps, shiftZ;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ // If NaR or a negative number, return NaR.
+ if (uiA & 0x80000000) {
+ uA.ui = 0x80000000;
+ return uA.p;
+ }
+ // If the argument is zero, return zero.
+ else if (!uiA) {
+ return uA.p;
+ }
+ // Compute the square root; shiftZ is the power-of-2 scaling of the result.
+ // Decode regime and exponent; scale the input to be in the range 1 to 4:
+ if (uiA & 0x40000000) {
+ shiftZ = -2;
+ while (uiA & 0x40000000) {
+ shiftZ += 2;
+ uiA = (uiA << 1) & 0xFFFFFFFF;
+ }
+ } else {
+ shiftZ = 0;
+ while (!(uiA & 0x40000000)) {
+ shiftZ -= 2;
+ uiA = (uiA << 1) & 0xFFFFFFFF;
+ }
+ }
+
+ uiA &= 0x3FFFFFFF;
+ expA = (uiA >> 28);
+ shiftZ += (expA >> 1);
+ expA = (0x1 ^ (expA & 0x1));
+ uiA &= 0x0FFFFFFF;
+ fracA = (uiA | 0x10000000);
+
+ // Use table look-up of first 4 bits for piecewise linear approx. of 1/sqrt:
+ index = ((fracA >> 24) & 0xE) + expA;
+ eps = ((fracA >> 9) & 0xFFFF);
+ r0 = softposit_approxRecipSqrt0[index]
+ - (((uint_fast32_t) softposit_approxRecipSqrt1[index] * eps) >> 20);
+
+ // Use Newton-Raphson refinement to get 33 bits of accuracy for 1/sqrt:
+ eSqrR0 = (uint_fast64_t) r0 * r0;
+ if (!expA) eSqrR0 <<= 1;
+ sigma0 = 0xFFFFFFFF & (0xFFFFFFFF ^ ((eSqrR0 * (uint64_t)fracA) >> 20));
+ recipSqrt = ((uint_fast64_t) r0 << 20) + (((uint_fast64_t) r0 * sigma0) >> 21);
+
+ sqrSigma0 = ((sigma0 * sigma0) >> 35);
+ recipSqrt += ( (( recipSqrt + (recipSqrt >> 2) - ((uint_fast64_t)r0 << 19) ) * sqrSigma0) >> 46 );
+
+
+ fracZ = (((uint_fast64_t) fracA) * recipSqrt) >> 31;
+ if (expA) fracZ = (fracZ >> 1);
+
+ // Find the exponent of Z and encode the regime bits.
+ expZ = shiftZ & 0x3;
+ if (shiftZ < 0) {
+ shift = (-1 - shiftZ) >> 2;
+ uiZ = 0x20000000 >> shift;
+ } else {
+ shift = shiftZ >> 2;
+ uiZ = 0x7FFFFFFF - (0x3FFFFFFF >> shift);
+ }
+
+ // Trick for eliminating off-by-one cases that only uses one multiply:
+ fracZ++;
+ if (!(fracZ & 0xF)) {
+ shiftedFracZ = fracZ >> 1;
+ negRem = (shiftedFracZ * shiftedFracZ) & 0x1FFFFFFFF;
+ if (negRem & 0x100000000) {
+ fracZ |= 1;
+ } else {
+ if (negRem) fracZ--;
+ }
+ }
+ // Strip off the hidden bit and round-to-nearest using last shift+5 bits.
+ fracZ &= 0xFFFFFFFF;
+ mask = (1 << (4 + shift));
+ if (mask & fracZ) {
+ if ( ((mask - 1) & fracZ) | ((mask << 1) & fracZ) ) fracZ += (mask << 1);
+ }
+ // Assemble the result and return it.
+ uA.ui = uiZ | (expZ << (27 - shift)) | (fracZ >> (5 + shift));
+ return uA.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/p32_sub.c b/source/luametatex/source/libraries/softposit/source/p32_sub.c
new file mode 100644
index 000000000..3b5a0f5d9
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_sub.c
@@ -0,0 +1,84 @@
+
+/*============================================================================
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+posit32_t p32_sub( posit32_t a, posit32_t b ) {
+
+
+ union ui32_p32 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ //Zero
+ else if ( uiA==0 || uiB==0 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = (uiA | -uiB);
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = (uiA | -uiB);
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_addMagsP32(uiA, (-uiB & 0xFFFFFFFF));
+ else
+ return softposit_subMagsP32(uiA, (-uiB & 0xFFFFFFFF));
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_i32.c b/source/luametatex/source/libraries/softposit/source/p32_to_i32.c
new file mode 100644
index 000000000..c6306eecc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_i32.c
@@ -0,0 +1,102 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast32_t pX2_to_i32( posit_2_t pA ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_i32(p32);
+}
+int_fast32_t p32_to_i32( posit32_t pA ){
+
+ union ui32_p32 uA;
+ uint_fast64_t iZ64, mask, tmp;
+ int_fast32_t iZ;
+ uint_fast32_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, bitsMore, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000) return 0;
+
+ sign = uiA>>31;
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (uiA <= 0x38000000) return 0; // 0 <= |pA| <= 1/2 rounds to zero.
+ else if (uiA < 0x44000000) iZ = 1; // 1/2 < x < 3/2 rounds to 1.
+ else if (uiA <= 0x4A000000) iZ = 2; // 3/2 <= x <= 5/2 rounds to 2. // For speed. Can be commented out
+ //overflow so return max integer value
+ else if(uiA>0x7FAFFFFF) return (sign) ? (-2147483648) : (2147483647); //return INT_MAX
+ else{
+ uiA -= 0x40000000;
+ while (0x20000000 & uiA) {
+ scale += 4;
+ uiA = (uiA - 0x20000000) << 1;
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale+=2; // If first exponent bit is 1, increment the scale.
+ if (0x10000000 & uiA) scale++;
+ iZ64 = (((uint64_t)uiA | 0x10000000ULL)&0x1FFFFFFFULL) << 34; // Left-justify fraction in 32-bit result (one left bit padding)
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ64 & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ64 & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ64 ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ64 & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ64 ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ64 += (mask << 1);
+ }
+
+ iZ = (uint64_t)iZ64 >> (62 - scale); // Right-justify the integer.
+ }
+
+ if (sign){
+ iZ = (-iZ & 0xFFFFFFFF);
+ }
+ return iZ;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_i64.c b/source/luametatex/source/libraries/softposit/source/p32_to_i64.c
new file mode 100644
index 000000000..3fa9b81fd
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_i64.c
@@ -0,0 +1,107 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast64_t pX2_to_i64( posit_2_t pA ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_i64(p32);
+}
+
+int_fast64_t p32_to_i64( posit32_t pA ){
+
+ union ui32_p32 uA;
+ uint_fast64_t mask, tmp;
+ int_fast64_t iZ;
+ uint_fast32_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, bitsMore, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000) return 0;
+
+ sign = uiA>>31;
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (uiA <= 0x38000000) return 0; // 0 <= |pA| <= 1/2 rounds to zero.
+ else if (uiA < 0x44000000) iZ = 1; // 1/2 < x < 3/2 rounds to 1.
+ else if (uiA <= 0x4A000000) iZ = 2; // 3/2 <= x <= 5/2 rounds to 2.
+ //overflow so return max integer value
+ else if(uiA>0x7FFFAFFF) return (sign) ? (-9223372036854775808) : (0x7FFFFFFFFFFFFFFF);
+ else{
+ uiA -= 0x40000000;
+ while (0x20000000 & uiA) {
+ scale += 4;
+ uiA = (uiA - 0x20000000) << 1;
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale+=2; // If first exponent bit is 1, increment the scale.
+ if (0x10000000 & uiA) scale++;
+ iZ = ((uiA | 0x10000000ULL)&0x1FFFFFFFULL) << 34; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ if(scale<62){
+
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+ iZ = ((uint64_t)iZ) >> (62 - scale); // Right-justify the integer.
+ }
+ else if (scale>62)
+ iZ = (uint64_t)iZ << (scale-62);
+
+ }
+
+ if (sign) iZ = -iZ ;
+ return iZ;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_p16.c b/source/luametatex/source/libraries/softposit/source/p32_to_p16.c
new file mode 100644
index 000000000..f547aaef9
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_p16.c
@@ -0,0 +1,122 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t pX2_to_p16( posit_2_t pA ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_p16(p32);
+}
+
+posit16_t p32_to_p16( posit32_t pA ){
+
+ union ui32_p32 uA;
+ union ui16_p16 uZ;
+ uint_fast32_t uiA, tmp=0, exp_frac32A;
+ uint_fast16_t regime, exp_frac=0;
+ bool sign, regSA, bitsMore=0, bitNPlusOne=0;
+ int_fast16_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = (uint16_t)(uiA>>16);
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (uiA>0x7F600000) uZ.ui = 0x7FFF;
+ else if (uiA<0x00A00000) uZ.ui = 0x1;
+ else{
+ regSA = signregP32UI(uiA);
+
+ //regime
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ //exp and frac
+ exp_frac32A = tmp<<1;
+printBinary(&exp_frac32A, 32);
+printf("kA: %d\n", kA);
+ if(kA<0){
+ regA = (-kA)<<1;
+ if (exp_frac32A&0x80000000) regA--;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = (kA<<1)+1;
+ if (exp_frac32A&0x80000000) regA++;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+ if ((exp_frac32A>>(17+regA)) & 0x1) bitNPlusOne = 1;
+ if (regA<14) exp_frac = (uint16_t) (exp_frac32A>>(18+regA));
+
+ uZ.ui = regime + exp_frac;
+ if (bitNPlusOne){
+ if ((exp_frac32A<<(15-regA)) & 0xFFFFFFFF) bitsMore=1;
+ uZ.ui += (bitNPlusOne & (uZ.ui&1)) | ( bitNPlusOne & bitsMore);
+ }
+ }
+
+
+
+
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_p8.c b/source/luametatex/source/libraries/softposit/source/p32_to_p8.c
new file mode 100644
index 000000000..270545cf2
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_p8.c
@@ -0,0 +1,123 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t pX2_to_p8( posit_2_t pA ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_p8(p32);
+}
+
+posit8_t p32_to_p8( posit32_t pA ){
+
+ union ui32_p32 uA;
+ union ui8_p8 uZ;
+ uint_fast32_t uiA, tmp=0, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitsMore=0, bitNPlusOne=0;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = (uint8_t)(uiA>>24) &0xFF;
+ return uZ.p;
+ }
+
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (uiA>0x66000000) uZ.ui = 0x7F;
+ else if (uiA<0x1A000000) uZ.ui = 0x1;
+ else{
+ regSA = signregP32UI(uiA);
+ //regime
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ //2nd and 3rd bit exp
+ exp_frac32A = tmp;
+
+ if(kA<0){
+ regA = ((-kA)<<2) - (exp_frac32A>>29);
+
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = (regA>6) ? (0x1) : (0x40>>regA);
+
+ }
+ else{
+ (kA==0)?(regA=1 + (exp_frac32A>>29)): (regA = (kA<<2) + (exp_frac32A>>29) +1);
+ regSA=1;
+ regime = 0x7F - (0x7F>>regA);
+ }
+ exp_frac32A = (exp_frac32A<<3) &0xFFFFFFFF;
+ if (regA>5){
+ uZ.ui = regime;
+ }
+ else{
+ //exp_frac32A= ((exp_frac32A)&0x3F) >> shift; //first 2 bits already empty (for sign and regime terminating bit)
+ uZ.ui = regime | ( exp_frac32A>>(regA+26) );
+ }
+ if ( exp_frac32A & (0x2000000<<regA) ){
+ bitsMore = exp_frac32A & (0xFFFFFFFF>>(7-regA));
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_pX1.c b/source/luametatex/source/libraries/softposit/source/p32_to_pX1.c
new file mode 100644
index 000000000..ec74b6f56
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_pX1.c
@@ -0,0 +1,131 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit_1_t p32_to_pX1( posit32_t pA, int x ){
+
+ union ui32_p32 uA;
+ union ui32_pX1 uZ;
+ uint_fast32_t uiA, tmp, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitNPlusOne, bitsMore;
+ int_fast8_t kA=0, regA;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else {
+ regSA = signregP32UI(uiA);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ //exp and frac
+ exp_frac32A = tmp<<1;
+//printf("kA: %d\n", kA);
+//printBinary(&exp_frac32A, 32);
+ if(kA<0){
+ regA = (-kA)<<1;
+ if (exp_frac32A&0x80000000) regA--;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = (kA<<1)+1;
+ if (exp_frac32A&0x80000000) regA++;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ bitNPlusOne = (exp_frac32A >>(regA+33-x))&0x1;
+ bitsMore = exp_frac32A&(0x7FFFFFFF>>(x-regA-2));
+
+ if (regA<30) exp_frac32A >>=(2+regA);
+ else exp_frac32A=0;
+ uZ.ui = regime + (exp_frac32A & ((int32_t)0x80000000>>(x-1)) );
+
+ if (uZ.ui==0) uZ.ui = 0x1<<(32-x);
+ else if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+
+ }
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_pX2.c b/source/luametatex/source/libraries/softposit/source/p32_to_pX2.c
new file mode 100644
index 000000000..bce55adee
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_pX2.c
@@ -0,0 +1,92 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX2_to_pX2( posit_2_t pA, int x ){
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_pX2(p32, x);
+}
+posit_2_t p32_to_pX2( posit32_t pA, int x ){
+
+ union ui32_p32 uA;
+ union ui32_pX2 uZ;
+ uint_fast32_t uiA;
+ bool sign;
+
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else if (x==32 || (((uint32_t)0xFFFFFFFF>>x) & uiA)==0 ){
+ uZ.ui = uiA;
+ }
+ else {
+
+ int shift = 32-x;
+ if( (uiA>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uiA){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uiA) || (((uint32_t)0x7FFFFFFF>>x) & uiA) )
+ uiA += (0x1<<shift);
+ }
+ }
+ uZ.ui = uiA & ((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+
+ }
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_ui32.c b/source/luametatex/source/libraries/softposit/source/p32_to_ui32.c
new file mode 100644
index 000000000..d5c885a12
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_ui32.c
@@ -0,0 +1,108 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast32_t pX2_to_ui32( posit_2_t pA ) {
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_ui32(p32);
+}
+
+uint_fast32_t p32_to_ui32( posit32_t pA ) {
+
+ union ui32_p32 uA;
+ uint_fast64_t iZ64, mask, tmp;
+ uint_fast32_t iZ, scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ //NaR
+ //if (uiA==0x80000000) return 0;
+ //negative
+ if (uiA>=0x80000000) return 0;
+
+ if (uiA <= 0x38000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x44000000) { // 1/2 < x < 3/2 rounds to 1.
+ return 1;
+ }
+ else if (uiA <= 0x4A000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ return 2;
+ }
+ //overflow so return max integer value
+ else if(uiA>0x7FBFFFFF){
+ return 0xFFFFFFFF;
+ }
+ else {
+ uiA -= 0x40000000;
+ while (0x20000000 & uiA) {
+ scale += 4;
+ uiA = (uiA - 0x20000000) << 1;
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale+=2; // If first exponent bit is 1, increment the scale.
+ if (0x10000000 & uiA) scale++;
+ iZ64 = (((uint64_t)uiA | 0x10000000ULL)&0x1FFFFFFFULL) << 34; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ64 & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ64 & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ64 ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ64 & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ64 ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ64 += (mask << 1);
+ }
+ iZ = (uint64_t)iZ64 >> (62 - scale); // Right-justify the integer.
+ }
+
+ return iZ;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p32_to_ui64.c b/source/luametatex/source/libraries/softposit/source/p32_to_ui64.c
new file mode 100644
index 000000000..70c367a71
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p32_to_ui64.c
@@ -0,0 +1,117 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast64_t pX2_to_ui64( posit_2_t pA ) {
+ posit32_t p32 = {.v = pA.v};
+ return p32_to_ui64(p32);
+
+}
+
+uint_fast64_t p32_to_ui64( posit32_t pA ) {
+
+ union ui32_p32 uA;
+ uint_fast64_t mask, iZ, tmp;
+ uint_fast32_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, bitsMore;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ //NaR
+ //if (uiA==0x80000000) return 0;
+ //negative
+ if (uiA>=0x80000000) return 0;
+
+ if (uiA <= 0x38000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x44000000) { // 1/2 < x < 3/2 rounds to 1.
+ return 1;
+ }
+ else if (uiA <= 0x4A000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ return 2;
+ }
+ else if (uiA>0x7FFFBFFF){
+ return 0xFFFFFFFFFFFFFFFFULL;
+ }
+ else {
+
+ uiA -= 0x40000000;
+ while (0x20000000 & uiA) {
+ scale += 4;
+ uiA = (uiA - 0x20000000) << 1;
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale+=2; // If first exponent bit is 1, increment the scale.
+ if (0x10000000 & uiA) scale++;
+ iZ = (((uint64_t)uiA | 0x10000000ULL)&0x1FFFFFFFULL) << 34; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ if(scale<62){
+
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+ iZ = (uint64_t)iZ >> (62 - scale); // Right-justify the integer.
+ }
+ else if (scale>62){
+ iZ = (uint64_t)iZ << (scale-62);
+ }
+
+ }
+ return iZ;
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_add.c b/source/luametatex/source/libraries/softposit/source/p8_add.c
new file mode 100644
index 000000000..cc654031f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_add.c
@@ -0,0 +1,81 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_add( posit8_t a, posit8_t b )
+{
+ union ui8_p8 uA, uB;
+ uint_fast8_t uiA, uiB;
+ union ui8_p8 uZ;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //Zero or infinity
+ if (uiA==0 || uiB==0){ // Not required but put here for speed
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = uiA | uiB;
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#else
+ uZ.ui = uiA | uiB;
+#endif
+ return uZ.p;
+ }
+ else if ( uiA==0x80 || uiB==0x80 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80;
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>7)
+ return softposit_subMagsP8(uiA, uiB);
+ else
+ return softposit_addMagsP8(uiA, uiB);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_div.c b/source/luametatex/source/libraries/softposit/source/p8_div.c
new file mode 100644
index 000000000..1e3e5d791
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_div.c
@@ -0,0 +1,167 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <stdlib.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_div( posit8_t pA, posit8_t pB ) {
+ union ui8_p8 uA, uB, uZ;
+ uint_fast8_t uiA, uiB, fracA, fracB, regA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0;
+ uint_fast16_t frac16A, frac16Z, rem;
+ div_t divresult;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if ( uiA==0x80 || uiB==0x80 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP8UI( uiA );
+ signB = signP8UI( uiB );
+ signZ = signA ^ signB;
+ if(signA) uiA = (-uiA & 0xFF);
+ if(signB) uiB = (-uiB & 0xFF);
+ regSA = signregP8UI(uiA);
+ regSB = signregP8UI(uiB);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ fracA = (0x80 | tmp);
+ frac16A = fracA<<7; //hidden bit 2nd bit
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ fracB = (0x80 | tmp);
+ }
+ else{
+ kA++;
+ while (!(tmp>>7)){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ fracB = (0x80 | (0x7F & tmp));
+ }
+
+ divresult = div (frac16A,fracB);
+ frac16Z = divresult.quot;
+ rem = divresult.rem;
+
+ if (frac16Z!=0){
+ rcarry = frac16Z >> 7; // this is the hidden bit (7th bit) , extreme right bit is bit 0
+ if (!rcarry){
+ kA --;
+ frac16Z<<=1;
+ }
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFF);
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7F-(0x7F>>regA);
+ }
+ if(regA>6){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7F): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac16Z &=0x7F;
+ fracA = (uint_fast16_t)frac16Z >> (regA+1);
+
+ bitNPlusOne = (0x1 & (frac16Z>> regA)) ;
+ uZ.ui = packToP8UI(regime, fracA);
+
+ //uZ.ui = (uint16_t) (regime) + ((uint16_t) (expA)<< (13-regA)) + ((uint16_t)(fracA));
+ if (bitNPlusOne){
+ (((1<<regA)-1) & frac16Z) ? (bitsMore=1) : (bitsMore=0);
+ if (rem) bitsMore =1;
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (signZ) uZ.ui = -uZ.ui & 0xFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_eq.c b/source/luametatex/source/libraries/softposit/source/p8_eq.c
new file mode 100644
index 000000000..08df82be9
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_eq.c
@@ -0,0 +1,52 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p8_eq( posit8_t pA, posit8_t pB ){
+
+ union ui8_p8 uA, uB;
+ int8_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int8_t) uA.ui;
+ uB.p = pB;
+ uiB = (int8_t)uB.ui;
+
+ if (uiA==uiB)
+ return true;
+ else
+ return false;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/p8_le.c b/source/luametatex/source/libraries/softposit/source/p8_le.c
new file mode 100644
index 000000000..0234792f1
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_le.c
@@ -0,0 +1,52 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p8_le( posit8_t pA, posit8_t pB ) {
+ union ui8_p8 uA, uB;
+ int8_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int8_t) uA.ui;
+ uB.p = pB;
+ uiB = (int8_t)uB.ui;
+
+ if (uiA<=uiB)
+ return true;
+ else
+ return false;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_lt.c b/source/luametatex/source/libraries/softposit/source/p8_lt.c
new file mode 100644
index 000000000..c10d903de
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_lt.c
@@ -0,0 +1,53 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool p8_lt( posit8_t pA, posit8_t pB ) {
+ union ui8_p8 uA, uB;
+ int8_t uiA, uiB;
+
+ uA.p = pA;
+ uiA = (int8_t) uA.ui;
+ uB.p = pB;
+ uiB = (int8_t)uB.ui;
+
+ if (uiA<uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_mul.c b/source/luametatex/source/libraries/softposit/source/p8_mul.c
new file mode 100644
index 000000000..d565c7a75
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_mul.c
@@ -0,0 +1,167 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_mul( posit8_t pA, posit8_t pB ){
+
+ union ui8_p8 uA, uB, uZ;
+ uint_fast8_t uiA, uiB;
+ uint_fast8_t regA, fracA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0;
+ uint_fast16_t frac16Z;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+ //NaR or Zero
+ if ( uiA==0x80 || uiB==0x80 ){
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP8UI( uiA );
+ signB = signP8UI( uiB );
+ signZ = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFF);
+ if(signB) uiB = (-uiB & 0xFF);
+
+ regSA = signregP8UI(uiA);
+ regSB = signregP8UI(uiB);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ fracA = (0x80 | tmp);
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16Z = (uint_fast16_t) fracA * (0x80 | tmp);
+
+ rcarry = frac16Z>>15;//1st bit of frac32Z
+ if (rcarry){
+ kA++;
+ frac16Z>>=1;
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFF);
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7F-(0x7F>>regA);
+ }
+
+
+
+ if(regA>6){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7F): (uZ.ui=0x1);
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac16Z = (frac16Z&0x3FFF) >> regA;
+ fracA = (uint_fast8_t) (frac16Z>>8);
+ bitNPlusOne = (0x80 & frac16Z) ;
+ uZ.ui = packToP8UI(regime, fracA);
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7F & frac16Z) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFF;
+ return uZ.p;
+}
+
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_mulAdd.c b/source/luametatex/source/libraries/softposit/source/p8_mulAdd.c
new file mode 100644
index 000000000..e48a335e8
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_mulAdd.c
@@ -0,0 +1,56 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_mulAdd( posit8_t a, posit8_t b, posit8_t c )
+{
+ union ui8_p8 uA;
+ uint_fast8_t uiA;
+ union ui8_p8 uB;
+ uint_fast8_t uiB;
+ union ui8_p8 uC;
+ uint_fast8_t uiC;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+ uC.p = c;
+ uiC = uC.ui;
+ return softposit_mulAddP8( uiA, uiB, uiC, 0 );
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_roundToInt.c b/source/luametatex/source/libraries/softposit/source/p8_roundToInt.c
new file mode 100644
index 000000000..65e1be228
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_roundToInt.c
@@ -0,0 +1,95 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_roundToInt( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ uint_fast8_t mask = 0x20, scale=0, tmp=0, uiA;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ sign = (uiA > 0x80);
+
+ // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFF;
+ if (uiA <= 0x20) { // 0 <= |pA| <= 1/2 rounds to zero.
+ uA.ui = 0;
+ return uA.p;
+ }
+ else if (uiA < 0x50) { // 1/2 < x < 3/2 rounds to 1.
+ uA.ui = 0x40;
+ }
+ else if (uiA <= 0x64) { // 3/2 <= x <= 5/2 rounds to 2.
+ uA.ui = 0x60;
+ }
+ else if (uiA >= 0x78) { // If |A| is 8 or greater, leave it unchanged.
+ return uA.p; // This also takes care of the NaR case, 0x80.
+ }
+ else {
+ while (mask & uiA) {
+ scale += 1;
+ mask >>= 1;
+ }
+
+ mask >>= scale;
+ bitLast = (uiA & mask);
+
+ mask >>= 1;
+ tmp = (uiA & mask);
+ bitNPlusOne = tmp;
+ uiA ^= tmp;
+ tmp = uiA & (mask - 1); //bitsMore
+ uiA ^= tmp;
+
+ if (bitNPlusOne) {
+ if (bitLast | tmp) uiA += (mask << 1);
+ }
+ uA.ui = uiA;
+ }
+ if (sign) uA.ui = -uA.ui & 0xFF;
+ return uA.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_sqrt.c b/source/luametatex/source/libraries/softposit/source/p8_sqrt.c
new file mode 100644
index 000000000..32a289b8d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_sqrt.c
@@ -0,0 +1,63 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_sqrt( posit8_t pA ) {
+ union ui8_p8 uA;
+ uint_fast8_t uiA;
+
+ static const uint8_t p8Sqrt [] =
+ {0, 8, 11, 14, 16, 18, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31, 32,
+ 33, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 45, 46,
+ 47, 47, 48, 49, 49, 50, 51, 51, 52, 52, 53, 54, 54, 55, 55, 56, 57,
+ 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, 64, 64, 65, 65,
+ 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 70, 71, 71, 72, 72, 72, 73,
+ 73, 74, 74, 74, 75, 75, 75, 76, 76, 77, 77, 77, 79, 80, 81, 83, 84,
+ 85, 86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 100,
+ 101, 102, 103, 105, 108, 110, 112, 114, 115, 120};
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA>=0x80){
+ uA.ui = 0x80;
+ return uA.p;
+ }
+ uA.ui = p8Sqrt[uiA];
+
+ return uA.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_sub.c b/source/luametatex/source/libraries/softposit/source/p8_sub.c
new file mode 100644
index 000000000..31c05d874
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_sub.c
@@ -0,0 +1,88 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t p8_sub( posit8_t a, posit8_t b ){
+
+ union ui8_p8 uA, uB;
+ uint_fast8_t uiA, uiB;
+ union ui8_p8 uZ;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //infinity
+ if ( uiA==0x80 || uiB==0x80 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80;
+#endif
+ return uZ.p;
+ }
+ //Zero
+ else if ( uiA==0 || uiB==0 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = (uiA | -uiB);
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = (uiA | -uiB);
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if (signP8UI(uiA^uiB))
+ return softposit_addMagsP8(uiA, (-uiB & 0xFF));
+ else
+ return softposit_subMagsP8(uiA, (-uiB & 0xFF));
+
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_i32.c b/source/luametatex/source/libraries/softposit/source/p8_to_i32.c
new file mode 100644
index 000000000..f4aaebbac
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_i32.c
@@ -0,0 +1,96 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast32_t p8_to_i32( posit8_t pA ){
+ union ui8_p8 uA;
+ int_fast32_t mask, iZ, tmp;
+ uint_fast8_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ if (uiA==0x80) return 0;
+
+ sign = (uiA > 0x80); // sign is True if pA > NaR.
+
+ if (sign) uiA = -uiA & 0xFF; // A is now |A|.
+
+ if (uiA <= 0x20) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x50) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40; // Strip off first regime bit (which is a 1).
+ while (0x20 & uiA) { // Increment scale one for each regime sign bit.
+ scale ++; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+
+ iZ = ((uint32_t)uiA | 0x40) << 24; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ mask = 0x40000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+ iZ = (uint32_t)iZ >> (30 - scale); // Right-justify the integer.
+ }
+
+ if (sign) iZ = -iZ; // Apply the sign of the input.
+ return iZ;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_i64.c b/source/luametatex/source/libraries/softposit/source/p8_to_i64.c
new file mode 100644
index 000000000..70d85f95d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_i64.c
@@ -0,0 +1,97 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast64_t p8_to_i64( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ int_fast64_t mask, iZ, tmp;
+ uint_fast8_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ if (uiA==0x80) return 0;
+
+ sign = uiA>>7;
+ if (sign) uiA = -uiA & 0xFF;
+
+ if (uiA <= 0x20) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x50) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else { // Decode the posit, left-justifying as we go.
+
+ uiA -= 0x40; // Strip off first regime bit (which is a 1).
+ while (0x20 & uiA) { // Increment scale by 1 for each regime sign bit.
+ scale ++; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+
+ iZ = ((uint64_t)uiA | 0x40) << 55; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ mask = 0x2000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask<<1) ;
+ }
+ iZ = (uint64_t)iZ >> (61 - scale); // Right-justify the integer.
+ }
+
+ if (sign) iZ = -iZ; // Apply the sign of the input.
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_p16.c b/source/luametatex/source/libraries/softposit/source/p8_to_p16.c
new file mode 100644
index 000000000..56773e7dd
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_p16.c
@@ -0,0 +1,107 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit16_t p8_to_p16( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ union ui16_p16 uZ;
+ uint_fast8_t uiA, tmp;
+ uint_fast16_t exp_frac16A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ //NaR or zero
+ if (uiA==0x80 || uiA==0 ){
+ uZ.ui = (uint16_t)uiA<<8;
+ return uZ.p;
+ }
+
+ sign = signP8UI( uiA );
+
+ if (sign) uiA = -uiA & 0xFF;
+ regSA = signregP8UI(uiA);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ exp_frac16A = tmp<<8;
+
+ if(kA<0){
+ regA = -kA;
+ if (regA&0x1) exp_frac16A |= 0x8000;
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ if (kA&0x1) exp_frac16A |= 0x8000;
+ regA = (kA+2)>>1;
+ if (regA==0) regA=1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+
+ exp_frac16A >>=(regA+2); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac16A;
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_p32.c b/source/luametatex/source/libraries/softposit/source/p8_to_p32.c
new file mode 100644
index 000000000..adee50e9e
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_p32.c
@@ -0,0 +1,111 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit32_t p8_to_p32( posit8_t pA ) {
+
+
+ union ui8_p8 uA;
+ union ui32_p32 uZ;
+ uint_fast8_t uiA, tmp;
+ uint_fast32_t exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80 || uiA==0 ){
+ uZ.ui = (uint32_t)uiA<<24;
+ return uZ.p;
+ }
+
+ sign = signP8UI( uiA );
+
+ if (sign) uiA = -uiA & 0xFF;
+ regSA = signregP8UI(uiA);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ exp_frac32A = tmp<<22;
+
+ if(kA<0){
+ regA = -kA;
+ // Place exponent bits
+ exp_frac32A |= ( ((regA&0x1)| ((regA+1)&0x2))<<29 );
+
+ regA = (regA+3)>>2;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ( (kA&0x3) << 29 );
+
+ regA = (kA+4)>>2;
+ if (regA==0) regA=1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ exp_frac32A =((uint_fast32_t)exp_frac32A) >> (regA+1); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac32A;
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_pX1.c b/source/luametatex/source/libraries/softposit/source/p8_to_pX1.c
new file mode 100644
index 000000000..84ef367bc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_pX1.c
@@ -0,0 +1,130 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t p8_to_pX1( posit8_t pA, int x ) {
+
+ union ui8_p8 uA;
+ union ui32_pX1 uZ;
+ uint_fast8_t uiA, tmp;
+ uint_fast32_t exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80 || uiA==0 ){
+ uZ.ui = (uint32_t)uiA<<24;
+ return uZ.p;
+ }
+
+ sign = signP8UI( uiA );
+ if (sign) uiA = -uiA & 0xFF;
+
+ if(x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else{
+ regSA = signregP8UI(uiA);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ exp_frac32A = tmp<<24;
+
+ if(kA<0){
+ regA = -kA;
+ // Place exponent bits
+ if (regA&0x1) exp_frac32A |= 0x80000000;
+
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ if (kA&0x1) exp_frac32A |= 0x80000000;
+
+ regA = (kA+2)>>1;
+ if (regA==0) regA=1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ exp_frac32A >>=(regA+2); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac32A;
+
+ int shift = 32-x;
+
+ if( (uZ.ui>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uZ.ui){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uZ.ui) || (((uint32_t)0x7FFFFFFF>>x) & uZ.ui) )
+ uZ.ui += (0x1<<shift);
+ }
+ }
+
+ uZ.ui &=((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_pX2.c b/source/luametatex/source/libraries/softposit/source/p8_to_pX2.c
new file mode 100644
index 000000000..777235e8a
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_pX2.c
@@ -0,0 +1,128 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t p8_to_pX2( posit8_t pA, int x ) {
+
+ union ui8_p8 uA;
+ union ui32_pX2 uZ;
+ uint_fast8_t uiA, tmp;
+ uint_fast32_t exp_frac32A=0, regime;
+ bool sign, regSA;
+ int_fast8_t kA=0, regA;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80 || uiA==0 ){
+ uZ.ui = (uint32_t)uiA<<24;
+ return uZ.p;
+ }
+
+ sign = signP8UI( uiA );
+ if (sign) uiA = -uiA & 0xFF;
+ if(x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else{
+ regSA = signregP8UI(uiA);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ exp_frac32A = tmp<<22;
+
+ if(kA<0){
+ regA = -kA;
+ // Place exponent bits
+ exp_frac32A |= ( ((regA&0x1)| ((regA+1)&0x2))<<29 );
+
+ regA = (regA+3)>>2;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ( (kA&0x3) << 29 );
+
+ regA = (kA+4)>>2;
+ if (regA==0) regA=1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ exp_frac32A =((uint_fast32_t)exp_frac32A) >> (regA+1); //2 because of sign and regime terminating bit
+
+ uZ.ui = regime + exp_frac32A;
+
+ int shift = 32-x;
+ if( (uZ.ui>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uZ.ui){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uZ.ui) || (((uint32_t)0x7FFFFFFF>>x) & uZ.ui) )
+ uZ.ui += (0x1<<shift);
+ }
+ }
+ uZ.ui &=((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_ui32.c b/source/luametatex/source/libraries/softposit/source/p8_to_ui32.c
new file mode 100644
index 000000000..88a302dda
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_ui32.c
@@ -0,0 +1,93 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast32_t p8_to_ui32( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ uint_fast32_t mask, iZ, tmp;
+ uint_fast8_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ //if (uiA==0x80) return 0;
+ if (uiA>=0x80) return 0; //negative
+
+ if (uiA <= 0x20) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x50) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40; // Strip off first regime bit (which is a 1).
+ while (0x20 & uiA) { // Increment scale by 1 for each regime sign bit.
+ scale ++; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+
+ iZ = ((uint32_t)uiA | 0x40) << 24; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ mask = 0x40000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask<<1) ;
+ }
+ iZ = iZ >> (30 - scale); // Right-justify the integer.
+ }
+
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/p8_to_ui64.c b/source/luametatex/source/libraries/softposit/source/p8_to_ui64.c
new file mode 100644
index 000000000..2a19fe95f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/p8_to_ui64.c
@@ -0,0 +1,94 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast64_t p8_to_ui64( posit8_t pA ) {
+
+ union ui8_p8 uA;
+ uint_fast64_t mask, iZ, tmp;
+ uint_fast8_t scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ //if (uiA==0x80) return 0;
+ if (uiA>=0x80) return 0; //negative
+
+ if (uiA <= 0x20) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x50) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else { // Decode the posit, left-justifying as we go.
+
+ uiA -= 0x40; // Strip off first regime bit (which is a 1).
+ while (0x20 & uiA) { // Increment scale by 1 for each regime sign bit.
+ scale ++; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+
+ iZ = ((uint64_t)uiA | 0x40) << 55; // Left-justify fraction in 32-bit result (one left bit padding)
+
+ mask = 0x2000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask<<1) ;
+ }
+ iZ = (uint64_t)iZ >> (61 - scale); // Right-justify the integer.
+ }
+
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_add.c b/source/luametatex/source/libraries/softposit/source/pX1_add.c
new file mode 100644
index 000000000..55569e05c
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_add.c
@@ -0,0 +1,77 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_add( posit_1_t a, posit_1_t b, int x ){
+ union ui32_pX1 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if (uiA==0 || uiB==0){ // Not required but put here for speed
+ uZ.ui = uiA | uiB;
+ return uZ.p;
+ }
+ else if ( uiA==0x80000000 || uiB==0x80000000 ){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_subMagsPX1(uiA, uiB, x);
+ else
+ return softposit_addMagsPX1(uiA, uiB, x);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_div.c b/source/luametatex/source/libraries/softposit/source/pX1_div.c
new file mode 100644
index 000000000..cd68c9bd7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_div.c
@@ -0,0 +1,210 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include <stdlib.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_div( posit_1_t pA, posit_1_t pB, int x ) {
+
+ union ui32_pX1 uA, uB, uZ;
+ int regA;
+ uint_fast32_t uiA, uiB, fracA, fracB, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0;
+ int_fast32_t expA;
+ uint_fast64_t frac64A, frac64Z, rem;
+ lldiv_t divresult;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ if (x==2){
+ uZ.ui = 0x40000000;
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>30; //to get 1 bits
+ fracA = (tmp | 0x40000000) & 0x7FFFFFFF;
+ frac64A = (uint64_t) fracA << 30;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA++;
+ while (!(tmp>>31)){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA -= tmp>>30;
+ fracB = (tmp | 0x40000000) & 0x7FFFFFFF;
+
+ divresult = lldiv (frac64A,(uint_fast64_t)fracB);
+ frac64Z = divresult.quot;
+ rem = divresult.rem;
+
+ if (expA<0){
+ expA=1;
+ kA--;
+ }
+ if (frac64Z!=0){
+ rcarry = frac64Z >> 30; // this is the hidden bit (14th bit) , extreme right bit is bit 0
+ if (!rcarry){
+ if (expA==0) kA --;
+ expA^=1;
+ frac64Z<<=1;
+ }
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac64Z &= 0x3FFFFFFF;
+ fracA = (uint_fast32_t)frac64Z >> (regA+1);
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA!=(x-2)){
+ bitNPlusOne |= (((uint64_t)0x80000000>>(x-regA-1)) & frac64Z);
+ bitsMore = ((0x7FFFFFFF>>(x-regA-1)) & frac64Z);
+ fracA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if (frac64Z>0) {
+ fracA=0;
+ bitsMore=1;
+ }
+ if(regA==(x-2) && expA){
+ bitNPlusOne=1;
+ expA=0;
+ }
+ if (rem) bitsMore =1;
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+
+ expA <<= (29-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ if (bitNPlusOne) uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_eq.c b/source/luametatex/source/libraries/softposit/source/pX1_eq.c
new file mode 100644
index 000000000..05a7243b0
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_eq.c
@@ -0,0 +1,60 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2021. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX1_eq( posit_1_t a, posit_1_t b) {
+ union ui32_pX1 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA==uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_le.c b/source/luametatex/source/libraries/softposit/source/pX1_le.c
new file mode 100644
index 000000000..3986a7503
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_le.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2021 NGA. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX1_le( posit_1_t a, posit_1_t b ) {
+ union ui32_pX1 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<=uiB)
+ return true;
+ else
+ return false;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_lt.c b/source/luametatex/source/libraries/softposit/source/pX1_lt.c
new file mode 100644
index 000000000..565b449ac
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_lt.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2021 NGA. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX1_lt( posit_1_t a, posit_1_t b ) {
+ union ui32_pX1 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_mul.c b/source/luametatex/source/libraries/softposit/source/pX1_mul.c
new file mode 100644
index 000000000..fb5a71dd4
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_mul.c
@@ -0,0 +1,213 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit_1_t pX1_mul( posit_1_t pA, posit_1_t pB, int x ){
+
+ union ui32_pX1 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+ int regA;
+ uint_fast32_t fracA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA;
+ int_fast8_t kA=0;
+ uint_fast64_t frac64Z;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+ //NaR or Zero
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ if (x==2){
+ uZ.ui = (regSA&regSB) ? (0x40000000) : (0x0);
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ expA = tmp>>30; //to get 1 bits
+ fracA = (tmp | 0x40000000) & 0x7FFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ expA += tmp>>30;
+ frac64Z = (uint_fast64_t) fracA * ((tmp | 0x40000000) & 0x7FFFFFFF);
+ if (expA>1){
+ kA++;
+ expA^=0x2;
+ }
+ rcarry = frac64Z>>61;//3rd bit of frac64Z
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac64Z>>=1;
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ //remove carry and rcarry bits and shift to correct position (2 bits exp, so + 1 than 16 bits)
+ frac64Z = (frac64Z &0xFFFFFFFFFFFFFFF)>> (regA-1);
+ fracA = (uint_fast32_t) (frac64Z>>32);
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA!=(x-2)){
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>x) & frac64Z);
+ bitsMore = ((0x7FFFFFFFFFFFFFFF>>x) & frac64Z);
+ fracA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if (frac64Z>0){
+ fracA=0;
+ bitsMore=1;
+ }
+ if(regA==(x-2) && expA){
+ bitNPlusOne=1;
+ expA=0;
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+ expA <<= (29-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+ if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_mulAdd.c b/source/luametatex/source/libraries/softposit/source/pX1_mulAdd.c
new file mode 100644
index 000000000..196200f3f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_mulAdd.c
@@ -0,0 +1,56 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_mulAdd( posit_1_t a, posit_1_t b, posit_1_t c, int x ) {
+ //a*b + c
+ union ui32_pX1 uA;
+ uint_fast32_t uiA;
+ union ui32_pX1 uB;
+ uint_fast32_t uiB;
+ union ui32_pX1 uC;
+ uint_fast32_t uiC;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+ uC.p = c;
+ uiC = uC.ui;
+ return softposit_mulAddPX1( uiA, uiB, uiC, 0, x);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_roundToInt.c b/source/luametatex/source/libraries/softposit/source/pX1_roundToInt.c
new file mode 100644
index 000000000..96fe32d0f
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_roundToInt.c
@@ -0,0 +1,110 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_roundToInt( posit_1_t pA, int x ){
+ union ui32_pX1 uA;
+ uint_fast32_t mask = 0x20000000, scale=0, tmp=0, uiA, uiZ;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ sign = uiA>>31;
+
+ // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFFFFFF; // A is now |A|.
+ if (uiA <= 0x30000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ uA.ui = 0;
+ return uA.p;
+ }
+ else if (uiA < 0x48000000) { // 1/2 < x < 3/2 rounds to 1.
+ uA.ui = 0x40000000;
+ }
+ else if (uiA <= 0x54000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ uA.ui = 0x50000000;
+ }
+ else if (uiA >= 0x7FE80000) { // If |A| is 0x7FE800000 (4194304) (posit is pure integer value), leave it unchanged.
+ if (x>8) return uA.p; // This also takes care of the NaR case, 0x80000000.
+ else{
+ bitNPlusOne=((uint32_t)0x80000000>>x) & uiA;
+ tmp = ((uint32_t)0x7FFFFFFF>>x)& uiA; //bitsMore
+ bitLast = ((uint32_t)0x80000000>>(x-1)) & uiA;
+ if (bitNPlusOne)
+ if (bitLast | tmp) uiA += bitLast;
+ uA.ui = uiA;
+ }
+ }
+ else { // 34% of the cases, we have to decode the posit.
+
+ while (mask & uiA) {
+ scale += 2;
+ mask >>= 1;
+ }
+ mask >>= 1;
+ if (mask & uiA) scale++;
+
+ mask >>= scale;
+
+ //the rest of the bits
+ bitLast = (uiA & mask);
+ mask >>= 1;
+ tmp = (uiA & mask);
+ bitNPlusOne = tmp;
+ uiA ^= tmp; // Erase the bit, if it was set.
+ tmp = uiA & (mask - 1); // this is actually bitsMore
+
+ uiA ^= tmp;
+
+ if (bitNPlusOne) {
+ if (bitLast | tmp) uiA += (mask << 1);
+ }
+ uA.ui = uiA;
+
+
+ }
+ if (sign) uA.ui = -uA.ui & 0xFFFFFFFF;
+ return uA.p;
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_sub.c b/source/luametatex/source/libraries/softposit/source/pX1_sub.c
new file mode 100644
index 000000000..6a6c3ac95
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_sub.c
@@ -0,0 +1,88 @@
+
+/*============================================================================
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_sub( posit_1_t a, posit_1_t b, int x) {
+ union ui32_pX1 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ //Zero
+ else if ( uiA==0 || uiB==0 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = (uiA | -uiB);
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = (uiA | -uiB);
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_addMagsPX1(uiA, (-uiB & 0xFFFFFFFF), x);
+ else
+ return softposit_subMagsPX1(uiA, (-uiB & 0xFFFFFFFF), x);
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_i32.c b/source/luametatex/source/libraries/softposit/source/pX1_to_i32.c
new file mode 100644
index 000000000..58b5b7326
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_i32.c
@@ -0,0 +1,105 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast32_t pX1_to_i32( posit_1_t pA ) {
+
+ union ui32_pX1 uA;
+ uint_fast64_t iZ64, mask, tmp;
+ int_fast32_t iZ, scale = 0, uiA;
+ bool sign=0, bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ if (uiA==0x80000000) return 0;
+
+ sign = (uiA > 0x80000000); // sign is True if pA > NaR.
+
+ if (sign) uiA = -uiA & 0xFFFFFFFF; // A is now |A|.
+
+ if (uiA <= 0x30000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x48000000) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else if (uiA <= 0x54000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ iZ = 2;
+ }
+ else if (uiA>0x7FFF9FFF){ //2147418112
+ return (sign) ? (-2147483648) : (2147483647);
+
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40000000; // Strip off first regime bit (which is a 1).
+ while (0x20000000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20000000) << 1; // Remove the bit; line up the next regime bit.
+ }
+
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ64 = ((uint64_t)uiA | 0x20000000) << 33; // Left-justify fraction in 64-bit result (one left bit padding)
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ64 & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ64 & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ64 ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ64 & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ64 ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ64 += (mask << 1);
+ }
+
+ iZ = (uint64_t)iZ64 >> (62 - scale); // Right-justify the integer.
+
+ }
+ if (sign) iZ = -iZ;
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_i64.c b/source/luametatex/source/libraries/softposit/source/pX1_to_i64.c
new file mode 100644
index 000000000..990e8ef65
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_i64.c
@@ -0,0 +1,100 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+int_fast64_t pX1_to_i64( posit_1_t pA ) {
+
+ union ui32_pX1 uA;
+ int_fast64_t mask, tmp;
+ uint_fast64_t iZ, scale = 0, uiA;
+ bool sign=0, bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ if (uiA==0x80000000) return 0;
+
+ sign = (uiA > 0x80000000); // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFFFFFF; // A is now |A|.
+
+
+ if (uiA <= 0x30000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x48000000) { // 1/2 < x < 3/2 rounds to 1.
+ iZ = 1;
+ }
+ else if (uiA <= 0x54000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ iZ = 2;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40000000; // Strip off first regime bit (which is a 1).
+ while (0x20000000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20000000) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ = ((uint64_t)uiA | 0x20000000) << 33; // Left-justify fraction in 64-bit result (one left bit padding)
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+
+ iZ = (uint64_t)iZ >> (62 - scale); // Right-justify the integer.
+
+ }
+ if (sign) iZ = -iZ;
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_p16.c b/source/luametatex/source/libraries/softposit/source/pX1_to_p16.c
new file mode 100644
index 000000000..4fbf7df3a
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_p16.c
@@ -0,0 +1,78 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit16_t pX1_to_p16( posit_1_t pA ){
+
+ union ui32_pX1 uA;
+ union ui16_p16 uZ;
+ uint_fast32_t uiA;
+ bool sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA>>16;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if ((uiA&0xFFFF)==0 ){
+ uZ.ui = uiA>>16;
+ }
+ else {
+ if( (uiA>>16)!=0x7FFF ){
+ if( (uint32_t)0x8000 & uiA){
+ if ( ( ((uint32_t)0x10000) & uiA) || (((uint32_t)0x7FFF) & uiA) )
+ uiA += 0x10000;
+ }
+ }
+ uZ.ui = uiA>>16;
+ if (uZ.ui==0) uZ.ui = 0x1;
+
+ }
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_p32.c b/source/luametatex/source/libraries/softposit/source/pX1_to_p32.c
new file mode 100644
index 000000000..774249c6b
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_p32.c
@@ -0,0 +1,113 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit32_t pX1_to_p32( posit_1_t pA ){
+
+ union ui32_pX1 uA;
+ union ui32_p32 uZ;
+ uint_fast32_t uiA, tmp, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+ regSA = signregP32UI(uiA);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+
+ //2nd bit exp
+ exp_frac32A = tmp;
+
+ if(kA<0){
+ regA = -kA;
+ exp_frac32A |= ((uint32_t)(regA&0x1)<<31);
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ((uint32_t)(kA&0x1)<<31);
+ (kA==0) ? (regA=1) : (regA = (kA+2)>>1);
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ bitNPlusOne = (exp_frac32A >>(regA+1))&0x1;
+ bitsMore = exp_frac32A&(0x7FFFFFFF>>(31-regA));
+
+ exp_frac32A >>=(regA+2); //2 because of sign and regime terminating bit
+ uZ.ui = regime + exp_frac32A;
+
+ if (bitNPlusOne){
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_p8.c b/source/luametatex/source/libraries/softposit/source/pX1_to_p8.c
new file mode 100644
index 000000000..a912cc7ce
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_p8.c
@@ -0,0 +1,118 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit8_t pX1_to_p8( posit_1_t pA ){
+
+ union ui32_pX1 uA;
+ union ui8_p8 uZ;
+ uint_fast32_t uiA, tmp, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitsMore=0;
+ int_fast8_t kA=0, regA;
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = (uiA>>24) & 0xFF;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+ regSA = signregP32UI(uiA);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+
+ if (kA<-3 || kA>=3){
+ (kA<0) ? (uZ.ui=0x1):(uZ.ui= 0x7F);
+ }
+ else{
+ //2nd bit exp
+ exp_frac32A = tmp;
+ if(kA<0){
+ regA = ((-kA)<<1) - (exp_frac32A>>30);
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+
+ (kA==0)?(regA=1 + (exp_frac32A>>30)): (regA = ((kA+1)<<1) + (exp_frac32A>>30) -1);
+ regSA=1;
+ regime = 0x7F - (0x7F>>regA);
+ }
+
+ if (regA>5){
+ uZ.ui = regime;
+ }
+ else{
+ uZ.ui = regime + ( ((exp_frac32A)&0x3FFFFFFF)>>(regA+24) );
+ }
+ }
+
+ if ( exp_frac32A & (0x800000<<regA)){
+ bitsMore = exp_frac32A & ((0x800000<<regA)-1);
+ uZ.ui += (uZ.ui&1) | bitsMore;
+
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_pX1.c b/source/luametatex/source/libraries/softposit/source/pX1_to_pX1.c
new file mode 100644
index 000000000..51cba8225
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_pX1.c
@@ -0,0 +1,88 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX1_to_pX1( posit_1_t pA, int x ){
+
+ union ui32_pX1 uA;
+ union ui32_pX1 uZ;
+ uint_fast32_t uiA;
+ bool sign;
+
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else if (x==32 || (((uint32_t)0xFFFFFFFF>>x) & uiA)==0 ){
+ uZ.ui = uiA;
+ }
+ else {
+
+ int shift = 32-x;
+ if( (uiA>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uiA){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uiA) || (((uint32_t)0x7FFFFFFF>>x) & uiA) )
+ uiA += (0x1<<shift);
+ }
+ }
+ uZ.ui = uiA & ((int32_t)0x80000000>>(x-1));
+ if (uZ.ui==0) uZ.ui = 0x1<<shift;
+
+ }
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_pX2.c b/source/luametatex/source/libraries/softposit/source/pX1_to_pX2.c
new file mode 100644
index 000000000..d608f499c
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_pX2.c
@@ -0,0 +1,145 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX1_to_pX2( posit_1_t pA, int x ){
+
+ union ui32_pX1 uA;
+ union ui32_pX2 uZ;
+ uint_fast32_t uiA, tmp, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitNPlusOne, bitsMore;
+ int_fast8_t kA=0, regA;
+
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+ regSA = signregP32UI(uiA);
+
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ /* else if (x==32 || (((uint32_t)0xFFFFFFFF>>x) & uiA)==0 ){
+ uZ.ui = uiA;
+ }*/
+ else {
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ //2nd bit exp
+ exp_frac32A = tmp;
+
+ if(kA<0){
+ regA = -kA;
+ exp_frac32A |= ((uint32_t)(regA&0x1)<<31);
+ regA = (regA+1)>>1;
+ if (regA==0) regA=1;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ exp_frac32A |= ((uint32_t)(kA&0x1)<<31);
+ (kA==0) ? (regA=1) : (regA = (kA+2)>>1);
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+//printBinary(&exp_frac32A, 32);
+//uint32_t temp = (0x7FFFFFFF>>(x-regA-2));
+//printBinary(&temp, 32);
+//printBinary(&regime, 32);
+ bitNPlusOne = (exp_frac32A >>(regA+33-x))&0x1;
+ bitsMore = exp_frac32A&(0x7FFFFFFF>>(x-regA-2));
+//printf("bitNPlusOne: %d bitsMore: %d\n", bitNPlusOne, bitsMore);
+ exp_frac32A >>= (regA+2); //2 because of sign and regime terminating bit
+ uZ.ui = regime + (exp_frac32A & ((int32_t)0x80000000>>(x-1)) );
+//printBinary(&uZ.ui, 32);
+ //int shift = 32-x;
+ /*if( (uiA>>shift)!=(0x7FFFFFFF>>shift) ){
+ if( ((uint32_t)0x80000000>>x) & uZ.ui){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uZ.ui) || (((uint32_t)0x7FFFFFFF>>x) & uZ.ui) )
+ uZ.ui += (0x1<<shift);
+ }
+ }
+
+ uZ.ui &=((int32_t)0x80000000>>(x-1));*/
+ if (uZ.ui==0) uZ.ui = (uint32_t)0x1<<(32-x);
+ else if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+
+ }
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_ui32.c b/source/luametatex/source/libraries/softposit/source/pX1_to_ui32.c
new file mode 100644
index 000000000..428038701
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_ui32.c
@@ -0,0 +1,100 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast32_t pX1_to_ui32( posit_1_t pA ) {
+
+ union ui32_pX1 uA;
+ uint_fast64_t iZ64, mask, tmp;
+ uint_fast32_t iZ, scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ //if (uiA==0x80000000) return 0;
+ if (uiA>=0x80000000) return 0; //negative
+
+ if (uiA <= 0x30000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x48000000) { // 1/2 < x < 3/2 rounds to 1.
+ return 1;
+ }
+ else if (uiA <= 0x54000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ return 2;
+ }
+ else if (uiA>0x7FFFBFFF){ //4294836223
+ return 4294967295;
+
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40000000; // Strip off first regime bit (which is a 1).
+ while (0x20000000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20000000) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ64 = (uint64_t)(uiA | 0x20000000) << 33; // Left-justify fraction in 64-bit result (one left bit padding)
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ64 & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ64 & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ64 ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ64 & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ64 ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ64 += (mask << 1);
+ }
+ iZ = (uint64_t)iZ64 >> (62 - scale); // Right-justify the integer.
+
+ }
+
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX1_to_ui64.c b/source/luametatex/source/libraries/softposit/source/pX1_to_ui64.c
new file mode 100644
index 000000000..5679c7ea2
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX1_to_ui64.c
@@ -0,0 +1,97 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+uint_fast64_t pX1_to_ui64( posit_1_t pA ) {
+
+ union ui32_pX1 uA;
+ uint_fast64_t mask, tmp;
+ uint_fast64_t iZ, scale = 0, uiA;
+ bool bitLast, bitNPlusOne;
+
+ uA.p = pA;
+ uiA = uA.ui; // Copy of the input.
+ //NaR
+ //if (uiA==0x80000000) return 0;
+ if (uiA>=0x80000000) return 0; //negative
+
+ if (uiA <= 0x30000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ return 0;
+ }
+ else if (uiA < 0x48000000) { // 1/2 < x < 3/2 rounds to 1.
+ return 1;
+ }
+ else if (uiA <= 0x54000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ return 2;
+ }
+ else { // Decode the posit, left-justifying as we go.
+ uiA -= 0x40000000; // Strip off first regime bit (which is a 1).
+ while (0x20000000 & uiA) { // Increment scale by 2 for each regime sign bit.
+ scale += 2; // Regime sign bit is always 1 in this range.
+ uiA = (uiA - 0x20000000) << 1; // Remove the bit; line up the next regime bit.
+ }
+ uiA <<= 1; // Skip over termination bit, which is 0.
+ if (0x20000000 & uiA) scale++; // If exponent is 1, increment the scale.
+ iZ = (uiA | 0x20000000) << 33; // Left-justify fraction in 64-bit result (one left bit padding)
+ mask = 0x4000000000000000 >> scale; // Point to the last bit of the integer part.
+
+ bitLast = (iZ & mask); // Extract the bit, without shifting it.
+ mask >>= 1;
+ tmp = (iZ & mask);
+ bitNPlusOne = tmp; // "True" if nonzero.
+ iZ ^= tmp; // Erase the bit, if it was set.
+ tmp = iZ & (mask - 1); // tmp has any remaining bits. // This is bitsMore
+ iZ ^= tmp; // Erase those bits, if any were set.
+
+ if (bitNPlusOne) { // logic for round to nearest, tie to even
+ if (bitLast | tmp) iZ += (mask << 1);
+ }
+
+ iZ = (uint64_t)iZ >> (62 - scale); // Right-justify the integer.
+
+ }
+
+ return iZ;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_add.c b/source/luametatex/source/libraries/softposit/source/pX2_add.c
new file mode 100644
index 000000000..1aaed5d09
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_add.c
@@ -0,0 +1,77 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX2_add( posit_2_t a, posit_2_t b, int x ){
+ union ui32_pX2 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if (uiA==0 || uiB==0){ // Not required but put here for speed
+ uZ.ui = uiA | uiB;
+ return uZ.p;
+ }
+ else if ( uiA==0x80000000 || uiB==0x80000000 ){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_subMagsPX2(uiA, uiB, x);
+ else
+ return softposit_addMagsPX2(uiA, uiB, x);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_div.c b/source/luametatex/source/libraries/softposit/source/pX2_div.c
new file mode 100644
index 000000000..e6b666463
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_div.c
@@ -0,0 +1,218 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include <stdlib.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX2_div( posit_2_t pA, posit_2_t pB, int x ) {
+ union ui32_pX2 uA, uB, uZ;
+ int regA, regB;
+ uint_fast32_t uiA, uiB, fracA, fracB, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0;
+ int_fast32_t expA;
+ uint_fast64_t frac64A, frac64Z, rem;
+ lldiv_t divresult;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //Zero or infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ if (x==2){
+ uZ.ui = 0x40000000;
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+ frac64A = (uint64_t) fracA << 30;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA++;
+ while (!(tmp>>31)){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA -= tmp>>29;
+ fracB = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+
+ divresult = lldiv (frac64A,(uint_fast64_t)fracB);
+ frac64Z = divresult.quot;
+ rem = divresult.rem;
+
+ if (expA<0){
+ expA+=4;
+ kA--;
+ }
+ if (frac64Z!=0){
+ rcarry = frac64Z >> 30; // this is the hidden bit (14th bit) , extreme right bit is bit 0
+ if (!rcarry){
+ if (expA==0){
+ kA--;
+ expA=3;
+ }
+ else
+ expA--;
+ frac64Z<<=1;
+ }
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position
+ frac64Z &= 0x3FFFFFFF;
+ fracA = (uint_fast32_t)frac64Z >> (regA+2);
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+ bitNPlusOne=((uint32_t)0x80000000>>(x-regA-2))& frac64Z;
+ bitsMore = ((0x7FFFFFFF>>(x-regA-2)) & frac64Z);
+ fracA&=((int32_t)0x80000000>>(x-1));
+ }
+ else {
+ if (regA==(x-2)){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==(x-3)){
+ bitNPlusOne = expA&0x1;
+ expA &=0x2;
+ }
+ if (frac64Z>0){
+ fracA=0;
+ bitsMore =1;
+ }
+
+ }
+ if (rem) bitsMore =1;
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+
+ expA <<= (28-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ if (bitNPlusOne) uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_eq.c b/source/luametatex/source/libraries/softposit/source/pX2_eq.c
new file mode 100644
index 000000000..733e3a35d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_eq.c
@@ -0,0 +1,60 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX2_eq( posit_2_t a, posit_2_t b) {
+ union ui32_pX2 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA==uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_le.c b/source/luametatex/source/libraries/softposit/source/pX2_le.c
new file mode 100644
index 000000000..a60e8ad57
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_le.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX2_le( posit_2_t a, posit_2_t b ) {
+ union ui32_pX2 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<=uiB)
+ return true;
+ else
+ return false;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_lt.c b/source/luametatex/source/libraries/softposit/source/pX2_lt.c
new file mode 100644
index 000000000..1c1ff44e7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_lt.c
@@ -0,0 +1,59 @@
+
+/*============================================================================
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+bool pX2_lt( posit_2_t a, posit_2_t b ) {
+ union ui32_pX2 uA, uB;
+ int32_t uiA, uiB;
+
+ uA.p = a;
+ uiA = (int32_t) uA.ui;
+ uB.p = b;
+ uiB = (int32_t)uB.ui;
+
+ if(uiA<uiB)
+ return true;
+ else
+ return false;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_mul.c b/source/luametatex/source/libraries/softposit/source/pX2_mul.c
new file mode 100644
index 000000000..e76d568d6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_mul.c
@@ -0,0 +1,228 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit_2_t pX2_mul( posit_2_t pA, posit_2_t pB, int x ){
+
+ union ui32_pX2 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+ int regA;
+ uint_fast32_t fracA, regime, tmp;
+ bool signA, signB, signZ, regSA, regSB, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA;
+ int_fast8_t kA=0;
+ uint_fast64_t frac64Z;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+ //NaR or Zero
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+#ifdef SOFTPOSIT_EXACT
+
+ uZ.ui.v = 0;
+ if ( (uiA==0 && uiA.ui.exact) || (uiB==0 && uiB.ui.exact) )
+ uZ.ui.exact = 1;
+ else
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0;
+#endif
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ if (x==2){
+ uZ.ui = (regSA&regSB) ? (0x40000000) : (0x0);
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ //tmpX--;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<1) | 0x40000000) & 0x7FFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<1) | 0x40000000) & 0x7FFFFFFF);
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+
+ rcarry = frac64Z>>61;//3rd bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64Z>>=1;
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove carry and rcarry bits and shift to correct position (2 bits exp, so + 1 than 16 bits)
+ frac64Z = (frac64Z&0xFFFFFFFFFFFFFFF) >> regA;
+ fracA = (uint_fast32_t) (frac64Z>>32);
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>x) & frac64Z);
+ bitsMore = ((0x7FFFFFFFFFFFFFFF>>x) & frac64Z);
+ fracA&=((int32_t)0x80000000>>(x-1));
+ }
+ else {
+ if (regA==(x-2)){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==(x-3)){
+ bitNPlusOne = expA&0x1;
+ //expA>>=1; //taken care of by the pack algo
+ expA &=0x2;
+ }
+
+ if (frac64Z>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+
+ expA <<= (28-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+ if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_mulAdd.c b/source/luametatex/source/libraries/softposit/source/pX2_mulAdd.c
new file mode 100644
index 000000000..6090bc7e3
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_mulAdd.c
@@ -0,0 +1,56 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX2_mulAdd( posit_2_t a, posit_2_t b, posit_2_t c, int x ) {
+ //a*b + c
+ union ui32_pX2 uA;
+ uint_fast32_t uiA;
+ union ui32_pX2 uB;
+ uint_fast32_t uiB;
+ union ui32_pX2 uC;
+ uint_fast32_t uiC;
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+ uC.p = c;
+ uiC = uC.ui;
+ return softposit_mulAddPX2( uiA, uiB, uiC, 0, x);
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_roundToInt.c b/source/luametatex/source/libraries/softposit/source/pX2_roundToInt.c
new file mode 100644
index 000000000..aaa747344
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_roundToInt.c
@@ -0,0 +1,113 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t pX2_roundToInt( posit_2_t pA, int x ){
+ union ui32_pX2 uA;
+ uint_fast32_t mask = 0x20000000, scale=0, tmp=0, uiA, uiZ;
+ bool bitLast, bitNPlusOne, sign;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ sign = uiA>>31;
+
+ // sign is True if pA > NaR.
+ if (sign) uiA = -uiA & 0xFFFFFFFF; // A is now |A|.
+ if (uiA <= 0x38000000) { // 0 <= |pA| <= 1/2 rounds to zero.
+ uA.ui = 0;
+ return uA.p;
+ }
+ else if (uiA < 0x44000000) { // 1/2 < x < 3/2 rounds to 1.
+ uA.ui = 0x40000000;
+ }
+ else if (uiA <= 0x4A000000) { // 3/2 <= x <= 5/2 rounds to 2.
+ uA.ui = (x>4) ? ( 0x48000000 ) : (0x40000000);
+ }
+ else if (uiA >= 0x7E800000) { // If |A| is 0x7E800000 (4194304) (posit is pure integer value), leave it unchanged.
+ if (x>8) return uA.p; // This also takes care of the NaR case, 0x80000000.
+ else{
+ bitNPlusOne=((uint32_t)0x80000000>>x) & uiA;
+ tmp = ((uint32_t)0x7FFFFFFF>>x)& uiA; //bitsMore
+ bitLast = ((uint32_t)0x80000000>>(x-1)) & uiA;
+ if (bitNPlusOne)
+ if (bitLast | tmp) uiA += bitLast;
+ uA.ui = uiA;
+ }
+ }
+ else { // 34% of the cases, we have to decode the posit.
+
+ while (mask & uiA) {
+ scale += 4;
+ mask >>= 1;
+ }
+ mask >>= 1;
+
+ //Exponential (2 bits)
+ if (mask & uiA) scale+=2;
+ mask >>= 1;
+ if (mask & uiA) scale++;
+ mask >>= scale;
+
+ //the rest of the bits
+ bitLast = (uiA & mask);
+ mask >>= 1;
+ tmp = (uiA & mask);
+ bitNPlusOne = tmp;
+ uiA ^= tmp; // Erase the bit, if it was set.
+ tmp = uiA & (mask - 1); // this is actually bitsMore
+
+ uiA ^= tmp;
+
+ if (bitNPlusOne) {
+ if (bitLast | tmp) uiA += (mask << 1);
+ }
+ uA.ui = uiA;
+
+
+ }
+ if (sign) uA.ui = -uA.ui & 0xFFFFFFFF;
+ return uA.p;
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_sqrt.c b/source/luametatex/source/libraries/softposit/source/pX2_sqrt.c
new file mode 100644
index 000000000..f001806c6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_sqrt.c
@@ -0,0 +1,154 @@
+
+/*============================================================================
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+extern const uint_fast16_t softposit_approxRecipSqrt0[];
+extern const uint_fast16_t softposit_approxRecipSqrt1[];
+
+
+
+posit_2_t pX2_sqrt( posit_2_t pA, int x ) {
+ union ui32_pX2 uA;
+ uint_fast32_t index, r0, shift, fracA, expZ, expA;
+ uint_fast32_t mask, uiA, uiZ;
+ uint_fast64_t eSqrR0, frac64Z, negRem, recipSqrt, shiftedFracZ, sigma0, sqrSigma0;
+ int_fast32_t eps, shiftZ;
+
+ if (x<2 || x>32){
+ uA.ui = 0x80000000;
+ return uA.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ // If NaR or a negative number, return NaR.
+ if (uiA & 0x80000000) {
+ uA.ui = 0x80000000;
+ return uA.p;
+ }
+ // If the argument is zero, return zero.
+ else if (!uiA) {
+ return uA.p;
+ }
+ // Compute the square root; shiftZ is the power-of-2 scaling of the result.
+ // Decode regime and exponent; scale the input to be in the range 1 to 4:
+ if (uiA & 0x40000000) {
+ shiftZ = -2;
+ while (uiA & 0x40000000) {
+ shiftZ += 2;
+ uiA = (uiA << 1) & 0xFFFFFFFF;
+ }
+ } else {
+ shiftZ = 0;
+ while (!(uiA & 0x40000000)) {
+ shiftZ -= 2;
+ uiA = (uiA << 1) & 0xFFFFFFFF;
+ }
+ }
+
+ uiA &= 0x3FFFFFFF;
+ expA = (uiA >> 28);
+ shiftZ += (expA >> 1);
+ expA = (0x1 ^ (expA & 0x1));
+ uiA &= 0x0FFFFFFF;
+ fracA = (uiA | 0x10000000);
+
+ // Use table look-up of first 4 bits for piecewise linear approx. of 1/sqrt:
+ index = ((fracA >> 24) & 0xE) + expA;
+ eps = ((fracA >> 9) & 0xFFFF);
+ r0 = softposit_approxRecipSqrt0[index]
+ - (((uint_fast32_t) softposit_approxRecipSqrt1[index] * eps) >> 20);
+
+ // Use Newton-Raphson refinement to get 33 bits of accuracy for 1/sqrt:
+ eSqrR0 = (uint_fast64_t) r0 * r0;
+ if (!expA) eSqrR0 <<= 1;
+ sigma0 = 0xFFFFFFFF & (0xFFFFFFFF ^ ((eSqrR0 * (uint64_t)fracA) >> 20));
+ recipSqrt = ((uint_fast64_t) r0 << 20) + (((uint_fast64_t) r0 * sigma0) >> 21);
+
+ sqrSigma0 = ((sigma0 * sigma0) >> 35);
+ recipSqrt += ( (( recipSqrt + (recipSqrt >> 2) - ((uint_fast64_t)r0 << 19) ) * sqrSigma0) >> 46 );
+
+
+ frac64Z = (((uint_fast64_t) fracA) * recipSqrt) >> 31;
+ if (expA) frac64Z = (frac64Z >> 1);
+
+ // Find the exponent of Z and encode the regime bits.
+ expZ = shiftZ & 0x3;
+ if (shiftZ < 0) {
+ shift = (-1 - shiftZ) >> 2;
+ uiZ = 0x20000000 >> shift;
+ } else {
+ shift = shiftZ >> 2;
+ uiZ = 0x7FFFFFFF - (0x3FFFFFFF >> shift);
+ }
+
+ // Trick for eliminating off-by-one cases that only uses one multiply:
+ frac64Z++;
+ if (!(frac64Z & 0xF)) {
+ shiftedFracZ = frac64Z >> 1;
+ negRem = (shiftedFracZ * shiftedFracZ) & 0x1FFFFFFFF;
+ if (negRem & 0x100000000) {
+ frac64Z |= 1;
+ } else {
+ if (negRem) frac64Z--;
+ }
+ }
+ // Strip off the hidden bit and round-to-nearest using last shift+5 bits.
+ frac64Z &= 0xFFFFFFFF;
+ mask = (1 << (36 + shift - x));
+ if (mask & frac64Z) {
+ if ( ((mask - 1) & frac64Z) | ((mask << 1) & frac64Z) )
+ frac64Z+=(mask << 1) ;
+ // Assemble the result and return it.
+ uA.ui = uiZ | (expZ << (27 - shift)) | (frac64Z >> (5 + shift));
+ }
+ else{
+ // Assemble the result and return it.
+ uA.ui = uiZ | (expZ << (27 - shift)) | (frac64Z >> (5 + shift));
+ //Check if rounding bits in regime or exp and clean off unwanted bits
+ if( ((uint32_t)0x80000000>>x) & uA.ui){
+ if ( ( ((uint32_t)0x80000000>>(x-1)) & uA.ui) || (((uint32_t)0x7FFFFFFF>>x) & uA.ui) )
+ uA.ui = (uA.ui & ((int32_t)0x80000000>>(x-1))) + ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+
+
+ uA.ui &=((int32_t)0x80000000>>(x-1));
+ return uA.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_sub.c b/source/luametatex/source/libraries/softposit/source/pX2_sub.c
new file mode 100644
index 000000000..49246ac6d
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_sub.c
@@ -0,0 +1,87 @@
+
+/*============================================================================
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+posit_2_t pX2_sub( posit_2_t a, posit_2_t b, int x) {
+ union ui32_pX2 uA, uB, uZ;
+ uint_fast32_t uiA, uiB;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = a;
+ uiA = uA.ui;
+ uB.p = b;
+ uiB = uB.ui;
+
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.exact = (uiA.ui.exact & uiB.ui.exact);
+#endif
+
+ //infinity
+ if ( uiA==0x80000000 || uiB==0x80000000 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = 0x80000000;
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = 0x80000000;
+#endif
+ return uZ.p;
+ }
+ //Zero
+ else if ( uiA==0 || uiB==0 ){
+#ifdef SOFTPOSIT_EXACT
+ uZ.ui.v = (uiA | -uiB);
+ uZ.ui.exact = 0;
+#else
+ uZ.ui = (uiA | -uiB);
+#endif
+ return uZ.p;
+ }
+
+ //different signs
+ if ((uiA^uiB)>>31)
+ return softposit_addMagsPX2(uiA, (-uiB & 0xFFFFFFFF), x);
+ else
+ return softposit_subMagsPX2(uiA, (-uiB & 0xFFFFFFFF), x);
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/pX2_to_pX1.c b/source/luametatex/source/libraries/softposit/source/pX2_to_pX1.c
new file mode 100644
index 000000000..35b161947
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/pX2_to_pX1.c
@@ -0,0 +1,128 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t pX2_to_pX1( posit_2_t pA, int x ){
+
+ union ui32_pX2 uA;
+ union ui32_pX1 uZ;
+ uint_fast32_t uiA, tmp, regime;
+ uint_fast32_t exp_frac32A=0;
+ bool sign, regSA, bitNPlusOne, bitsMore;
+ int_fast8_t kA=0, regA;
+
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ uA.p = pA;
+ uiA = uA.ui;
+
+ if (uiA==0x80000000 || uiA==0 ){
+ uZ.ui = uiA;
+ return uZ.p;
+ }
+
+ sign = signP32UI( uiA );
+ if (sign) uiA = -uiA & 0xFFFFFFFF;
+
+
+ regSA = signregP32UI(uiA);
+ if (x==2){
+ uZ.ui=(uiA>0)?(0x40000000):(0);
+ }
+ else {
+ //regime
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ //exp and frac
+ exp_frac32A = tmp<<1;
+ if(kA<0){
+ regA = (-kA)<<1;
+ if (exp_frac32A&0x80000000) regA--;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = (kA<<1)+1;
+ if (exp_frac32A&0x80000000) regA++;
+ exp_frac32A = (exp_frac32A<<1) &0xFFFFFFFF;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ bitNPlusOne = (exp_frac32A >>(regA+33-x))&0x1;
+ bitsMore = exp_frac32A&(0x7FFFFFFF>>(x-regA-2));
+
+ if (regA<30) exp_frac32A >>=(2+regA);
+ else exp_frac32A=0;
+ uZ.ui = regime + (exp_frac32A & ((int32_t)0x80000000>>(x-1)) );
+
+ if (uZ.ui==0) uZ.ui = 0x1<<(32-x);
+ else if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+ }
+
+ if (sign) uZ.ui = (-uZ.ui & 0xFFFFFFFF);
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/quire16_fdp_add.c b/source/luametatex/source/libraries/softposit/source/quire16_fdp_add.c
new file mode 100644
index 000000000..959849ef7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire16_fdp_add.c
@@ -0,0 +1,185 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <inttypes.h>
+
+#include "platform.h"
+#include "internals.h"
+
+quire16_t q16_fdp_add( quire16_t q, posit16_t pA, posit16_t pB ){
+
+ union ui16_p16 uA, uB;
+ union ui128_q16 uZ, uZ1, uZ2;
+ uint_fast16_t uiA, uiB;
+ uint_fast16_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast8_t expA;
+ int_fast16_t kA=0, shiftRight=0;
+ uint_fast32_t frac32Z;
+ //For add
+ bool rcarryb, b1, b2, rcarryZ;//, rcarrySignZ;
+
+ uZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //NaR
+ if (isNaRQ16(q) || isNaRP16UI(uA.ui) || isNaRP16UI(uB.ui)){
+ uZ2.ui[0]=0x8000000000000000ULL;
+ uZ2.ui[1] = 0;
+ return uZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+ //max pos (sign plus and minus)
+ signA = signP16UI( uiA );
+ signB = signP16UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFF);
+ if(signB) uiB = (-uiB & 0xFFFF);
+
+ regSA = signregP16UI(uiA);
+ regSB = signregP16UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ fracA = (0x4000 | tmp);
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA += tmp>>14;
+ frac32Z = (uint_fast32_t) fracA * (0x4000 | tmp);
+
+ if (expA>1){
+ kA++;
+ expA ^=0x2;
+ }
+ rcarry = frac32Z>>29;//3rd bit (position 2) of frac32Z, hidden bit is 4th bit (position 3)
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac32Z>>=1;
+ }
+
+ //default dot is between bit 71 and 72, extreme left bit is bit 0. Last right bit is bit 127.
+ //Scale = 2^es * k + e => 2k + e
+ int firstPos = 71 - (kA<<1) - expA;
+
+ //No worries about hidden bit moving before position 4 because fraction is right aligned so
+ //there are 16 spare bits
+ if (firstPos>63){ //This means entire fraction is in right 64 bits
+ uZ2.ui[0] = 0;
+ shiftRight = firstPos-99;//99 = 63+ 4+ 32
+
+ uZ2.ui[1] = (shiftRight<0) ? ((uint64_t)frac32Z << -shiftRight) : ((uint64_t) frac32Z >> shiftRight);
+
+ }
+ else{//frac32Z can be in both left64 and right64
+ shiftRight = firstPos - 35;// -35= -3-32
+ if (shiftRight<0)
+ uZ2.ui[0] = ((uint64_t)frac32Z) << -shiftRight;
+ else{
+ uZ2.ui[0] = (uint64_t)frac32Z >> shiftRight;
+ uZ2.ui[1] = (uint64_t) frac32Z << (64 - shiftRight);
+ }
+
+ }
+
+ if (signZ2){
+ if (uZ2.ui[1]>0){
+ uZ2.ui[1] = - uZ2.ui[1];
+ uZ2.ui[0] = ~uZ2.ui[0];
+ }
+ else{
+ uZ2.ui[0] = -uZ2.ui[0];
+ }
+ }
+
+ //Addition
+ b1 = uZ1.ui[1]&0x1;
+ b2 = uZ2.ui[1]&0x1;
+ rcarryb = b1 & b2;
+ uZ.ui[1] = (uZ1.ui[1]>>1) + (uZ2.ui[1]>>1) + rcarryb;
+
+ rcarryZ = uZ.ui[1]>>63;
+
+ uZ.ui[1] = (uZ.ui[1]<<1 | (b1^b2) );
+
+ b1 = uZ1.ui[0]&0x1;
+ b2 = uZ2.ui[0]&0x1;
+ rcarryb = b1 & b2 ;
+ int_fast8_t rcarryb3 = b1 + b2 + rcarryZ;
+
+ uZ.ui[0] = (uZ1.ui[0]>>1) + (uZ2.ui[0]>>1) + ((rcarryb3>>1)& 0x1);
+ //rcarrySignZ = uZ.ui[0]>>63;
+
+ uZ.ui[0] = (uZ.ui[0]<<1 | (rcarryb3 & 0x1) );
+
+ //Exception handling for NaR
+ if (isNaRQ16(uZ.q)) uZ.q.v[0] = 0;
+
+ return uZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire16_fdp_sub.c b/source/luametatex/source/libraries/softposit/source/quire16_fdp_sub.c
new file mode 100644
index 000000000..caa034963
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire16_fdp_sub.c
@@ -0,0 +1,191 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <inttypes.h>
+
+#include "platform.h"
+#include "internals.h"
+
+quire16_t q16_fdp_sub( quire16_t q, posit16_t pA, posit16_t pB ){
+
+ union ui16_p16 uA, uB;
+ union ui128_q16 uZ, uZ1, uZ2;
+ uint_fast16_t uiA, uiB;
+ uint_fast16_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast8_t expA;
+ int_fast16_t kA=0, shiftRight;
+ uint_fast32_t frac32Z;
+ //For add
+ bool rcarryb, b1, b2, rcarryZ;//, rcarrySignZ;
+
+ uZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //NaR
+ if (isNaRQ16(q) || isNaRP16UI(uA.ui) || isNaRP16UI(uB.ui)){
+ uZ2.ui[0]=0x8000000000000000ULL;
+ uZ2.ui[1] = 0;
+ return uZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+
+ //max pos (sign plus and minus)
+ signA = signP16UI( uiA );
+ signB = signP16UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFF);
+ if(signB) uiB = (-uiB & 0xFFFF);
+
+ regSA = signregP16UI(uiA);
+ regSB = signregP16UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ fracA = (0x4000 | tmp);
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA += tmp>>14;
+ frac32Z = (uint_fast32_t) fracA * (0x4000 | tmp);
+
+ if (expA>1){
+ kA++;
+ expA ^=0x2;
+ }
+
+ rcarry = frac32Z>>29;//3rd bit (position 2) of frac32Z, hidden bit is 4th bit (position 3)
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac32Z>>=1;
+ }
+
+ //default dot is between bit 71 and 72, extreme left bit is bit 0. Last right bit is bit 127.
+ //Scale = 2^es * k + e => 2k + e
+ int firstPos = 71 - (kA<<1) - expA;
+
+ //No worries about hidden bit moving before position 4 because fraction is right aligned so
+ //there are 16 spare bits
+ if (firstPos>63){ //This means entire fraction is in right 64 bits
+ uZ2.ui[0] = 0;
+ shiftRight = firstPos-99;//99 = 63+ 4+ 32
+ if (shiftRight<0)//shiftLeft
+ uZ2.ui[1] = ((uint64_t)frac32Z) << -shiftRight;
+ else
+ uZ2.ui[1] = (uint64_t) frac32Z >> shiftRight;
+ }
+ else{//frac32Z can be in both left64 and right64
+ shiftRight = firstPos - 35;// -35= -3-32
+ if (shiftRight<0)
+ uZ2.ui[0] = ((uint64_t)frac32Z) << -shiftRight;
+ else{
+ uZ2.ui[0] = (uint64_t)frac32Z >> shiftRight;
+ uZ2.ui[1] = (uint64_t) frac32Z << (64 - shiftRight);
+ }
+
+ }
+
+ //This is the only difference from ADD (signZ2) and (!signZ2)
+ if (!signZ2){
+ if (uZ2.ui[1]>0){
+ uZ2.ui[1] = - uZ2.ui[1];
+ uZ2.ui[0] = ~uZ2.ui[0];
+ }
+ else{
+ uZ2.ui[0] = -uZ2.ui[0];
+ }
+ }
+
+ //Subtraction
+ b1 = uZ1.ui[1]&0x1;
+ b2 = uZ2.ui[1]&0x1;
+ rcarryb = b1 & b2;
+ uZ.ui[1] = (uZ1.ui[1]>>1) + (uZ2.ui[1]>>1) + rcarryb;
+
+ rcarryZ = uZ.ui[1]>>63;
+
+ uZ.ui[1] = (uZ.ui[1]<<1 | (b1^b2) );
+
+
+ b1 = uZ1.ui[0]&0x1;
+ b2 = uZ2.ui[0]&0x1;
+ rcarryb = b1 & b2 ;
+ int_fast8_t rcarryb3 = b1 + b2 + rcarryZ;
+
+ uZ.ui[0] = (uZ1.ui[0]>>1) + (uZ2.ui[0]>>1) + ((rcarryb3>>1)& 0x1);
+ //rcarrySignZ = uZ.ui[0]>>63;
+
+
+ uZ.ui[0] = (uZ.ui[0]<<1 | (rcarryb3 & 0x1) );
+
+ //Exception handling
+ if (isNaRQ16(uZ.q)) uZ.q.v[0] = 0;
+
+ return uZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire32_fdp_add.c b/source/luametatex/source/libraries/softposit/source/quire32_fdp_add.c
new file mode 100644
index 000000000..8adac09fb
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire32_fdp_add.c
@@ -0,0 +1,202 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "platform.h"
+#include "internals.h"
+
+quire_2_t qX2_fdp_add( quire_2_t q, posit_2_t pA, posit_2_t pB ){
+ union ui512_q32 uQZ;
+ union ui32_p32 uA, uB;
+ memcpy(uQZ.ui, q.v, 8*sizeof(uint64_t));
+ uA.ui = pA.v;
+ uB.ui = pB.v;
+ uQZ.q = q32_fdp_add(uQZ.q, uA.p, uB.p);
+ memcpy(q.v, uQZ.ui, 8*sizeof(uint64_t));
+ return q;
+}
+
+quire32_t q32_fdp_add( quire32_t q, posit32_t pA, posit32_t pB ){
+
+ union ui32_p32 uA, uB;
+ union ui512_q32 uZ, uZ1, uZ2;
+ uint_fast32_t uiA, uiB;
+ uint_fast32_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast32_t expA;
+ int_fast16_t kA=0, shiftRight=0;
+ uint_fast64_t frac64Z;
+ //For add
+ bool rcarryb, b1, b2, rcarryZ=0;
+
+ uZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ uZ2.q = q32Clr(uZ2.q); //set it to zero
+ //NaR
+ if (isNaRQ32(q) || isNaRP32UI(uA.ui) || isNaRP32UI(uB.ui)){
+ //set to all zeros
+ uZ2.ui[0]=0x8000000000000000ULL;
+ return uZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+
+ //max pos (sign plus and minus)
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<2) | 0x80000000) & 0xFFFFFFFF;
+
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<2) | 0x80000000) & 0xFFFFFFFF);
+
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+ //Will align frac64Z such that hidden bit is the first bit on the left.
+ rcarry = frac64Z>>63;//1st bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ }
+ else
+ frac64Z<<=1;
+
+ //default dot is between bit 271 and 272, extreme left bit is bit 0. Last right bit is bit 512.
+ //Minpos is 120 position to the right of binary point (dot)
+ //Scale = 2^es * k + e => 2k + e
+ int firstPos = 271 - (kA<<2) - expA;
+
+ //Moving in chunk of 64. If it is in first chunk, a part might be in the chunk right to it. Simply have to handle that.
+ int i;
+ for (i=0; i<8; i++){
+ if (firstPos<(i+1)*64){
+ //Need to check how much of the fraction is in the next 64 bits
+ shiftRight = firstPos - (i*64);
+ uZ2.ui[i] = frac64Z >> shiftRight;
+
+ if (i!=7 && shiftRight!=0) uZ2.ui[i+1] = frac64Z << (64 - shiftRight);
+ break;
+ }
+ }
+
+ if (signZ2){
+ for (i=7; i>=0; i--){
+ if (uZ2.ui[i]>0){
+ uZ2.ui[i] = - uZ2.ui[i];
+ i--;
+ while(i>=0){
+ uZ2.ui[i] = ~uZ2.ui[i];
+ i--;
+ }
+ break;
+ }
+ }
+ }
+
+ //Addition
+ for (i=7; i>=0; i--){
+ b1 = uZ1.ui[i] & 0x1;
+ b2 = uZ2.ui[i] & 0x1;
+ if (i==7){
+ rcarryb = b1 & b2;
+ uZ.ui[i] = (uZ1.ui[i]>>1) + (uZ2.ui[i]>>1) + rcarryb;
+ rcarryZ = uZ.ui[i]>>63;
+ uZ.ui[i] = (uZ.ui[i]<<1 | (b1^b2) );
+ }
+ else{
+ int_fast8_t rcarryb3 = b1 + b2 + rcarryZ;
+ uZ.ui[i] = (uZ1.ui[i]>>1) + (uZ2.ui[i]>>1) + (rcarryb3>>1);
+ rcarryZ = uZ.ui[i]>>63;
+ uZ.ui[i] = (uZ.ui[i]<<1 | (rcarryb3 & 0x1) );
+ }
+
+ }
+
+ //Exception handling
+ if (isNaRQ32(uZ.q) ) uZ.q.v[0]=0;
+
+ return uZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire32_fdp_sub.c b/source/luametatex/source/libraries/softposit/source/quire32_fdp_sub.c
new file mode 100644
index 000000000..1a132eefd
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire32_fdp_sub.c
@@ -0,0 +1,205 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "platform.h"
+#include "internals.h"
+
+//c-(a*b)
+
+quire_2_t qX2_fdp_sub( quire_2_t q, posit_2_t pA, posit_2_t pB ){
+ union ui512_q32 uQZ;
+ union ui32_p32 uA, uB;
+ memcpy(uQZ.ui, q.v, 8*sizeof(uint64_t));
+ uA.ui = pA.v;
+ uB.ui = pB.v;
+ uQZ.q = q32_fdp_sub(uQZ.q, uA.p, uB.p);
+ memcpy(q.v, uQZ.ui, 8*sizeof(uint64_t));
+ return q;
+}
+
+quire32_t q32_fdp_sub( quire32_t q, posit32_t pA, posit32_t pB ){
+
+ union ui32_p32 uA, uB;
+ union ui512_q32 uZ, uZ1, uZ2;
+ uint_fast32_t uiA, uiB;
+ uint_fast32_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast32_t expA;
+ int_fast16_t kA=0, shiftRight=0;
+ uint_fast64_t frac64Z;
+ //For sub
+ bool rcarryb, b1, b2, rcarryZ;
+
+ uZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ uZ2.q = q32Clr(uZ2.q); //set it to zero
+ //NaR
+ if (isNaRQ32(q) || isNaRP32UI(uA.ui) || isNaRP32UI(uB.ui)){
+ //set to all zeros
+ uZ2.ui[0]=0x8000000000000000ULL;
+ return uZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+ //max pos (sign plus and minus)
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+
+ tmp = (uiA<<2) & 0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<2) | 0x80000000) & 0xFFFFFFFF;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<2) | 0x80000000) & 0xFFFFFFFF);
+
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+ //Will align frac64Z such that hidden bit is the first bit on the left.
+ rcarry = frac64Z>>63;//1st bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ //frac64Z>>=1;
+ }
+ else
+ frac64Z<<=1;
+
+ //default dot is between bit 271 and 272, extreme left bit is bit 0. Last right bit is bit 512.
+ //Minpos is 120 position to the right of binary point (dot)
+ //Scale = 2^es * k + e => 2k + e
+ int firstPos = 271 - (kA<<2) - expA;
+
+ //Moving in chunk of 64. If it is in first chunk, a part might be in the chunk right to it. Simply have to handle that.
+ int i;
+ for (i=0; i<8; i++){
+ if (firstPos<(i+1)*64){
+ //Need to check how much of the fraction is in the next 64 bits
+ shiftRight = firstPos - (i*64);
+ uZ2.ui[i] = frac64Z >> shiftRight;
+ if (i!=7 && shiftRight!=0) uZ2.ui[i+1] = frac64Z << (64 - shiftRight);
+ break;
+ }
+ }
+
+
+ //This is the only difference from ADD (signZ2) and (!signZ2)
+ if (!signZ2){
+ for (i=7; i>=0; i--){
+ if (uZ2.ui[i]>0){
+ uZ2.ui[i] = - uZ2.ui[i];
+ i--;
+ while(i>=0){
+ uZ2.ui[i] = ~uZ2.ui[i];
+ i--;
+ }
+ break;
+ }
+ }
+
+ }
+
+ //Subtraction
+ for (i=7; i>=0; i--){
+ b1 = uZ1.ui[i] & 0x1;
+ b2 = uZ2.ui[i] & 0x1;
+ if (i==7){
+ rcarryb = b1 & b2;
+ uZ.ui[i] = (uZ1.ui[i]>>1) + (uZ2.ui[i]>>1) + rcarryb;
+ rcarryZ = uZ.ui[i]>>63;
+ uZ.ui[i] = (uZ.ui[i]<<1 | (b1^b2) );
+ }
+ else{
+ int_fast8_t rcarryb3 = b1 + b2 + rcarryZ;
+ uZ.ui[i] = (uZ1.ui[i]>>1) + (uZ2.ui[i]>>1) + (rcarryb3>>1);
+ rcarryZ = uZ.ui[i]>>63;
+ uZ.ui[i] = (uZ.ui[i]<<1 | (rcarryb3 & 0x1) );
+ }
+
+ }
+
+ //Exception handling
+ if (isNaRQ32(uZ.q) ) uZ.q.v[0]=0;
+
+ return uZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire8_fdp_add.c b/source/luametatex/source/libraries/softposit/source/quire8_fdp_add.c
new file mode 100644
index 000000000..8d8497352
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire8_fdp_add.c
@@ -0,0 +1,137 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include <inttypes.h>
+
+#include "platform.h"
+#include "internals.h"
+
+quire8_t q8_fdp_add( quire8_t q, posit8_t pA, posit8_t pB ){
+ union ui8_p8 uA, uB;
+ union ui32_q8 uqZ, uqZ1, uqZ2;
+ uint_fast8_t uiA, uiB;
+ uint_fast8_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast8_t kA=0, shiftRight=0;
+ uint_fast32_t frac32Z;
+
+
+ uqZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //NaR
+ if (isNaRQ8(q) || isNaRP8UI(uA.ui) || isNaRP8UI(uB.ui)){
+ uqZ2.ui=0x80000000;
+ return uqZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+
+ //max pos (sign plus and minus)
+ signA = signP8UI( uiA );
+ signB = signP8UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFF);
+ if(signB) uiB = (-uiB & 0xFF);
+
+ regSA = signregP8UI(uiA);
+ regSB = signregP8UI(uiB);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ fracA = (0x80 | tmp);
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac32Z = (uint_fast32_t)( fracA * (0x80 | tmp) ) <<16;
+
+ rcarry = frac32Z>>31;//1st bit (position 2) of frac32Z, hidden bit is 4th bit (position 3)
+ if (rcarry){
+ kA ++;
+ frac32Z>>=1;
+ }
+
+
+
+ //default dot is between bit 19 and 20, extreme left bit is bit 0. Last right bit is bit 31.
+ //Scale = 2^es * k + e => 2k + e // firstPost = 19-kA, shift = firstPos -1 (because frac32Z start from 2nd bit)
+ //int firstPos = 19 - kA;
+ shiftRight = 18-kA;
+
+ uqZ2.ui = frac32Z>> shiftRight;
+
+
+ if (signZ2) uqZ2.ui = -uqZ2.ui & 0xFFFFFFFF;
+
+
+ //Addition
+ uqZ.ui = uqZ2.ui + uqZ1.ui;
+ //Exception handling
+ if (isNaRQ8(uqZ.q) ) uqZ.q.v = 0;
+
+ return uqZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire8_fdp_sub.c b/source/luametatex/source/libraries/softposit/source/quire8_fdp_sub.c
new file mode 100644
index 000000000..f1de20cb7
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire8_fdp_sub.c
@@ -0,0 +1,136 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include <inttypes.h>
+
+#include "platform.h"
+#include "internals.h"
+
+
+//q - (pA*pB)
+quire8_t q8_fdp_sub( quire8_t q, posit8_t pA, posit8_t pB ){
+
+ union ui8_p8 uA, uB;
+ union ui32_q8 uqZ, uqZ1, uqZ2;
+ uint_fast8_t uiA, uiB;
+ uint_fast8_t fracA, tmp;
+ bool signA, signB, signZ2, regSA, regSB, rcarry;
+ int_fast8_t kA=0, shiftRight=0;
+ uint_fast32_t frac32Z;
+
+ uqZ1.q = q;
+
+ uA.p = pA;
+ uiA = uA.ui;
+ uB.p = pB;
+ uiB = uB.ui;
+
+ //NaR
+ if (isNaRQ8(q) || isNaRP8UI(uA.ui) || isNaRP8UI(uB.ui)){
+ uqZ2.ui=0x80000000;
+ return uqZ2.q;
+ }
+ else if (uiA==0 || uiB==0)
+ return q;
+
+
+ //max pos (sign plus and minus)
+ signA = signP8UI( uiA );
+ signB = signP8UI( uiB );
+ signZ2 = signA ^ signB;
+
+ if(signA) uiA = (-uiA & 0xFF);
+ if(signB) uiB = (-uiB & 0xFF);
+
+ regSA = signregP8UI(uiA);
+ regSB = signregP8UI(uiB);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ fracA = (0x80 | tmp);
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac32Z = (uint_fast32_t)( fracA * (0x80 | tmp) ) <<16;
+
+ rcarry = frac32Z>>31;//1st bit (position 2) of frac32Z, hidden bit is 4th bit (position 3)
+ if (rcarry){
+ kA ++;
+ frac32Z>>=1;
+ }
+
+ //default dot is between bit 19 and 20, extreme left bit is bit 0. Last right bit is bit 31.
+ //Scale = 2^es * k + e => 2k + e // firstPost = 19-kA, shift = firstPos -1 (because frac32Z start from 2nd bit)
+ //int firstPos = 19 - kA;
+ shiftRight = 18-kA;
+
+ uqZ2.ui = frac32Z>> shiftRight;
+
+ //This is the only difference from ADD (signZ2) and (!signZ2)
+ if (!signZ2) uqZ2.ui = -uqZ2.ui & 0xFFFFFFFF;
+
+ //Addition
+ uqZ.ui = uqZ2.ui + uqZ1.ui;
+
+ //Exception handling
+ if (isNaRQ8(uqZ.q) ) uqZ.q.v=0;
+
+ return uqZ.q;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/quire_helper.c b/source/luametatex/source/libraries/softposit/source/quire_helper.c
new file mode 100644
index 000000000..110e40433
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/quire_helper.c
@@ -0,0 +1,194 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+void printBinary(uint64_t * s, int size) {
+ int i;
+
+ uint64_t number = *s;
+ int bitSize = size -1;
+ for(i = 0; i < size; ++i) {
+ if(i%8 == 0)
+ putchar(' ');
+ printf("%llu", (number >> (bitSize-i))&1);
+ }
+ printf("\n");
+
+}
+void printBinaryQuire32(quire32_t * s){
+ int size = 512;
+ int dotPos = 272;
+ int bitSize = 63;
+
+ int n = 0;
+ uint64_t number = s->v[n];
+ for(int i = 0; i < size; ++i) {
+ if (i!=0 && i%64==0){
+ printf("\n");
+ n++;
+ number = s->v[n];
+ }
+ if(i%8 == 0)
+ putchar(' ');
+ if (i==dotPos)
+ putchar('.');
+ printf("%llu", (number >> (bitSize-i))&1);
+ }
+ printf("\n");
+}
+
+void printBinaryQuire16(quire16_t * s){
+ int size = 128;
+ int dotPos = 72;
+ int bitSize = 63;
+
+ int n = 0;
+ uint64_t number = s->v[n];
+ for(int i = 0; i < size; ++i) {
+ if (i!=0 && i%64==0){
+ printf("\n");
+ n++;
+ number = s->v[n];
+ }
+ if(i%8 == 0)
+ putchar(' ');
+ if (i==dotPos)
+ putchar('.');
+ printf("%llu", (number >> (bitSize-i))&1);
+ }
+ printf("\n");
+}
+
+void printBinaryQuire8(quire8_t * s){
+ int size = 32;
+ uint32_t number = s->v;
+ int dotPos = 20;
+
+ int bitSize = size -1;
+ for(int i = 0; i < size; ++i) {
+ if(i%8 == 0)
+ putchar(' ');
+ if (i==dotPos)
+ putchar('.');
+ printf("%u", (number >> (bitSize-i))&1);
+ }
+ printf("\n");
+}
+
+void printBinaryPX(uint32_t * s, int size) {
+ int i;
+ uint32_t number = *s;
+ number >>= (32-size);
+ int bitSize = size -1;
+ for(i = 0; i < size; ++i){
+ if(i%8 == 0)
+ putchar(' ');
+ printf("%u", (number >> (bitSize-i))&1);
+ }
+ printf("\n");
+
+}
+void printHex64(uint64_t s) {
+ printf("%016llx\n", s);
+
+}
+void printHex(uint64_t s) {
+ printf("0x%llx\n", s);
+
+}
+void printHexPX(uint32_t s, int size) {
+ s>>=(32-size);
+ printf("0x%x\n", s);
+
+}
+quire16_t q16_TwosComplement(quire16_t q){
+ if (!isQ16Zero(q) && !isNaRQ16(q)){
+ if (q.v[1]==0){
+ q.v[0] = -q.v[0];
+ }
+ else{
+ q.v[1] = - q.v[1];
+ q.v[0] = ~q.v[0];
+ }
+ }
+ return q;
+
+}
+
+quire32_t q32_TwosComplement(quire32_t q){
+ if (!isQ32Zero(q) && !isNaRQ32(q)){
+ int i=7;
+ bool found = false;
+ while(i){
+ if (found){
+ q.v[i] = ~q.v[i];
+ }
+ else{
+ if (q.v[i]!=0){
+ q.v[i] = -q.v[i];
+ found = true;
+ }
+ }
+ i--;
+ }
+ }
+ return q;
+
+}
+
+quire_2_t qX2_TwosComplement(quire_2_t q){
+ if (!isQX2Zero(q) && !isNaRQX2(q)){
+ int i=7;
+ bool found = false;
+ while(i){
+ if (found){
+ q.v[i] = ~q.v[i];
+ }
+ else{
+ if (q.v[i]!=0){
+ q.v[i] = -q.v[i];
+ found = true;
+ }
+ }
+ i--;
+ }
+ }
+ return q;
+
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_addMagsP16.c b/source/luametatex/source/libraries/softposit/source/s_addMagsP16.c
new file mode 100644
index 000000000..a90b4bb65
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_addMagsP16.c
@@ -0,0 +1,172 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+#include "stdlib.h"
+#include <math.h>
+
+#ifdef SOFTPOSIT_EXACT
+posit16_t softposit_addMagsP16( uint_fast16_t uiA, uint_fast16_t uiB, bool isExact){
+#else
+posit16_t softposit_addMagsP16( uint_fast16_t uiA, uint_fast16_t uiB ){
+#endif
+
+ uint_fast16_t regA, uiX, uiY;
+ uint_fast32_t frac32A, frac32B;
+ uint_fast16_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, rcarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0, expA;
+ int_fast16_t shiftRight;
+ union ui16_p16 uZ;
+
+ sign = signP16UI( uiA ); //sign is always positive.. actually don't have to do this.
+ if (sign){
+ uiA = -uiA & 0xFFFF;
+ uiB = -uiB & 0xFFFF;
+ }
+
+ if ((int_fast16_t)uiA < (int_fast16_t)uiB){
+ uiX = uiA;
+ uiY = uiB;
+ uiA = uiY;
+ uiB = uiX;
+ }
+ regSA = signregP16UI( uiA );
+ regSB = signregP16UI( uiB );
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ frac32A = (0x4000 | tmp) << 16;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ frac32B = (0x4000 | tmp) <<16;
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>15)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ frac32B = ( (0x4000 | tmp) <<16 ) & 0x7FFFFFFF;
+ }
+
+ //This is 2kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<1) + expA - (tmp>>14);
+
+ if (shiftRight==0){
+ frac32A += frac32B;
+ //rcarry is one
+ if (expA) kA ++;
+ expA^=1;
+ frac32A>>=1;
+ }
+ else{
+ //Manage CLANG (LLVM) compiler when shifting right more than number of bits
+ (shiftRight>31) ? (frac32B=0): (frac32B >>= shiftRight); //frac32B >>= shiftRight
+
+ frac32A += frac32B;
+ rcarry = 0x80000000 & frac32A; //first left bit
+ if(rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac32A>>=1;
+ }
+ }
+ if(kA<0){
+ regA = (-kA & 0xFFFF);
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+ if(regA>14){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac32A = (frac32A & 0x3FFFFFFF) >>(regA + 1) ;
+ fracA = frac32A>>16;
+ if (regA!=14) bitNPlusOne = (frac32A>>15) & 0x1;
+ else if (frac32A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==14 && expA) bitNPlusOne = 1;
+ uZ.ui = packToP16UI(regime, regA, expA, fracA);
+ if (bitNPlusOne){
+ if ( frac32A&0x7FFF ) bitsMore=1;
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_addMagsP32.c b/source/luametatex/source/libraries/softposit/source/s_addMagsP32.c
new file mode 100644
index 000000000..47ecc1b3a
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_addMagsP32.c
@@ -0,0 +1,173 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t softposit_addMagsP32( uint_fast32_t uiA, uint_fast32_t uiB ) {
+ uint_fast16_t regA, regB;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, rcarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA;
+ int_fast16_t shiftRight;
+ union ui32_p32 uZ;
+
+ sign = signP32UI( uiA );
+ if (sign){
+ uiA = -uiA & 0xFFFFFFFF;
+ uiB = -uiB & 0xFFFFFFFF;
+ }
+
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ expA = tmp>>29; //to get 2 bits
+ frac64A = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ frac64B = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ //This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<2) + expA - (tmp>>29);
+
+ //Manage CLANG (LLVM) compiler when shifting right more than number of bits
+ (shiftRight>63) ? (frac64B=0): (frac64B >>= shiftRight); //frac64B >>= shiftRight
+
+ frac64A += frac64B;
+
+ rcarry = 0x8000000000000000 & frac64A; //first left bit
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64A>>=1;
+ }
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>30){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 2) ; // 2 bits exp
+
+ fracA = frac64A>>32;
+
+ if (regA<=28){
+ bitNPlusOne |= (0x80000000 & frac64A) ;
+ expA <<= (28-regA);
+ }
+ else {
+ if (regA==30){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==29){
+ bitNPlusOne = expA&0x1;
+ expA>>=1;
+ }
+ if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ }
+
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7FFFFFFF & frac64A) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_addMagsP8.c b/source/luametatex/source/libraries/softposit/source/s_addMagsP8.c
new file mode 100644
index 000000000..8e0a74641
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_addMagsP8.c
@@ -0,0 +1,145 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+#ifdef SOFTPOSIT_EXACT
+posit8_t softposit_addMagsP8( uint_fast8_t uiA, uint_fast8_t uiB, bool isExact){
+#else
+posit8_t softposit_addMagsP8( uint_fast8_t uiA, uint_fast8_t uiB ){
+#endif
+
+ uint_fast8_t regA;
+ uint_fast16_t frac16A, frac16B;
+ uint_fast8_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, rcarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast16_t shiftRight;
+ union ui8_p8 uZ;
+
+ sign = signP8UI( uiA ); //sign is always positive.. actually don't have to do this.
+ if (sign){
+ uiA = -uiA & 0xFF;
+ uiB = -uiB & 0xFF;
+ }
+
+ if ((int_fast8_t)uiA < (int_fast8_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ }
+ regSA = signregP8UI( uiA );
+ regSB = signregP8UI( uiB );
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16A = (0x80 | tmp) << 7;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>7)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16B = (0x80 | tmp) <<7 ;
+
+ //Manage CLANG (LLVM) compiler when shifting right more than number of bits
+ (shiftRight>7) ? (frac16B=0): (frac16B >>= shiftRight); //frac32B >>= shiftRight
+
+ frac16A += frac16B;
+
+ rcarry = 0x8000 & frac16A; //first left bit
+ if (rcarry){
+ kA++;
+ frac16A>>=1;
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFF);
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7F-(0x7F>>regA);
+ }
+
+ if(regA>6){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7F): (uZ.ui=0x1);
+ }
+ else{
+ frac16A = (frac16A&0x3FFF) >> regA;
+ fracA = (uint_fast8_t) (frac16A>>8);
+ bitNPlusOne = (0x80 & frac16A) ;
+ uZ.ui = packToP8UI(regime, fracA);
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7F & frac16A) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+ return uZ.p;
+}
+
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_addMagsPX1.c b/source/luametatex/source/libraries/softposit/source/s_addMagsPX1.c
new file mode 100644
index 000000000..608ca948c
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_addMagsPX1.c
@@ -0,0 +1,190 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t softposit_addMagsPX1( uint_fast32_t uiA, uint_fast32_t uiB, int x ) {
+ int regA;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, rcarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA=0;
+ int_fast16_t shiftRight;
+ union ui32_pX1 uZ;
+
+
+ sign = signP32UI( uiA );
+ if (sign){
+ uiA = -uiA & 0xFFFFFFFF;
+ uiB = -uiB & 0xFFFFFFFF;
+ }
+
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ if (x==2){
+ uZ.ui = (regSA|regSB) ? (0x40000000) : (0x0);
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ expA = tmp>>30; //to get 1 bits
+ frac64A = ((0x40000000ULL | tmp) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ frac64B = ((0x40000000ULL | tmp) & 0x7FFFFFFFULL) <<32;
+ //This is 2kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<1) + expA - (tmp>>30);
+
+ if (shiftRight==0){
+ frac64A += frac64B;
+ //rcarry is one
+ if (expA) kA ++;
+ expA^=1;
+ frac64A>>=1;
+ }
+ else{
+ //Manage CLANG (LLVM) compiler when shifting right more than number of bits
+ (shiftRight>63) ? (frac64B=0): (frac64B >>= shiftRight); //frac64B >>= shiftRight
+
+ frac64A += frac64B;
+
+ rcarry = 0x8000000000000000 & frac64A; //first left bit
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;;
+ frac64A>>=1;
+ }
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 1) ; // 2 bits exp
+ fracA = frac64A>>32;
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA!=(x-2))
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>x) & frac64A);
+ else if (frac64A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==(x-2) && expA){
+ bitNPlusOne = 1;
+ expA=0;
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+ fracA &=((int32_t)0x80000000>>(x-1));
+
+ expA <<= (29-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if ((0x7FFFFFFFFFFFFFFF>>x) & frac64A) bitsMore=1;
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_addMagsPX2.c b/source/luametatex/source/libraries/softposit/source/s_addMagsPX2.c
new file mode 100644
index 000000000..d4515b8c6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_addMagsPX2.c
@@ -0,0 +1,198 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t softposit_addMagsPX2( uint_fast32_t uiA, uint_fast32_t uiB, int x ) {
+ int regA;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, rcarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA=0;
+ int_fast16_t shiftRight;
+ union ui32_pX2 uZ;
+
+
+ sign = signP32UI( uiA );
+ if (sign){
+ uiA = -uiA & 0xFFFFFFFF;
+ uiB = -uiB & 0xFFFFFFFF;
+ }
+
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ if (x==2){
+ uZ.ui = (regSA|regSB) ? (0x40000000) : (0x0);
+ }
+ else{
+ //int tmpX = x-2;
+ tmp = (uiA<<2)&0xFFFFFFFF;
+
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ expA = tmp>>29; //to get 2 bits
+ frac64A = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ frac64B = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ //This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<2) + expA - (tmp>>29);
+
+ //Manage CLANG (LLVM) compiler when shifting right more than number of bits
+ (shiftRight>63) ? (frac64B=0): (frac64B >>= shiftRight); //frac64B >>= shiftRight
+
+ frac64A += frac64B;
+
+ rcarry = 0x8000000000000000 & frac64A; //first left bit
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64A>>=1;
+ }
+
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 2) ; // 2 bits exp
+ fracA = frac64A>>32;
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+ bitNPlusOne |= (((uint64_t)0x80000000<<(32-x))& frac64A) ;
+ //expA <<= (28-regA);
+ }
+ else {
+ if (regA==(x-2)){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==(x-3)){
+ bitNPlusOne = expA&0x1;
+ //expA>>=1;
+ expA &=0x2;
+ }
+ if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+ fracA &=((int32_t)0x80000000>>(x-1));
+
+ expA <<= (28-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (((uint64_t)0xFFFFFFFFFFFFFFFF>>(x+1)) & frac64A) bitsMore=1;
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_approxRecipSqrt_1Ks.c b/source/luametatex/source/libraries/softposit/source/s_approxRecipSqrt_1Ks.c
new file mode 100644
index 000000000..f85522321
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_approxRecipSqrt_1Ks.c
@@ -0,0 +1,52 @@
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+#include "platform.h"
+
+const uint_fast16_t softposit_approxRecipSqrt0[16] = {
+ 0xb4c9, 0xffab, 0xaa7d, 0xf11c, 0xa1c5, 0xe4c7, 0x9a43, 0xda29,
+ 0x93b5, 0xd0e5, 0x8ded, 0xc8b7, 0x88c6, 0xc16d, 0x8424, 0xbae1
+};
+const uint_fast16_t softposit_approxRecipSqrt1[16] = {
+ 0xa5a5, 0xea42, 0x8c21, 0xc62d, 0x788f, 0xaa7f, 0x6928, 0x94b6,
+ 0x5cc7, 0x8335, 0x52a6, 0x74e2, 0x4a3e, 0x68fe, 0x432b, 0x5efd
+};
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_mulAddP16.c b/source/luametatex/source/libraries/softposit/source/s_mulAddP16.c
new file mode 100644
index 000000000..9b5cf6020
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_mulAddP16.c
@@ -0,0 +1,275 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+//softposit_mulAdd_subC => (uiA*uiB)-uiC
+//softposit_mulAdd_subProd => uiC - (uiA*uiB)
+//Default is always op==0
+posit16_t softposit_mulAddP16( uint_fast16_t uiA, uint_fast16_t uiB, uint_fast16_t uiC, uint_fast16_t op ){
+
+
+ union ui16_p16 uZ;
+ uint_fast16_t regZ, fracA, fracZ, regime, tmp;
+ bool signA, signB, signC, signZ, regSA, regSB, regSC, regSZ, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t expA, expC, expZ;
+ int_fast16_t kA=0, kC=0, kZ=0, shiftRight;
+ uint_fast32_t frac32C=0, frac32Z=0;
+
+ //NaR
+ if ( uiA==0x8000 || uiB==0x8000 || uiC==0x8000 ){
+ uZ.ui = 0x8000;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ if (op == softposit_mulAdd_subC)
+ uZ.ui = -uiC;
+ else
+ uZ.ui = uiC;
+ return uZ.p;
+ }
+
+ signA = signP16UI( uiA );
+ signB = signP16UI( uiB );
+ signC = signP16UI( uiC );//^ (op == softposit_mulAdd_subC);
+ signZ = signA ^ signB;// ^ (op == softposit_mulAdd_subProd);
+
+ if(signA) uiA = (-uiA & 0xFFFF);
+ if(signB) uiB = (-uiB & 0xFFFF);
+ if(signC) uiC = (-uiC & 0xFFFF);
+
+ regSA = signregP16UI(uiA);
+ regSB = signregP16UI(uiB);
+ regSC = signregP16UI(uiC);
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ fracA = (0x8000 | (tmp<<1)); //use first bit here for hidden bit to get more bits
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA += tmp>>14;
+ frac32Z = (uint_fast32_t) fracA * (0x8000 | (tmp <<1)); // first bit hidden bit
+
+ if (expA>1){
+ kA++;
+ expA ^=0x2;
+ }
+
+ rcarry = frac32Z>>31;//1st bit of frac32Z
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac32Z>>=1;
+ }
+
+ //Add
+ if (uiC!=0){
+ tmp = (uiC<<2) & 0xFFFF;
+ if (regSC){
+ while (tmp>>15){
+ kC++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kC=-1;
+ while (!(tmp>>15)){
+ kC--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expC = tmp>>14;
+ frac32C = (0x4000 | tmp) << 16;
+ shiftRight = ((kA-kC)<<1) + (expA-expC); //actually this is the scale
+
+ if (shiftRight<0){ // |uiC| > |Prod Z|
+ if (shiftRight<=-31){
+ bitsMore = 1;
+ frac32Z = 0;
+ }
+ else if (((frac32Z<<(32+shiftRight))&0xFFFFFFFF)!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac32Z = frac32C + (frac32Z>>-shiftRight);
+ else {//different signs
+ frac32Z = frac32C - (frac32Z>>-shiftRight) ;
+ signZ=signC;
+ if (bitsMore) frac32Z-=1;
+ }
+ kZ = kC;
+ expZ = expC;
+
+ }
+ else if (shiftRight>0){// |uiC| < |Prod|
+ //if (frac32C&((1<<shiftRight)-1)) bitsMore = 1;
+ if(shiftRight>=31){
+ bitsMore = 1;
+ frac32C = 0;
+ }
+ else if (((frac32C<<(32-shiftRight))&0xFFFFFFFF)!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac32Z = frac32Z + (frac32C>>shiftRight);
+ else{
+ frac32Z = frac32Z - (frac32C>>shiftRight);
+ if (bitsMore) frac32Z-=1;
+ }
+ kZ = kA;
+ expZ = expA;
+
+ }
+ else{
+ if(frac32C==frac32Z && signZ!=signC ){ //check if same number
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else{
+ if (signZ==signC)
+ frac32Z += frac32C;
+ else{
+ if (frac32Z<frac32C){
+ frac32Z = frac32C - frac32Z;
+ signZ = signC;
+ }
+ else{
+ frac32Z -= frac32C;
+ }
+ }
+ }
+ kZ = kA;// actually can be kC too, no diff
+ expZ = expA; //same here
+ }
+
+ rcarry = 0x80000000 & frac32Z; //first left bit
+ if(rcarry){
+ if (expZ) kZ ++;
+ expZ^=1;
+ if (frac32Z&0x1) bitsMore = 1;
+ frac32Z=(frac32Z>>1)&0x7FFFFFFF;
+ }
+ else {
+ //for subtract cases
+ if (frac32Z!=0){
+ while((frac32Z>>29)==0){
+ kZ--;
+ frac32Z<<=2;
+ }
+ }
+ bool ecarry = (0x40000000 & frac32Z)>>30;
+
+ if(!ecarry){
+ if (expZ==0) kZ--;
+ expZ^=1;
+ frac32Z<<=1;
+ }
+ }
+ }
+ else{
+ kZ = kA;
+ expZ=expA;
+ }
+
+ if(kZ<0){
+ regZ = (-kZ & 0xFFFF);
+ regSZ = 0;
+ regime = 0x4000>>regZ;
+ }
+ else{
+ regZ = kZ+1;
+ regSZ=1;
+ regime = 0x7FFF - (0x7FFF>>regZ);
+ }
+
+ if(regZ>14){
+ //max or min pos. exp and frac does not matter.
+ (regSZ) ? (uZ.ui= 0x7FFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac32Z &= 0x3FFFFFFF;
+ fracZ = frac32Z >> (regZ + 17);
+
+ if (regZ!=14) bitNPlusOne = (frac32Z>>regZ) & 0x10000;
+ else if (frac32Z>0){
+ fracZ=0;
+ bitsMore =1;
+ }
+ if (regZ==14 && expZ) bitNPlusOne = 1;
+ uZ.ui = packToP16UI(regime, regZ, expZ, fracZ);
+ if (bitNPlusOne){
+ if ( (frac32Z<<(16-regZ)) &0xFFFFFFFF ) bitsMore =1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_mulAddP32.c b/source/luametatex/source/libraries/softposit/source/s_mulAddP32.c
new file mode 100644
index 000000000..c1dcb6a27
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_mulAddP32.c
@@ -0,0 +1,296 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+
+posit32_t
+ softposit_mulAddP32(
+ uint_fast32_t uiA, uint_fast32_t uiB, uint_fast32_t uiC, uint_fast32_t op ){
+
+ union ui32_p32 uZ;
+ uint_fast32_t regA, regZ, fracA, fracZ, regime, tmp;
+ bool signA, signB, signC, signZ, regSA, regSB, regSC, regSZ, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA, expC, expZ;
+ int_fast16_t kA=0, kC=0, kZ=0, shiftRight;
+ uint_fast64_t frac64C, frac64Z;
+
+ //NaR
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiC==0x80000000 ){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ if (op == softposit_mulAdd_subC)
+ uZ.ui = -uiC;
+ else
+ uZ.ui = uiC;
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signC = signP32UI( uiC );//^ (op == softposit_mulAdd_subC);
+ signZ = signA ^ signB;// ^ (op == softposit_mulAdd_subProd);
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ if(signC) uiC = (-uiC & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+ regSC = signregP32UI(uiC);
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<2) | 0x80000000) & 0xFFFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<2) | 0x80000000) & 0xFFFFFFFF);
+
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+
+ rcarry = frac64Z>>63;//1st bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64Z>>=1;
+ }
+
+ if (uiC!=0){
+ tmp = (uiC<<2)&0xFFFFFFFF;
+ if (regSC){
+ while (tmp>>31){
+ kC++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kC=-1;
+ while (!(tmp>>31)){
+ kC--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expC = tmp>>29; //to get 2 bits
+ frac64C = (((tmp<<1) | 0x40000000ULL) & 0x7FFFFFFFULL)<<32;
+ shiftRight = ((kA-kC)<<2) + (expA-expC);
+
+ if (shiftRight<0){ // |uiC| > |Prod|
+ if (shiftRight<=-63){
+ bitsMore = 1;
+ frac64Z = 0;
+ //set bitsMore to one?
+ }
+ else if ((frac64Z<<(64+shiftRight))!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac64Z = frac64C + (frac64Z>>-shiftRight);
+ else {//different signs
+ frac64Z = frac64C - (frac64Z>>-shiftRight) ;
+ signZ=signC;
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kC;
+ expZ = expC;
+
+ }
+ else if (shiftRight>0){// |uiC| < |Prod|
+ //if (frac32C&((1<<shiftRight)-1)) bitsMore = 1;
+ if(shiftRight>=63) {
+ bitsMore = 1;
+ frac64C = 0;
+ }
+ else if ((frac64C<<(64-shiftRight))!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac64Z = frac64Z + (frac64C>>shiftRight);
+ else{
+ frac64Z = frac64Z - (frac64C>>shiftRight);
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kA;
+ expZ = expA;
+
+ }
+ else{
+ if(frac64C==frac64Z && signZ!=signC ){ //check if same number
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else{
+ if (signZ==signC)
+ frac64Z += frac64C;
+ else{
+ if (frac64Z<frac64C){
+ frac64Z = frac64C - frac64Z;
+ signZ = signC;
+ }
+ else{
+ frac64Z -= frac64C;
+ }
+ }
+ }
+ kZ = kA;// actually can be kC too, no diff
+ expZ = expA; //same here
+ }
+ rcarry = (uint64_t)frac64Z>>63; //first left bit
+
+ if(rcarry){
+ expZ++;
+ if (expZ>3){
+ kZ++;
+ expZ&=0x3;
+ }
+ frac64Z=(frac64Z>>1)&0x7FFFFFFFFFFFFFFF;
+ }
+ else {
+ //for subtract cases
+ if (frac64Z!=0){
+ while((frac64Z>>59)==0){
+ kZ--;
+ frac64Z<<=4;
+ }
+ while((frac64Z>>62)==0){
+ expZ--;
+ frac64Z<<=1;
+ if (expZ<0){
+ kZ--;
+ expZ=3;
+ }
+ }
+ }
+ }
+
+ }
+ else{
+ kZ = kA;
+ expZ=expA;
+ }
+ if(kZ<0){
+ regZ = -kZ;
+ regSZ = 0;
+ regime = 0x40000000>>regZ;
+ }
+ else{
+ regZ = kZ+1;
+ regSZ=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regZ);
+ }
+
+ if(regZ>30){
+ //max or min pos. exp and frac does not matter.
+ (regSZ) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ else{
+
+ if (regZ<=28){
+ //remove hidden bits
+ frac64Z &= 0x3FFFFFFFFFFFFFFF;
+ fracZ = frac64Z >> (regZ + 34);//frac32Z>>16;
+ bitNPlusOne |= (0x200000000 & (frac64Z >>regZ ) ) ;
+ expZ <<= (28-regZ);
+ }
+ else {
+ if (regZ==30){
+ bitNPlusOne = expZ&0x2;
+ bitsMore = (expZ&0x1);
+ expZ = 0;
+ }
+ else if (regZ==29){
+ bitNPlusOne = expZ&0x1;
+ expZ>>=1;
+ }
+ if (fracZ>0){
+ fracZ=0;
+ bitsMore =1;
+
+ }
+ }
+ uZ.ui = packToP32UI(regime, expZ, fracZ);
+
+ if (bitNPlusOne){
+ if ( (frac64Z<<(32-regZ)) &0xFFFFFFFFFFFFFFFF ) bitsMore =1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+
+ }
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFFFFFFFFFF;
+ return uZ.p;
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_mulAddP8.c b/source/luametatex/source/libraries/softposit/source/s_mulAddP8.c
new file mode 100644
index 000000000..a99756678
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_mulAddP8.c
@@ -0,0 +1,243 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+//softposit_mulAdd_subC => (uiA*uiB)-uiC
+//softposit_mulAdd_subProd => uiC - (uiA*uiB)
+//Default is always op==0
+posit8_t softposit_mulAddP8( uint_fast8_t uiA, uint_fast8_t uiB, uint_fast8_t uiC, uint_fast8_t op ){
+
+
+ union ui8_p8 uZ;
+ uint_fast8_t regZ, fracA, fracZ, regime, tmp;
+ bool signA, signB, signC, signZ, regSA, regSB, regSC, regSZ, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast8_t kA=0, kC=0, kZ=0, shiftRight;
+ uint_fast16_t frac16C, frac16Z;
+
+ //NaR
+ if ( uiA==0x80 || uiB==0x80 || uiC==0x80 ){
+ uZ.ui = 0x80;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ if (op == softposit_mulAdd_subC)
+ uZ.ui = -uiC;
+ else
+ uZ.ui = uiC;
+ return uZ.p;
+ }
+
+ signA = signP8UI( uiA );
+ signB = signP8UI( uiB );
+ signC = signP8UI( uiC );//^ (op == softposit_mulAdd_subC);
+ signZ = signA ^ signB;// ^ (op == softposit_mulAdd_subProd);
+
+ if(signA) uiA = (-uiA & 0xFF);
+ if(signB) uiB = (-uiB & 0xFF);
+ if(signC) uiC = (-uiC & 0xFF);
+
+ regSA = signregP8UI(uiA);
+ regSB = signregP8UI(uiB);
+ regSC = signregP8UI(uiC);
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ fracA = (0x80 | tmp); //use first bit here for hidden bit to get more bits
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16Z = (uint_fast16_t) fracA * (0x80 | tmp);
+
+ rcarry = frac16Z>>15;//1st bit of frac16Z
+ if (rcarry){
+ kA++;
+ frac16Z>>=1;
+ }
+
+ if (uiC!=0){
+ tmp = (uiC<<2) & 0xFF;
+ if (regSC){
+ while (tmp>>7){
+ kC++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kC=-1;
+ while (!(tmp>>7)){
+ kC--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16C = (0x80 | tmp) <<7 ;
+ shiftRight = (kA-kC);
+
+ if (shiftRight<0){ // |uiC| > |Prod|
+ if (shiftRight<=-15) {
+ bitsMore = 1;
+ frac16Z=0;
+ }
+ else if (((frac16Z<<(16+shiftRight))&0xFFFF)!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac16Z = frac16C + (frac16Z>>-shiftRight);
+ else {//different signs
+ frac16Z = frac16C - (frac16Z>>-shiftRight) ;
+ signZ=signC;
+ if (bitsMore) frac16Z-=1;
+ }
+ kZ = kC;
+
+ }
+ else if (shiftRight>0){// |uiC| < |Prod|
+
+ if(shiftRight>=15){
+ bitsMore = 1;
+ frac16C = 0;
+ }
+ else if (((frac16C<<(16-shiftRight))&0xFFFF)!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac16Z += (frac16C>>shiftRight);
+ else{
+ frac16Z -= (frac16C>>shiftRight);
+ if (bitsMore) frac16Z-=1;
+ }
+ kZ = kA;
+ }
+ else{
+ if(frac16C==frac16Z && signZ!=signC ){ //check if same number
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else{
+ if (signZ==signC)
+ frac16Z += frac16C;
+ else{
+ if (frac16Z<frac16C){
+ frac16Z = frac16C - frac16Z;
+ signZ = signC;
+ }
+ else{
+ frac16Z -= frac16C;
+ }
+ }
+ }
+ kZ = kA;// actually can be kC too, no diff
+ }
+
+ rcarry = 0x8000 & frac16Z; //first left bit
+ if(rcarry){
+ kZ ++;
+ frac16Z=(frac16Z>>1)&0x7FFF;
+ }
+ else {
+
+ //for subtract cases
+ if (frac16Z!=0){
+ while((frac16Z>>14)==0){
+ kZ--;
+ frac16Z<<=1;
+ }
+ }
+ }
+
+ }
+ else{
+ kZ = kA;
+ }
+
+ if(kZ<0){
+ regZ = (-kZ & 0xFF);
+ regSZ = 0;
+ regime = 0x40>>regZ;
+ }
+ else{
+ regZ = kZ+1;
+ regSZ=1;
+ regime = 0x7F - (0x7F>>regZ);
+ }
+
+ if(regZ>6){
+ //max or min pos. exp and frac does not matter.
+ (regSZ) ? (uZ.ui= 0x7F): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac16Z &= 0x3FFF;
+
+ fracZ = (frac16Z >> regZ) >> 8;
+
+ bitNPlusOne = ((frac16Z>>regZ) & 0x80);
+ uZ.ui = packToP8UI(regime, fracZ);
+
+ if (bitNPlusOne){
+ if ( (frac16Z<<(9-regZ)) &0xFFFF ) bitsMore =1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+
+ if (signZ) uZ.ui = -uZ.ui & 0xFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_mulAddPX1.c b/source/luametatex/source/libraries/softposit/source/s_mulAddPX1.c
new file mode 100644
index 000000000..78482d192
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_mulAddPX1.c
@@ -0,0 +1,341 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+//a*b+c
+posit_1_t
+ softposit_mulAddPX1(
+ uint_fast32_t uiA, uint_fast32_t uiB, uint_fast32_t uiC, uint_fast32_t op, int x ){
+
+ union ui32_pX1 uZ;
+ int regZ;
+ uint_fast32_t fracA, fracZ, regime, tmp;
+ bool signA, signB, signC, signZ, regSA, regSB, regSC, regSZ, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA, expC, expZ;
+ int_fast16_t kA=0, kC=0, kZ=0, shiftRight;
+ uint_fast64_t frac64C, frac64Z;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ //NaR
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiC==0x80000000 ){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ if (op == softposit_mulAdd_subC)
+ uZ.ui = -uiC;
+ else
+ uZ.ui = uiC;
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signC = signP32UI( uiC );//^ (op == softposit_mulAdd_subC);
+ signZ = signA ^ signB;// ^ (op == softposit_mulAdd_subProd);
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ if(signC) uiC = (-uiC & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+ regSC = signregP32UI(uiC);
+
+ if (x==2){
+ uZ.ui = (regSA&regSB) ? (0x40000000) : (0x0);
+ if (signZ){// i.e. negative prod
+ if (signC){
+ uZ.ui |= uiC;
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ }
+ else{//prod is negative
+ if (uiC==uZ.ui) uZ.ui = 0;
+ else uZ.ui =(uZ.ui>0)?( 0xC0000000):(0x40000000);
+ }
+ }
+ else{ //prod : same sign signZ=0
+ if (signC){
+ if (uiC==uZ.ui) uZ.ui = 0;
+ else uZ.ui = (uZ.ui>0) ? (0x40000000) : (0xC0000000);
+ }
+ else{//C is positive
+ uZ.ui |= uiC;
+ }
+ }
+ return uZ.p;
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>30; //to get 2 bits
+ fracA = ((tmp<<1) | 0x80000000) & 0xFFFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>30;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<1) | 0x80000000) & 0xFFFFFFFF);
+
+ if (expA>1){
+ kA++;
+ expA^=0x2;
+ }
+
+ rcarry = frac64Z>>63;//1st bit of frac64Z
+ if (rcarry){
+ if (expA) kA ++;
+ expA^=1;
+ frac64Z>>=1;
+ }
+
+ if (uiC!=0){
+ tmp = (uiC<<2)&0xFFFFFFFF;
+ if (regSC){
+ while (tmp>>31){
+ kC++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kC=-1;
+ while (!(tmp>>31)){
+ kC--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+//printBinary(&expC, 32);
+ expC = tmp>>30; //to get 1 bits
+ frac64C = ((tmp | 0x40000000ULL) & 0x7FFFFFFFULL)<<32;
+ shiftRight = ((kA-kC)<<1) + (expA-expC);
+//printf("shiftRight: %d kA: %d kC: %d\n", shiftRight, kA, kC);
+//printBinary(&frac64Z, 64);
+//printBinary(&frac64C, 64);
+ if (shiftRight<0){ // |uiC| > |Prod|
+ if (shiftRight<=-63){
+ bitsMore = 1;
+ frac64Z = 0;
+ //set bitsMore to one?
+ }
+ else if ((frac64Z<<(64+shiftRight))!=0) bitsMore = 1;
+//printf("bitsMore: %d\n", bitsMore);
+ if (signZ==signC)
+ frac64Z = frac64C + (frac64Z>>-shiftRight);
+ else {//different signs
+ frac64Z = frac64C - (frac64Z>>-shiftRight) ;
+ signZ=signC;
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kC;
+ expZ = expC;
+//printf("kZ: %d expZ: %d\n", kZ, expZ);
+//printBinary(&frac64Z, 64);
+ }
+ else if (shiftRight>0){// |uiC| < |Prod|
+ //if (frac32C&((1<<shiftRight)-1)) bitsMore = 1;
+ if(shiftRight>=63) {
+ bitsMore = 1;
+ frac64C = 0;
+ }
+ else if ((frac64C<<(64-shiftRight))!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac64Z = frac64Z + (frac64C>>shiftRight);
+ else{
+ frac64Z = frac64Z - (frac64C>>shiftRight);
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kA;
+ expZ = expA;
+
+ }
+ else{
+ if(frac64C==frac64Z && signZ!=signC ){ //check if same number
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else{
+ if (signZ==signC)
+ frac64Z += frac64C;
+ else{
+ if (frac64Z<frac64C){
+ frac64Z = frac64C - frac64Z;
+ signZ = signC;
+ }
+ else{
+ frac64Z -= frac64C;
+ }
+ }
+ }
+ kZ = kA;// actually can be kC too, no diff
+ expZ = expA; //same here
+ }
+ rcarry = (uint64_t)frac64Z>>63; //first left bit
+
+ if(rcarry){
+ if (expZ) kZ ++;
+ expZ^=1;
+ if (frac64Z&0x1) bitsMore = 1;
+ frac64Z=(frac64Z>>1)&0x7FFFFFFFFFFFFFFF;
+ }
+ else {
+ //for subtract cases
+ if (frac64Z!=0){
+ while((frac64Z>>61)==0){
+ kZ--;
+ frac64Z<<=2;
+ }
+ }
+ bool ecarry = (0x4000000000000000 & frac64Z)>>62;
+
+ if(!ecarry){
+ if (expZ==0) kZ--;
+ expZ^=1;
+ frac64Z<<=1;
+ }
+
+//printf("kZ: %d expZ: %d\n", kZ, expZ);
+//printf("frac64Z:\n");
+//printBinary(&frac64Z,64);
+
+ }
+
+ }
+ else{
+ kZ = kA;
+ expZ=expA;
+ }
+
+ if(kZ<0){
+ regZ = -kZ;
+ regSZ = 0;
+ regime = 0x40000000>>regZ;
+ }
+ else{
+ regZ = kZ+1;
+ regSZ=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regZ);
+ }
+//printf("regZ: %d regSZ: %d kZ: %d expZ: %d\n", regZ, regSZ, kZ, expZ);
+//printBinary(&frac64Z,64);
+ if(regZ>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSZ) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ if (regZ<x){
+ //remove hidden bits
+ frac64Z &= 0x3FFFFFFFFFFFFFFF;
+ fracZ = frac64Z >> (regZ + 33);//frac32Z>>16;
+//printBinary(&frac64Z,64);
+//printBinary(&fracZ,32);
+ if (regZ!=(x-2)){
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>(x-regZ-1)) & frac64Z);
+ bitsMore = ((0x7FFFFFFFFFFFFFFF>>(x-regZ-1)) & frac64Z);
+ fracZ&=((int32_t)0x80000000>>(x-1));
+ }
+ else if (frac64Z>0){
+ fracZ=0;
+ bitsMore=1;
+ }
+ if(regZ==(x-2) && expZ){
+ bitNPlusOne=1;
+ expZ=0;
+ }
+ }
+ else{
+ regime=(regSZ) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expZ=0;
+ fracZ=0;
+ }
+//printBinary(&fracZ, 32);
+//printf("expZ: %d bitNPlusOne: %d bitsMore; %d\n", expZ, bitNPlusOne, bitsMore);
+ expZ <<= (29-regZ);
+
+ uZ.ui = packToP32UI(regime, expZ, fracZ);
+//printBinary(&uZ.ui, 32);
+ if (bitNPlusOne){
+ //if (((uint64_t)0xFFFFFFFFFFFFFFFF>>(x+1)) & frac64Z) bitsMore=1;
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+
+ }
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+ }
+//printf("expA: %d expC: %d expZ: %d kZ: %d\n", expA, expC, expZ, kZ);
+
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_mulAddPX2.c b/source/luametatex/source/libraries/softposit/source/s_mulAddPX2.c
new file mode 100644
index 000000000..3401422e6
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_mulAddPX2.c
@@ -0,0 +1,345 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+//a*b+c
+posit_2_t
+ softposit_mulAddPX2(
+ uint_fast32_t uiA, uint_fast32_t uiB, uint_fast32_t uiC, uint_fast32_t op, int x ){
+
+ union ui32_pX2 uZ;
+ int regZ;
+ uint_fast32_t fracA, fracZ, regime, tmp;
+ bool signA, signB, signC, signZ, regSA, regSB, regSC, regSZ, bitNPlusOne=0, bitsMore=0, rcarry;
+ int_fast32_t expA, expC, expZ;
+ int_fast16_t kA=0, kC=0, kZ=0, shiftRight;
+ uint_fast64_t frac64C, frac64Z;
+
+ if (x<2 || x>32){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+
+ //NaR
+ if ( uiA==0x80000000 || uiB==0x80000000 || uiC==0x80000000 ){
+ uZ.ui = 0x80000000;
+ return uZ.p;
+ }
+ else if (uiA==0 || uiB==0){
+ if (op == softposit_mulAdd_subC)
+ uZ.ui = -uiC;
+ else
+ uZ.ui = uiC;
+ return uZ.p;
+ }
+
+ signA = signP32UI( uiA );
+ signB = signP32UI( uiB );
+ signC = signP32UI( uiC );//^ (op == softposit_mulAdd_subC);
+ signZ = signA ^ signB;// ^ (op == softposit_mulAdd_subProd);
+
+ if(signA) uiA = (-uiA & 0xFFFFFFFF);
+ if(signB) uiB = (-uiB & 0xFFFFFFFF);
+ if(signC) uiC = (-uiC & 0xFFFFFFFF);
+
+ regSA = signregP32UI(uiA);
+ regSB = signregP32UI(uiB);
+ regSC = signregP32UI(uiC);
+
+ if (x==2){
+ uZ.ui = (regSA&regSB) ? (0x40000000) : (0x0);
+ if (signZ){// i.e. negative prod
+ if (signC){
+ uZ.ui |= uiC;
+ uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ }
+ else{//prod is negative
+ if (uiC==uZ.ui) uZ.ui = 0;
+ else uZ.ui =(uZ.ui>0)?( 0xC0000000):(0x40000000);
+ }
+ }
+ else{ //prod : same sign signZ=0
+ if (signC){
+ if (uiC==uZ.ui) uZ.ui = 0;
+ else uZ.ui = (uZ.ui>0) ? (0x40000000) : (0xC0000000);
+ }
+ else{//C is positive
+ uZ.ui |= uiC;
+ }
+ }
+ return uZ.p;
+ }
+ else{
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA = tmp>>29; //to get 2 bits
+ fracA = ((tmp<<2) | 0x80000000) & 0xFFFFFFFF;
+
+ tmp = (uiB<<2)&0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA--;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+ expA += tmp>>29;
+ frac64Z = (uint_fast64_t) fracA * (((tmp<<2) | 0x80000000) & 0xFFFFFFFF);
+
+ if (expA>3){
+ kA++;
+ expA&=0x3; // -=4
+ }
+
+ rcarry = frac64Z>>63;//1st bit of frac64Z
+ if (rcarry){
+ expA++;
+ if (expA>3){
+ kA ++;
+ expA&=0x3;
+ }
+ frac64Z>>=1;
+ }
+
+ if (uiC!=0){
+ tmp = (uiC<<2)&0xFFFFFFFF;
+ if (regSC){
+ while (tmp>>31){
+ kC++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kC=-1;
+ while (!(tmp>>31)){
+ kC--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ expC = tmp>>29; //to get 2 bits
+ frac64C = (((tmp<<1) | 0x40000000ULL) & 0x7FFFFFFFULL)<<32;
+ shiftRight = ((kA-kC)<<2) + (expA-expC);
+
+ if (shiftRight<0){ // |uiC| > |Prod|
+ if (shiftRight<=-63){
+ bitsMore = 1;
+ frac64Z = 0;
+ //set bitsMore to one?
+ }
+ else if ((frac64Z<<(64+shiftRight))!=0) bitsMore = 1;
+
+ if (signZ==signC)
+ frac64Z = frac64C + (frac64Z>>-shiftRight);
+ else {//different signs
+ frac64Z = frac64C - (frac64Z>>-shiftRight) ;
+ signZ=signC;
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kC;
+ expZ = expC;
+ }
+ else if (shiftRight>0){// |uiC| < |Prod|
+ //if (frac32C&((1<<shiftRight)-1)) bitsMore = 1;
+ if(shiftRight>=63) {
+ bitsMore = 1;
+ frac64C = 0;
+ }
+ else if ((frac64C<<(64-shiftRight))!=0) bitsMore = 1;
+ if (signZ==signC)
+ frac64Z = frac64Z + (frac64C>>shiftRight);
+ else{
+ frac64Z = frac64Z - (frac64C>>shiftRight);
+ if (bitsMore) frac64Z-=1;
+ }
+ kZ = kA;
+ expZ = expA;
+
+ }
+ else{
+ if(frac64C==frac64Z && signZ!=signC ){ //check if same number
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ else{
+ if (signZ==signC)
+ frac64Z += frac64C;
+ else{
+ if (frac64Z<frac64C){
+ frac64Z = frac64C - frac64Z;
+ signZ = signC;
+ }
+ else{
+ frac64Z -= frac64C;
+ }
+ }
+ }
+ kZ = kA;// actually can be kC too, no diff
+ expZ = expA; //same here
+ }
+ rcarry = (uint64_t)frac64Z>>63; //first left bit
+
+ if(rcarry){
+ expZ++;
+ if (expZ>3){
+ kZ++;
+ expZ&=0x3;
+ }
+ frac64Z=(frac64Z>>1)&0x7FFFFFFFFFFFFFFF;
+ }
+ else {
+ //for subtract cases
+ if (frac64Z!=0){
+ while((frac64Z>>59)==0){
+ kZ--;
+ frac64Z<<=4;
+ }
+ while((frac64Z>>62)==0){
+ expZ--;
+ frac64Z<<=1;
+ if (expZ<0){
+ kZ--;
+ expZ=3;
+ }
+ }
+ }
+
+ }
+
+ }
+ else{
+ kZ = kA;
+ expZ=expA;
+ }
+
+ if(kZ<0){
+ regZ = -kZ;
+ regSZ = 0;
+ regime = 0x40000000>>regZ;
+ }
+ else{
+ regZ = kZ+1;
+ regSZ=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regZ);
+ }
+
+ if(regZ>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSZ) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+
+ if (regZ<x){
+ //remove hidden bits
+ frac64Z &= 0x3FFFFFFFFFFFFFFF;
+ fracZ = frac64Z >> (regZ + 34);//frac32Z>>16;
+
+ if (regZ<=(x-4)){
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>(x-regZ-2)) & frac64Z) ;
+ bitsMore = (((uint64_t)0x7FFFFFFFFFFFFFFF>>(x-regZ-2)) & frac64Z) ;
+ fracZ &=((int32_t)0x80000000>>(x-1));
+ }
+ else {
+
+ if (regZ==(x-2)){
+
+ bitNPlusOne = expZ&0x2;
+ bitsMore = (expZ&0x1);
+ expZ = 0;
+ }
+ else if (regZ==(x-3)){
+
+ bitNPlusOne = expZ&0x1;
+ expZ &=0x2;
+
+ }
+ if (frac64Z>0){
+ fracZ=0;
+ bitsMore =1;
+
+ }
+ }
+ }
+ else{
+ regime=(regSZ) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expZ=0;
+ fracZ=0;
+ }
+
+ expZ <<= (28-regZ);
+
+ uZ.ui = packToP32UI(regime, expZ, fracZ);
+
+ if (bitNPlusOne){
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+
+ }
+ if (signZ) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+ }
+
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_subMagsP16.c b/source/luametatex/source/libraries/softposit/source/s_subMagsP16.c
new file mode 100644
index 000000000..a0c9c33e2
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_subMagsP16.c
@@ -0,0 +1,175 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+#ifdef SOFTPOSIT_EXACT
+posit16_t softposit_subMagsP16( uint_fast16_t uiA, uint_fast16_t uiB, bool isExact){
+#else
+posit16_t softposit_subMagsP16( uint_fast16_t uiA, uint_fast16_t uiB ){
+#endif
+ uint_fast16_t regA;
+ uint_fast32_t frac32A, frac32B;
+ uint_fast16_t fracA=0, regime, tmp;
+ bool sign=0, regSA, regSB, ecarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast16_t shiftRight;
+ int_fast8_t kA=0, expA;
+ union ui16_p16 uZ;
+
+ //Both uiA and uiB are actually the same signs if uiB inherits sign of sub
+ //Make both positive
+ sign = signP16UI( uiA );
+ (sign)?(uiA = (-uiA & 0xFFFF)): (uiB = (-uiB & 0xFFFF));
+
+ if (uiA==uiB){ //essential, if not need special handling
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ if(uiA<uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ (sign) ? (sign = 0 ) : (sign=1); //A becomes B
+ }
+
+ regSA = signregP16UI( uiA );
+ regSB = signregP16UI( uiB );
+
+ tmp = (uiA<<2) & 0xFFFF;
+ if (regSA){
+ while (tmp>>15){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>15)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ expA = tmp>>14;
+ frac32A = (0x4000 | tmp) << 16;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFF;
+ if (regSB){
+ while (tmp>>15){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>15)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFF;
+ }
+ tmp&=0x7FFF;
+ }
+ frac32B = (0x4000 | tmp) <<16;
+ //This is 2kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+
+ shiftRight = (shiftRight<<1) + expA - (tmp>>14);
+
+ if (shiftRight!=0){
+ if (shiftRight>=29){
+ uZ.ui = uiA;
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+ }
+ else
+ frac32B >>= shiftRight;
+ }
+
+ frac32A -= frac32B;
+
+ while((frac32A>>29)==0){
+ kA--;
+ frac32A<<=2;
+ }
+ ecarry = (0x40000000 & frac32A)>>30;
+ if(!ecarry){
+ if (expA==0) kA--;
+ expA^=1;
+ frac32A<<=1;
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFFFF);
+ regSA = 0;
+ regime = 0x4000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFF - (0x7FFF>>regA);
+ }
+
+ if(regA>14){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac32A = (frac32A & 0x3FFFFFFF) >>(regA + 1) ;
+ fracA = frac32A>>16;
+ if (regA!=14) bitNPlusOne = (frac32A>>15) & 0x1;
+ else if (frac32A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==14 && expA) bitNPlusOne = 1;
+ uZ.ui = packToP16UI(regime, regA, expA, fracA);
+ if (bitNPlusOne){
+ if ( frac32A&0x7FFF ) bitsMore=1;
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_subMagsP32.c b/source/luametatex/source/libraries/softposit/source/s_subMagsP32.c
new file mode 100644
index 000000000..4771626de
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_subMagsP32.c
@@ -0,0 +1,194 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t softposit_subMagsP32( uint_fast32_t uiA, uint_fast32_t uiB ) {
+
+ uint_fast16_t regA, regB;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, ecarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA=0;
+ int_fast16_t shiftRight;
+ union ui32_p32 uZ;
+
+ sign = signP32UI( uiA );
+ if (sign)
+ uiA = -uiA & 0xFFFFFFFF;
+ else
+ uiB = -uiB & 0xFFFFFFFF;
+
+ if (uiA==uiB){ //essential, if not need special handling
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ (sign) ? (sign = 0 ) : (sign=1); //A becomes B
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+ expA = tmp>>29; //to get 2 bits
+ frac64A = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ frac64B = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+
+ //This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<2) + expA - (tmp>>29);
+ if (shiftRight>63){
+ uZ.ui = uiA;
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+ }
+ else
+ (frac64B >>= shiftRight);
+
+ frac64A -= frac64B;
+
+ while((frac64A>>59)==0){
+ kA--;
+ frac64A<<=4;
+ }
+ ecarry = (0x4000000000000000 & frac64A);//(0x4000000000000000 & frac64A)>>62;
+ while (!ecarry){
+ if (expA==0){
+ kA--;
+ expA=3;
+ }
+ else
+ expA--;
+ frac64A<<=1;
+ ecarry = (0x4000000000000000 & frac64A);
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>30){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7FFFFFFF): (uZ.ui=0x1);
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 2) ; // 2 bits exp
+
+ fracA = frac64A>>32;
+
+ if (regA<=28){
+ bitNPlusOne |= (0x80000000 & frac64A) ;
+ expA <<= (28-regA);
+ }
+ else {
+ if (regA==30){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==29){
+ bitNPlusOne = expA&0x1;
+ expA>>=1;
+ }
+ if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+
+ }
+
+ uZ.ui = packToP32UI(regime, expA, fracA);
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (0x7FFFFFFF & frac64A)bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_subMagsP8.c b/source/luametatex/source/libraries/softposit/source/s_subMagsP8.c
new file mode 100644
index 000000000..dd48c4225
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_subMagsP8.c
@@ -0,0 +1,160 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions, and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. Neither the name of the University nor the names of its contributors may
+ be used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
+DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+=============================================================================*/
+
+
+#include "platform.h"
+#include "internals.h"
+
+
+#ifdef SOFTPOSIT_EXACT
+posit8_t softposit_subMagsP8( uint_fast8_t uiA, uint_fast8_t uiB, bool isExact){
+#else
+posit8_t softposit_subMagsP8( uint_fast8_t uiA, uint_fast8_t uiB ){
+#endif
+ uint_fast8_t regA;
+ uint_fast16_t frac16A, frac16B;
+ uint_fast8_t fracA=0, regime, tmp;
+ bool sign=0, regSA, regSB, ecarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast16_t shiftRight;
+ int_fast8_t kA=0;
+ union ui8_p8 uZ;
+
+
+ //Both uiA and uiB are actually the same signs if uiB inherits sign of sub
+ //Make both positive
+ sign = signP8UI( uiA );
+ (sign)? (uiA = (-uiA & 0xFF)): (uiB = (-uiB & 0xFF));
+
+ if (uiA==uiB){ //essential, if not need special handling
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ if(uiA<uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ (sign) ? (sign = 0 ) : (sign=1); //A becomes B
+ }
+
+ regSA = signregP8UI( uiA );
+ regSB = signregP8UI( uiB );
+
+ tmp = (uiA<<2) & 0xFF;
+ if (regSA){
+ while (tmp>>7){
+ kA++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>7)){
+ kA--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16A = (0x80 | tmp) << 7;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFF;
+ if (regSB){
+ while (tmp>>7){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>7)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFF;
+ }
+ tmp&=0x7F;
+ }
+ frac16B = (0x80 | tmp) <<7;
+
+
+ if (shiftRight>=14){
+ uZ.ui = uiA;
+ if (sign) uZ.ui = -uZ.ui & 0xFFFF;
+ return uZ.p;
+ }
+ else
+ frac16B >>= shiftRight;
+
+ frac16A -= frac16B;
+
+ while((frac16A>>14)==0){
+ kA--;
+ frac16A<<=1;
+ }
+ ecarry = (0x4000 & frac16A)>>14;
+ if(!ecarry){
+ kA--;
+ frac16A<<=1;
+ }
+
+ if(kA<0){
+ regA = (-kA & 0xFF);
+ regSA = 0;
+ regime = 0x40>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7F-(0x7F>>regA);
+ }
+
+ if(regA>6){
+ //max or min pos. exp and frac does not matter.
+ (regSA) ? (uZ.ui= 0x7F): (uZ.ui=0x1);
+ }
+ else{
+ frac16A = (frac16A&0x3FFF) >> regA;
+ fracA = (uint_fast8_t) (frac16A>>8);
+ bitNPlusOne = (0x80 & frac16A) ;
+ uZ.ui = packToP8UI(regime, fracA);
+
+ if (bitNPlusOne){
+ if (0x7F & frac16A) bitsMore=1;
+ uZ.ui += (uZ.ui&1) | bitsMore;
+ }
+ }
+ if (sign) uZ.ui = -uZ.ui & 0xFF;
+ return uZ.p;
+
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_subMagsPX1.c b/source/luametatex/source/libraries/softposit/source/s_subMagsPX1.c
new file mode 100644
index 000000000..e6b6b4837
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_subMagsPX1.c
@@ -0,0 +1,202 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t softposit_subMagsPX1( uint_fast32_t uiA, uint_fast32_t uiB, int x ) {
+
+ int regA;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, ecarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA=0;
+ int_fast16_t shiftRight;
+ union ui32_pX1 uZ;
+
+ sign = signP32UI( uiA );
+ if (sign)
+ uiA = -uiA & 0xFFFFFFFF;
+ else
+ uiB = -uiB & 0xFFFFFFFF;
+
+ if (uiA==uiB){ //essential, if not need special handling
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ (sign) ? (sign = 0 ) : (sign=1); //A becomes B
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ if (x==2){
+ uZ.ui = (regSA==regSB) ? (0x0): (0x40000000) ;
+ }
+ else{
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+
+ expA = tmp>>30; //to get 1 bits
+ frac64A = ((0x40000000ULL | tmp) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ frac64B = ((0x40000000ULL | tmp) & 0x7FFFFFFFULL) <<32;
+ //This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<1) + expA - (tmp>>30);
+ if (shiftRight>60){
+ uZ.ui = uiA;
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+ }
+ else
+ (frac64B >>= shiftRight);
+
+ frac64A -= frac64B;
+
+ while((frac64A>>61)==0){
+ kA--;
+ frac64A<<=2;
+ }
+ ecarry = (0x4000000000000000 & frac64A);//(0x4000000000000000 & frac64A)>>62;
+ if (!ecarry){
+ if (expA==0) kA--;
+ expA^=1;
+ frac64A<<=1;
+ }
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 1) ; // 2 bits exp
+ fracA = frac64A>>32;
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+ bitNPlusOne |= (((uint64_t)0x80000000<<(32-x)) & frac64A) ;
+ //expA <<= (28-regA);
+ }
+ else {
+ if (regA!=(x-2))
+ bitNPlusOne |= (((uint64_t)0x8000000000000000>>x) & frac64A);
+ else if (frac64A>0){
+ fracA=0;
+ bitsMore =1;
+ }
+ if (regA==(x-2) && expA){
+ bitNPlusOne = 1;
+ expA=0;
+ }
+
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+ fracA &=((int32_t)0x80000000>>(x-1));
+
+ expA <<= (29-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if ((0x7FFFFFFFFFFFFFFF>>x) & frac64A) bitsMore=1;
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/s_subMagsPX2.c b/source/luametatex/source/libraries/softposit/source/s_subMagsPX2.c
new file mode 100644
index 000000000..4a1106c23
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/s_subMagsPX2.c
@@ -0,0 +1,213 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017, 2018 A*STAR. All rights reserved.
+
+This C source 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.
+
+=============================================================================*/
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t softposit_subMagsPX2( uint_fast32_t uiA, uint_fast32_t uiB, int x ) {
+
+ int regA;
+ uint_fast64_t frac64A=0, frac64B=0;
+ uint_fast32_t fracA=0, regime, tmp;
+ bool sign, regSA, regSB, ecarry=0, bitNPlusOne=0, bitsMore=0;
+ int_fast8_t kA=0;
+ int_fast32_t expA=0;
+ int_fast16_t shiftRight;
+ union ui32_pX2 uZ;
+
+ sign = signP32UI( uiA );
+ if (sign)
+ uiA = -uiA & 0xFFFFFFFF;
+ else
+ uiB = -uiB & 0xFFFFFFFF;
+
+ if (uiA==uiB){ //essential, if not need special handling
+ uZ.ui = 0;
+ return uZ.p;
+ }
+ if ((int_fast32_t)uiA < (int_fast32_t)uiB){
+ uiA ^= uiB;
+ uiB ^= uiA;
+ uiA ^= uiB;
+ (sign) ? (sign = 0 ) : (sign=1); //A becomes B
+ }
+ regSA = signregP32UI( uiA );
+ regSB = signregP32UI( uiB );
+
+ if (x==2){
+ uZ.ui = (regSA==regSB) ? (0x0): (0x40000000) ;
+ }
+ else{
+
+ tmp = (uiA<<2)&0xFFFFFFFF;
+ if (regSA){
+ while (tmp>>31){
+ kA++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ }
+ else{
+ kA=-1;
+ while (!(tmp>>31)){
+ kA--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+ }
+
+
+ expA = tmp>>29; //to get 2 bits
+ frac64A = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ shiftRight = kA;
+
+ tmp = (uiB<<2) & 0xFFFFFFFF;
+ if (regSB){
+ while (tmp>>31){
+ shiftRight--;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+
+ }
+ else{
+ shiftRight++;
+ while (!(tmp>>31)){
+ shiftRight++;
+ tmp= (tmp<<1) & 0xFFFFFFFF;
+ }
+ tmp&=0x7FFFFFFF;
+
+ }
+ frac64B = ((0x40000000ULL | tmp<<1) & 0x7FFFFFFFULL) <<32;
+ //This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
+ shiftRight = (shiftRight<<2) + expA - (tmp>>29);
+ if (shiftRight>63){
+ uZ.ui = uiA;
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+ }
+ else
+ (frac64B >>= shiftRight);
+
+ frac64A -= frac64B;
+
+ while((frac64A>>59)==0){
+ kA--;
+ frac64A<<=4;
+ }
+ ecarry = (0x4000000000000000 & frac64A);//(0x4000000000000000 & frac64A)>>62;
+ while (!ecarry){
+ if (expA==0){
+ kA--;
+ expA=3;
+ }
+ else
+ expA--;
+ frac64A<<=1;
+ ecarry = (0x4000000000000000 & frac64A);
+ }
+
+
+
+ if(kA<0){
+ regA = -kA;
+ regSA = 0;
+ regime = 0x40000000>>regA;
+ }
+ else{
+ regA = kA+1;
+ regSA=1;
+ regime = 0x7FFFFFFF - (0x7FFFFFFF>>regA);
+ }
+ if(regA>(x-2)){
+ //max or min pos. exp and frac does not matter.
+ uZ.ui=(regSA) ? (0x7FFFFFFF & ((int32_t)0x80000000>>(x-1)) ): (0x1 << (32-x));
+ }
+ else{
+ //remove hidden bits
+ frac64A = (frac64A & 0x3FFFFFFFFFFFFFFF) >>(regA + 2) ; // 2 bits exp
+ fracA = frac64A>>32;
+
+ //regime length is smaller than length of posit
+ if (regA<x){
+ if (regA<=(x-4)){
+ bitNPlusOne |= (((uint64_t)0x80000000<<(32-x)) & frac64A) ;
+ //expA <<= (28-regA);
+ }
+ else {
+ if (regA==(x-2)){
+ bitNPlusOne = expA&0x2;
+ bitsMore = (expA&0x1);
+ expA = 0;
+ }
+ else if (regA==(x-3)){
+ bitNPlusOne = expA&0x1;
+ //expA>>=1;
+ expA &=0x2;
+ }
+ if (fracA>0){
+ fracA=0;
+ bitsMore =1;
+ }
+
+ }
+ }
+ else{
+ regime=(regSA) ? (regime & ((int32_t)0x80000000>>(x-1)) ): (regime << (32-x));
+ expA=0;
+ fracA=0;
+ }
+ fracA &=((int32_t)0x80000000>>(x-1));
+
+ expA <<= (28-regA);
+ uZ.ui = packToP32UI(regime, expA, fracA);
+
+ //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
+ if (bitNPlusOne){
+ if (((uint64_t)0xFFFFFFFFFFFFFFFF>>(x+1)) & frac64A) bitsMore=1;
+ uZ.ui += (uint32_t)(((uZ.ui>>(32-x))&1) | bitsMore) << (32-x) ;
+ }
+ }
+ }
+
+ if (sign) uZ.ui = -uZ.ui & 0xFFFFFFFF;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/ui32_to_p16.c b/source/luametatex/source/libraries/softposit/source/ui32_to_p16.c
new file mode 100644
index 000000000..3e1f0b546
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui32_to_p16.c
@@ -0,0 +1,74 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t ui32_to_p16( uint32_t a ){
+ int_fast8_t k, log2 = 25;
+ union ui16_p16 uZ;
+ uint_fast16_t uiA;
+ uint_fast32_t expA, mask = 0x02000000, fracA;
+
+ if ( a > 0x08000000 ) uiA = 0x7FFF; //134217729 to MAX_INT round to maxpos
+ else if ( a > 0x02FFFFFF ) uiA = 0x7FFE;
+ else if ( a < 2 ) uiA = (a << 14);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = log2 >> 1;
+ expA = (log2 & 0x1) << (12 - k);
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7FFF ^ (0x3FFF >> k)) | expA | ( fracA >> (k + 13));
+ mask = 0x1000 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui32_to_p32.c b/source/luametatex/source/libraries/softposit/source/ui32_to_p32.c
new file mode 100644
index 000000000..d35ca0429
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui32_to_p32.c
@@ -0,0 +1,77 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t ui32_to_p32( uint32_t a ) {
+ int_fast8_t k, log2 = 31;//length of bit (e.g. 4294966271) in int (32 but because we have only 32 bits, so one bit off to accomdate that fact)
+ union ui32_p32 uZ;
+ uint_fast32_t uiA;
+ uint_fast32_t expA, mask = 0x80000000, fracA;
+
+ if ( a > 4294966271)
+ uiA = 0x7FC00000; // 4294967296
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = (log2 >> 2);
+ expA = (log2 & 0x3) << (27 - k);
+ fracA = (fracA ^ mask);
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | expA | fracA>>(k+4);
+
+ mask = 0x8 << k; //bitNPlusOne
+
+ if (mask & fracA)
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/ui32_to_p8.c b/source/luametatex/source/libraries/softposit/source/ui32_to_p8.c
new file mode 100644
index 000000000..5b0c3fefc
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui32_to_p8.c
@@ -0,0 +1,75 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t ui32_to_p8( uint32_t a ){
+ int_fast8_t k, log2 = 6;//length of bit
+ union ui8_p8 uZ;
+ uint_fast8_t uiA;
+ uint_fast32_t mask = 0x40, fracA;
+
+ if ( a > 48 ) uiA = 0x7F;
+ else if ( a < 2 ) uiA = (a << 6);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = log2;
+
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7F ^ (0x3F >> k)) | ( fracA >> (k+1) ) ;
+
+ mask = 0x1 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui32_to_pX2.c b/source/luametatex/source/libraries/softposit/source/ui32_to_pX2.c
new file mode 100644
index 000000000..8cb6c635b
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui32_to_pX2.c
@@ -0,0 +1,113 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t ui32_to_pX2( uint32_t a, int x ) {
+ int_fast8_t k, log2 = 31;//length of bit (e.g. 4294966271) in int (32 but because we have only 32 bits, so one bit off to accomdate that fact)
+ union ui32_pX2 uZ;
+ uint_fast32_t uiA=0;
+ uint_fast32_t expA, mask = 0x80000000, fracA;
+
+ //NaR
+ if (a == 0x80000000 || x<2 || x>32)
+ uiA = 0x80000000;
+ else if (x==2){
+ if (a>0) uiA=0x40000000;
+ }
+ else if ( a > 0xFFFFFBFF){//4294966271
+ uiA = 0x7FC00000; // 4294967296
+ if (x<12) uiA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if ( a < 0x2 ){
+ uiA = (a << 30);
+ }
+ else {
+ fracA = a;
+
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = (log2 >> 2);
+ expA = (log2 & 0x3) ;
+ fracA = (fracA ^ mask);
+
+ if(k>=(x-2)){//maxpos
+ uiA = 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+
+ }
+ else if (k==(x-3)){//bitNPlusOne-> first exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+ if( (expA & 0x2) && ((expA&0x1) | fracA) ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | ((expA &0x2)<< (27 - k));
+ if(expA&0x1){
+ if( (((uint32_t)0x80000000>>(x-1)) & uiA)| fracA)
+ uiA += ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ else if (k==(x-5)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k));
+ mask = 0x8 << (k -x);
+ if (mask & fracA){ //bitNPlusOne
+ if (((mask - 1) & fracA) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = ((0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k)) | fracA>>(k+4)) & ((int32_t)0x80000000>>(x-1));;
+ mask = 0x8 << (k-x); //bitNPlusOne
+ if (mask & fracA)
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
+
diff --git a/source/luametatex/source/libraries/softposit/source/ui64_to_p16.c b/source/luametatex/source/libraries/softposit/source/ui64_to_p16.c
new file mode 100644
index 000000000..b6620114c
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui64_to_p16.c
@@ -0,0 +1,74 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit16_t ui64_to_p16( uint64_t a ) {
+ int_fast8_t k, log2 = 25;
+ union ui16_p16 uZ;
+ uint_fast16_t uiA;
+ uint_fast64_t expA, mask = 0x0000000002000000, fracA;
+
+ if ( a > 0x0000000008000000 ) uiA = 0x7FFF; //134217729 to MAX_INT round to maxpos
+ else if ( a > 0x0000000002FFFFFF ) uiA = 0x7FFE;
+ else if ( a < 2 ) uiA = (a << 14);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+ k = log2 >> 1;
+ expA = (log2 & 0x1) << (12 - k);
+ fracA = fracA ^ mask;
+ uiA = (0x7FFF ^ (0x3FFF >> k)) | expA | (fracA >> (k + 13));
+ mask = 0x1000 << k;
+ if (mask & fracA) {
+ if ( ((mask - 1) & fracA) | ((mask << 1) & fracA) ) uiA++;
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui64_to_p32.c b/source/luametatex/source/libraries/softposit/source/ui64_to_p32.c
new file mode 100644
index 000000000..c32a00548
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui64_to_p32.c
@@ -0,0 +1,82 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit32_t ui64_to_p32( uint64_t a ) {
+
+ int_fast8_t k, log2 = 63;//length of bit (e.g. 18445618173802708992) in int (64 but because we have only 64 bits, so one bit off to accommodate that fact)
+ union ui32_p32 uZ;
+ uint_fast64_t uiA;
+ uint_fast64_t mask = 0x8000000000000000, fracA;
+ uint_fast32_t expA;
+
+
+ if ( a > 18445618173802708991ULL)//0xFFFBFFFFFFFFFFFF is the midpoint
+ uiA = 0x7FFFC000; // P32: 18446744073709552000
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = (log2 >> 2);
+
+ expA = (log2 & 0x3) << (27 - k);
+ fracA = (fracA ^ mask);
+
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | expA | fracA>>(k+36);
+
+ mask = 0x800000000 << k; //bitNPlusOne
+
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui64_to_p8.c b/source/luametatex/source/libraries/softposit/source/ui64_to_p8.c
new file mode 100644
index 000000000..ec931ffdf
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui64_to_p8.c
@@ -0,0 +1,73 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane) and John Gustafson.
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit8_t ui64_to_p8( uint64_t a ){
+ int_fast8_t k, log2 = 6;//length of bit
+ union ui8_p8 uZ;
+ uint_fast8_t uiA;
+ uint_fast64_t mask = 0x40, fracA;
+
+ if ( a > 48 ) uiA = 0x7F;
+ else if ( a < 2 ) uiA = (a << 6);
+ else {
+ fracA = a;
+ while ( !(fracA & mask) ) {
+ log2--;
+ fracA <<= 1;
+ }
+
+ k = log2;
+ fracA = (fracA ^ mask);
+ uiA = (0x7F ^ (0x3F >> k)) | ( fracA >> (k+1) ) ;
+
+ mask = 0x1 << k; //bitNPlusOne
+ if (mask & fracA) {
+ if (((mask - 1) & fracA) | ((mask << 1) & fracA)) uiA++;
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui64_to_pX1.c b/source/luametatex/source/libraries/softposit/source/ui64_to_pX1.c
new file mode 100644
index 000000000..663983084
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui64_to_pX1.c
@@ -0,0 +1,124 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit_1_t ui64_to_pX1 ( uint64_t a, int x ) {
+ int_fast8_t k, log2 = 63;//60;//length of bit (e.g. 576460752303423488 = 2^59) in int (64 but because we have only 64 bits, so one bit off to accommodate that fact)
+ union ui32_pX1 uZ;
+ uint_fast64_t uiA=0;
+ uint_fast64_t mask = 0x8000000000000000, frac64A;
+ uint_fast32_t expA;
+
+ //NaR
+ if (a == 0x8000000000000000 || x<2 || x>32)
+ uiA = 0x80000000;
+ else if (x==2){
+ if (a>0) uiA=0x40000000;
+ }
+ else if ( a > 0x8000000000000000){//576460752303423488 -> wrong number need to change
+uint64_t test = ((uint64_t)0x80000000>>(x-1));
+printBinary(&test, 32);
+ uiA = 0x7FFFFFFF & ((uint64_t)0x80000000>>(x-1)); // 1152921504606847000
+ }
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ frac64A = a;
+//printBinary(&frac64A, 64);
+ while ( !(frac64A & mask) ) {
+ log2--;
+ frac64A <<= 1;
+ }
+//printf("after regime:\n");
+//printBinary(&frac64A, 64);
+ k = (log2 >> 1);
+
+ expA = (log2 & 0x1) ;
+//printf("expA:\n");
+//printBinary(&expA, 32);
+ frac64A = (frac64A ^ mask) <<1;
+//printf("frac64A:\n");
+//printBinary(&frac64A, 64);
+//printf("log2: %d k: %d\n", log2, k);
+
+
+
+ if(k>=(x-2)){//maxpos
+ uiA = 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-3)){//bitNPlusOne-> exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+//printBinary(&a, 64);
+//printBinary(&uiA, 32);
+ if( (expA & 0x1) && frac64A ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){ //bitLast = regime terminating bit
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (28 - k));
+//printBinary(&a, 64);
+//printBinary(&uiA, 32);
+ mask = (uint64_t)0x800000000 << (k + 32-x);
+//printBinary(&mask, 64);
+//printBinary(&frac64A, 64);
+ if (mask & frac64A){ //bitNPlusOne
+ if (((mask - 1) & frac64A) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (28 - k)) | ((frac64A>>(k+36)) & ((int32_t)0x80000000>>(x-1)));
+//printBinary(&uiA, 32);
+ mask = (uint64_t)0x800000000 << (k + 32-x); //bitNPlusOne position
+ if (mask & frac64A) {
+ if (((mask - 1) & frac64A) | ((mask << 1) & frac64A)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}
diff --git a/source/luametatex/source/libraries/softposit/source/ui64_to_pX2.c b/source/luametatex/source/libraries/softposit/source/ui64_to_pX2.c
new file mode 100644
index 000000000..1250d26fd
--- /dev/null
+++ b/source/luametatex/source/libraries/softposit/source/ui64_to_pX2.c
@@ -0,0 +1,115 @@
+
+/*============================================================================
+
+This C source file is part of the SoftPosit Posit Arithmetic Package
+by S. H. Leong (Cerlane).
+
+Copyright 2017 2018 A*STAR. All rights reserved.
+
+This C source file was based on SoftFloat IEEE Floating-Point Arithmetic
+Package, Release 3d, by John R. Hauser.
+
+Copyright 2011, 2012, 2013, 2014, 2015, 2016 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.
+
+=============================================================================*/
+
+#include <stdint.h>
+
+#include "platform.h"
+#include "internals.h"
+
+posit_2_t ui64_to_pX2 ( uint64_t a, int x ) {
+ int_fast8_t k, log2 = 63;//length of bit (e.g. 18445618173802708991) in int (64 but because we have only 64 bits, so one bit off to accommodate that fact)
+ union ui32_pX2 uZ;
+ uint_fast64_t uiA=0;
+ uint_fast64_t mask = 0x8000000000000000, frac64A;
+ uint_fast32_t expA;
+
+ //NaR
+ if (a == 0x8000000000000000 || x<2 || x>32)
+ uiA = 0x80000000;
+ else if (x==2){
+ if (a>0) uiA=0x40000000;
+ }
+ else if ( a > 0xFFFBFFFFFFFFFFFF){//18445618173802708991
+ uiA = 0x7FFFC000; // 18446744073709552000
+ if (x<18) uiA&=((int32_t)0x80000000>>(x-1));
+ }
+ else if ( a < 0x2 )
+ uiA = (a << 30);
+ else {
+ frac64A = a;
+ while ( !(frac64A & mask) ) {
+ log2--;
+ frac64A <<= 1;
+ }
+
+ k = (log2 >> 2);
+
+ expA = (log2 & 0x3) ;
+ frac64A = (frac64A ^ mask);
+
+ if(k>=(x-2)){//maxpos
+ uiA = 0x7FFFFFFF & ((int32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-3)){//bitNPlusOne-> first exp bit //bitLast is zero
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k));
+ if( (expA & 0x2) && ((expA&0x1) | frac64A) ) //bitNPlusOne //bitsMore
+ uiA |= ((uint32_t)0x80000000>>(x-1));
+ }
+ else if (k==(x-4)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | ((expA &0x2)<< (27 - k));
+ if(expA&0x1){
+ if( (((uint32_t)0x80000000>>(x-1)) & uiA)|| frac64A)
+ uiA += ((uint32_t)0x80000000>>(x-1));
+ }
+
+ }
+ else if (k==(x-5)){
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k));
+ mask = (uint64_t)0x800000000 << (k + 32-x);
+ if (mask & frac64A){ //bitNPlusOne
+ if (((mask - 1) & frac64A) | (expA&0x1)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ else{
+ uiA = (0x7FFFFFFF ^ (0x3FFFFFFF >> k)) | (expA<< (27 - k)) | ((frac64A>>(k+36)) & ((int32_t)0x80000000>>(x-1)));
+ mask = (uint64_t)0x800000000 << (k + 32-x); //bitNPlusOne position
+ if (mask & frac64A) {
+ if (((mask - 1) & frac64A) | ((mask << 1) & frac64A)) {
+ uiA+= ((uint32_t)0x80000000>>(x-1));
+ }
+ }
+ }
+ }
+ uZ.ui = uiA;
+ return uZ.p;
+}