1 #pragma once 2 // positctypes.h: generic C header defining the posit types 3 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <stdbool.h> 7 #include <stdint.h> 8 9 #ifdef __cplusplus 10 // export a C interface if used by C++ source code 11 extern "C" { 12 #endif 13 14 ////////////////////////////////////////////////////////////////////// 15 /// Standard posit configuration per the POSIT standard 16 typedef union posit8_u { 17 uint8_t x[1]; 18 uint8_t v; 19 } posit8_t; // posit<8,0> 20 typedef union posit16_u { 21 uint8_t x[2]; 22 uint16_t v; 23 } posit16_t; // posit<16,1> 24 typedef union posit32_u { 25 uint8_t x[4]; 26 uint32_t v; 27 } posit32_t; // posit<32,2> 28 typedef union posit64_u { 29 uint8_t x[8]; 30 uint64_t v; 31 } posit64_t; // posit<64,3> 32 typedef union posit128_u { 33 uint8_t x[16]; 34 uint64_t longs[2]; 35 } posit128_t; // posit<128,4> 36 typedef union posit256_u { 37 uint8_t x[32]; 38 uint64_t longs[4]; 39 } posit256_t; // posit<256,5> 40 41 // these storage formats are the same, but interpretation of the bits is specialized 42 typedef posit8_t posit8_0_t; 43 typedef posit8_t posit8_1_t; 44 typedef posit8_t posit8_2_t; 45 typedef posit8_t posit8_3_t; 46 typedef posit16_t posit16_0_t; 47 typedef posit16_t posit16_1_t; 48 typedef posit16_t posit16_2_t; 49 typedef posit16_t posit16_3_t; 50 typedef posit32_t posit32_0_t; 51 typedef posit32_t posit32_1_t; 52 typedef posit32_t posit32_2_t; 53 typedef posit32_t posit32_3_t; 54 typedef posit64_t posit64_0_t; 55 typedef posit64_t posit64_1_t; 56 typedef posit64_t posit64_2_t; 57 typedef posit64_t posit64_3_t; 58 59 /////////////////////////////////////////////////////////////////////// 60 /// associated quire configurations 61 typedef union quire8_u { 62 uint8_t x[8]; 63 uint64_t v; 64 } quire8_t; // quire<8,0,39> 65 typedef union quire16_u { 66 uint8_t x[16]; 67 uint64_t v[2]; 68 } quire16_t; // quire<16,1,15> 69 typedef union quire32_u { 70 uint8_t x[64]; 71 uint64_t v[8]; 72 } quire32_t; // quire<32,2,31> 73 typedef union quire64_u { 74 uint8_t x[256]; 75 uint64_t v[32]; 76 } quire64_t; // quire<64,3,63> 77 typedef union quire128_u { 78 uint8_t x[1024]; 79 uint64_t v[128]; 80 } quire128_t; // quire<128,4,127> 81 typedef union quire256_u { 82 uint8_t x[4096]; 83 uint64_t v[512]; 84 } quire256_t; // quire<256,5,255> 85 86 /// quire< 8, 0, 7> 32 bits <--- likely not enough capacity bits 87 /// quire< 16, 1, 15> 128 bits 88 /// quire< 32, 2, 31> 512 bits 89 /// quire< 64, 3, 63> 2048 bits 90 /// quire<128, 4, 127> 8192 bits <--- likely too many capacity bits 91 /// quire<256, 5, 7> 32520 bits <--- 4065 bytes: smallest size aligned to 4byte boundary 92 /// quire<256, 5, 255> 32768 bits <--- 4096 bytes 93 94 ////////////////////////////////////////////////////////////////////// 95 /// special posits 96 97 ////////////////////////////////////////////////////////////////////// 98 // for Deep Learning/AI algorithms 99 typedef union posit4_u { 100 uint8_t x[1]; 101 uint8_t v; 102 } posit4_t; // posit<4,0> 103 #ifdef DEEP_LEARNING 104 typedef union posit5_u { 105 uint8_t x[1]; 106 uint8_t v; 107 } posit5_t; // posit<5,0> 108 typedef union posit6_u { 109 uint8_t x[1]; 110 uint8_t v; 111 } posit6_t; // posit<6,0> 112 typedef union posit7_u { 113 uint8_t x[1]; 114 uint8_t v; 115 } posit7_t; // posit<7,0> 116 #endif // DEEP_LEARNING 117 118 #ifdef DSP_PIPELINES 119 ////////////////////////////////////////////////////////////////////// 120 // for DSP applications and ADC/DAC pipelines 121 typedef union posit9_u { 122 uint8_t x[2]; 123 uint16_t v; 124 } posit9_t; // posit<9,0> 125 typedef union posit10_u { 126 uint8_t x[2]; 127 uint16_t v; 128 } posit10_t; // posit<10,0> 129 typedef union posit12_u { 130 uint8_t x[2]; 131 uint16_t v; 132 } posit12_t; // posit<12,0> 133 typedef union posit14_u { 134 uint8_t x[2]; 135 uint16_t v; 136 } posit14_t; // posit<14,0> 137 #endif // DSP_PIPELINES 138 139 #ifdef EXTENDED_STANDARD 140 ////////////////////////////////////////////////////////////////////// 141 // for Linear Algebra and general CAD/CAE/CAM/HPC applications 142 143 ////////////////////////////////////////////////////////////////////// 144 // posits between posit<16,1> and posit<32,2> staying with ES = 1 145 typedef union posit20_u { 146 uint8_t x[4]; 147 uint32_t v; 148 } posit20_t; // posit<20,1> 149 typedef union posit24_u { 150 uint8_t x[4]; 151 uint32_t v; 152 } posit24_t; // posit<24,1> 153 154 // posits between posit<32,2> and posit<64,3> staying with ES = 2 155 // notice we keep the cast to a uint64_t 156 typedef union posit40_u { 157 uint8_t x[8]; 158 uint64_t v; 159 } posit40_t; // posit<40,2> 160 typedef union posit48_u { 161 uint8_t x[8]; 162 uint64_t v; 163 } posit48_t; // posit<48,2> 164 typedef union posit56_u { 165 uint8_t x[8]; 166 uint64_t v; 167 } posit56_t; // posit<56,2> 168 169 ////////////////////////////////////////////////////////////////////// 170 // posits between posit<64,3> and posit<128,4> staying with ES = 3 171 typedef union posit80_u { 172 uint8_t x[10]; 173 //uint64_t v[2];// if we cast it to exactly 10 bytes, this cast would not work 174 } posit80_t; // posit<80,3> 175 typedef union posit96_u { 176 uint8_t x[12]; 177 //uint64_t v[2]; 178 } posit96_t; // posit<96,3> 179 typedef union posit96_u { 180 uint8_t x[14]; 181 //uint64_t v[2]; 182 } posit112_t; // posit<112,3> 183 #endif // EXTENDED_STANDARD 184 185 ////////////////////////////////////////////////////////////////////// 186 // C API function definitions 187 188 ////////////////////////////////////////////////////////////////////// 189 // Important posit constants // we a storing this in little endian 190 static const posit4_t NAR4 = {{ 191 0x08 192 }}; 193 static const posit8_t NAR8 = {{ 194 0x80 195 }}; 196 static const posit16_t NAR16 = {{ 197 0x00, 0x80 198 }}; 199 static const posit32_t NAR32 = {{ 200 0x00, 0x00, 0x00, 0x80 201 }}; 202 static const posit64_t NAR64 = {{ 203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 204 }}; 205 static const posit128_t NAR128 = {{ 206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 208 }}; 209 static const posit256_t NAR256 = {{ 210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 214 }}; 215 216 static const posit4_t ZERO4 = {{ 0 }}; 217 static const posit8_t ZERO8 = {{ 0 }}; 218 static const posit16_t ZERO16 = {{ 0 }}; 219 static const posit32_t ZERO32 = {{ 0 }}; 220 static const posit64_t ZERO64 = {{ 0 }}; 221 static const posit128_t ZERO128 = {{ 0 }}; 222 static const posit256_t ZERO256 = {{ 0 }}; 223 224 225 #ifdef __cplusplus 226 } 227 #endif 228