haskell-posit
Haskell bindings for the SoftPosit C library 🧮 (WIP)
lib.c (1370B)
1 #include "softposit.h" 2 #include <stdint.h> 3 #include <stdbool.h> 4 5 #define DEFINE_INT_TO_POSIT(N_BITS) \ 6 uint##N_BITS##_t int_to_posit##N_BITS (int64_t x) \ 7 { \ 8 return castUI(i64_to_p##N_BITS (x)); \ 9 } 10 11 #define DEFINE_BIN_OP(N_BITS, OP) \ 12 uint##N_BITS##_t posit##N_BITS##_##OP (uint##N_BITS##_t a, uint##N_BITS##_t b) \ 13 { \ 14 posit##N_BITS##_t pa, pb; \ 15 \ 16 pa = castP##N_BITS (a); \ 17 pb = castP##N_BITS (b); \ 18 \ 19 return castUI(p##N_BITS##_##OP (pa, pb)); \ 20 } 21 22 #define DEFINE_NEG(N_BITS) \ 23 uint##N_BITS##_t posit##N_BITS##neg (uint##N_BITS##_t a) \ 24 { \ 25 posit##N_BITS##_t pa = castP##N_BITS (a); \ 26 return castUI(negP##N_BITS (pa)); \ 27 } 28 29 30 #define DEFINE_ARITH_OPS(N_BITS) \ 31 DEFINE_BIN_OP(N_BITS, add) \ 32 DEFINE_BIN_OP(N_BITS, sub) \ 33 DEFINE_BIN_OP(N_BITS, mul) \ 34 DEFINE_BIN_OP(N_BITS, div) \ 35 DEFINE_NEG(N_BITS) 36 37 #define DEFINE_COMP_OP(N_BITS, OP) \ 38 bool posit##N_BITS##_##OP (uint##N_BITS##_t a, uint##N_BITS##_t b) \ 39 { \ 40 posit##N_BITS##_t pa, pb; \ 41 \ 42 pa = castP##N_BITS (a); \ 43 pb = castP##N_BITS (b); \ 44 \ 45 return p##N_BITS##_##OP (pa, pb); \ 46 } 47 48 #define DEFINE_COMP_OPS(N_BITS) \ 49 DEFINE_COMP_OP(N_BITS, eq) \ 50 DEFINE_COMP_OP(N_BITS, le) \ 51 DEFINE_COMP_OP(N_BITS, lt) 52 53 #define DEFINE_ALL_OPS(N_BITS) \ 54 DEFINE_INT_TO_POSIT(N_BITS) \ 55 DEFINE_ARITH_OPS(N_BITS) \ 56 DEFINE_COMP_OPS(N_BITS) 57 58 DEFINE_ALL_OPS(8) 59 DEFINE_ALL_OPS(16) 60 DEFINE_ALL_OPS(32) 61