fast-posit 0.2.0

Software implementation of the Posit floating point format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Re-export some internals for benchmarking purposes; available with feature = "bench".

use crate::posit::{Posit, Decoded};
use crate::{RoundFrom, RoundInto};

impl<
  const N: u32,
  const ES: u32,
  Int: crate::Int,
> Posit<N, ES, Int> {
  pub unsafe fn bench_decode_regular(self) -> Decoded<N, ES, N, Int> {
    unsafe { self.decode_regular() }
  }

  pub unsafe fn bench_add_kernel(a: Decoded<N, ES, N, Int>, b: Decoded<N, ES, N, Int>) -> (Decoded<N, ES, N, Int>, Int) {
    unsafe { Self::add_kernel(a, b) }
  }

  pub unsafe fn bench_mul_kernel(a: Decoded<N, ES, N, Int>, b: Decoded<N, ES, N, Int>) -> (Decoded<N, ES, N, Int>, Int) {
    unsafe { Self::mul_kernel(a, b) }
  }

  pub unsafe fn bench_div_kernel(a: Decoded<N, ES, N, Int>, b: Decoded<N, ES, N, Int>) -> (Decoded<N, ES, N, Int>, Int) {
    unsafe { Self::div_kernel(a, b) }
  }

  pub unsafe fn bench_sqrt_kernel(a: Decoded<N, ES, N, Int>) -> (Decoded<N, ES, N, Int>, Int) {
    unsafe { Self::sqrt_kernel(a) }
  }
}

impl<
  const N: u32,
  const ES: u32,
  Int: crate::Int,
> Decoded<N, ES, N, Int> {
  pub unsafe fn bench_encode_regular(self) -> Posit<N, ES, Int> {
    unsafe { self.encode_regular() }
  }

  pub unsafe fn bench_encode_regular_round(self, sticky: Int) -> Posit<N, ES, Int> {
    unsafe { self.encode_regular_round(sticky) }
  }
}

// Export these for inspection with `cargo asm`.

#[unsafe(no_mangle)]
pub fn decode_8(arg: Posit<8, 2, i8>) -> Decoded<8, 2, 8, i8> {
  unsafe { arg.decode_regular() }
}

#[unsafe(no_mangle)]
pub fn decode_16(arg: Posit<16, 2, i16>) -> Decoded<16, 2, 16, i16> {
  unsafe { arg.decode_regular() }
}

#[unsafe(no_mangle)]
pub fn decode_32(arg: Posit<32, 2, i32>) -> Decoded<32, 2, 32, i32> {
  unsafe { arg.decode_regular() }
}

#[unsafe(no_mangle)]
pub fn decode_64(arg: Posit<64, 2, i64>) -> Decoded<64, 2, 64, i64> {
  unsafe { arg.decode_regular() }
}

#[unsafe(no_mangle)]
pub fn decode_b16(arg: Posit<16, 5, i16, 6>) -> Decoded<16, 5, 6, i16> {
  unsafe { arg.decode_regular() }
}

#[unsafe(no_mangle)]
pub fn decode_b32(arg: Posit<32, 5, i32, 6>) -> Decoded<32, 5, 6, i32> {
  unsafe { arg.decode_regular() }
}

//

#[unsafe(no_mangle)]
pub fn encode_8(arg: Decoded<8, 2, 8, i8>, sticky: i8) -> Posit<8, 2, i8> {
  unsafe { arg.encode_regular_round(sticky) }
}

#[unsafe(no_mangle)]
pub fn encode_16(arg: Decoded<16, 2, 16, i16>, sticky: i16) -> Posit<16, 2, i16> {
  unsafe { arg.encode_regular_round(sticky) }
}

#[unsafe(no_mangle)]
pub fn encode_32(arg: Decoded<32, 2, 32, i32>, sticky: i32) -> Posit<32, 2, i32> {
  unsafe { arg.encode_regular_round(sticky) }
}

#[unsafe(no_mangle)]
pub fn encode_64(arg: Decoded<64, 2, 64, i64>, sticky: i64) -> Posit<64, 2, i64> {
  unsafe { arg.encode_regular_round(sticky) }
}

#[unsafe(no_mangle)]
pub fn encode_b16(arg: Decoded<16, 5, 6, i16>, sticky: i16) -> Posit<16, 5, i16, 6> {
  unsafe { arg.encode_regular_round(sticky) }
}

#[unsafe(no_mangle)]
pub fn encode_b32(arg: Decoded<32, 5, 6, i32>, sticky: i32) -> Posit<32, 5, i32, 6> {
  unsafe { arg.encode_regular_round(sticky) }
}

//

#[unsafe(no_mangle)]
pub fn add_kernel_8(x: Decoded<8, 2, 8, i8>, y: Decoded<8, 2, 8, i8>) -> (Decoded<8, 2, 8, i8>, i8) {
  unsafe { Posit::<8, 2, i8>::add_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn add_kernel_16(x: Decoded<16, 2, 16, i16>, y: Decoded<16, 2, 16, i16>) -> (Decoded<16, 2, 16, i16>, i16) {
  unsafe { Posit::<16, 2, i16>::add_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn add_kernel_32(x: Decoded<32, 2, 32, i32>, y: Decoded<32, 2, 32, i32>) -> (Decoded<32, 2, 32, i32>, i32) {
  unsafe { Posit::<32, 2, i32>::add_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn add_kernel_64(x: Decoded<64, 2, 64, i64>, y: Decoded<64, 2, 64, i64>) -> (Decoded<64, 2, 64, i64>, i64) {
  unsafe { Posit::<64, 2, i64>::add_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn add_8(x: Posit<8, 2, i8>, y: Posit<8, 2, i8>) -> Posit<8, 2, i8> {
  x + y
}

#[unsafe(no_mangle)]
pub fn add_16(x: Posit<16, 2, i16>, y: Posit<16, 2, i16>) -> Posit<16, 2, i16> {
  x + y
}

#[unsafe(no_mangle)]
pub fn add_32(x: Posit<32, 2, i32>, y: Posit<32, 2, i32>) -> Posit<32, 2, i32> {
  x + y
}

#[unsafe(no_mangle)]
pub fn add_64(x: Posit<64, 2, i64>, y: Posit<64, 2, i64>) -> Posit<64, 2, i64> {
  x + y
}

#[unsafe(no_mangle)]
pub fn sub_8(x: Posit<8, 2, i8>, y: Posit<8, 2, i8>) -> Posit<8, 2, i8> {
  x - y
}

#[unsafe(no_mangle)]
pub fn sub_16(x: Posit<16, 2, i16>, y: Posit<16, 2, i16>) -> Posit<16, 2, i16> {
  x - y
}

#[unsafe(no_mangle)]
pub fn sub_32(x: Posit<32, 2, i32>, y: Posit<32, 2, i32>) -> Posit<32, 2, i32> {
  x - y
}

#[unsafe(no_mangle)]
pub fn sub_64(x: Posit<64, 2, i64>, y: Posit<64, 2, i64>) -> Posit<64, 2, i64> {
  x - y
}

//

#[unsafe(no_mangle)]
pub fn mul_kernel_8(x: Decoded<8, 2, 8, i8>, y: Decoded<8, 2, 8, i8>) -> (Decoded<8, 2, 8, i8>, i8) {
  unsafe { Posit::<8, 2, i8>::mul_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn mul_kernel_16(x: Decoded<16, 2, 16, i16>, y: Decoded<16, 2, 16, i16>) -> (Decoded<16, 2, 16, i16>, i16) {
  unsafe { Posit::<16, 2, i16>::mul_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn mul_kernel_32(x: Decoded<32, 2, 32, i32>, y: Decoded<32, 2, 32, i32>) -> (Decoded<32, 2, 32, i32>, i32) {
  unsafe { Posit::<32, 2, i32>::mul_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn mul_kernel_64(x: Decoded<64, 2, 64, i64>, y: Decoded<64, 2, 64, i64>) -> (Decoded<64, 2, 64, i64>, i64) {
  unsafe { Posit::<64, 2, i64>::mul_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn mul_8(x: Posit<8, 2, i8>, y: Posit<8, 2, i8>) -> Posit<8, 2, i8> {
  x * y
}

#[unsafe(no_mangle)]
pub fn mul_16(x: Posit<16, 2, i16>, y: Posit<16, 2, i16>) -> Posit<16, 2, i16> {
  x * y
}

#[unsafe(no_mangle)]
pub fn mul_32(x: Posit<32, 2, i32>, y: Posit<32, 2, i32>) -> Posit<32, 2, i32> {
  x * y
}

#[unsafe(no_mangle)]
pub fn mul_64(x: Posit<64, 2, i64>, y: Posit<64, 2, i64>) -> Posit<64, 2, i64> {
  x * y
}

//

#[unsafe(no_mangle)]
pub fn div_kernel_8(x: Decoded<8, 2, 8, i8>, y: Decoded<8, 2, 8, i8>) -> (Decoded<8, 2, 8, i8>, i8) {
  unsafe { Posit::<8, 2, i8>::div_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn div_kernel_16(x: Decoded<16, 2, 16, i16>, y: Decoded<16, 2, 16, i16>) -> (Decoded<16, 2, 16, i16>, i16) {
  unsafe { Posit::<16, 2, i16>::div_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn div_kernel_32(x: Decoded<32, 2, 32, i32>, y: Decoded<32, 2, 32, i32>) -> (Decoded<32, 2, 32, i32>, i32) {
  unsafe { Posit::<32, 2, i32>::div_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn div_kernel_64(x: Decoded<64, 2, 64, i64>, y: Decoded<64, 2, 64, i64>) -> (Decoded<64, 2, 64, i64>, i64) {
  unsafe { Posit::<64, 2, i64>::div_kernel(x, y) }
}

#[unsafe(no_mangle)]
pub fn div_8(x: Posit<8, 2, i8>, y: Posit<8, 2, i8>) -> Posit<8, 2, i8> {
  x / y
}

#[unsafe(no_mangle)]
pub fn div_16(x: Posit<16, 2, i16>, y: Posit<16, 2, i16>) -> Posit<16, 2, i16> {
  x / y
}

#[unsafe(no_mangle)]
pub fn div_32(x: Posit<32, 2, i32>, y: Posit<32, 2, i32>) -> Posit<32, 2, i32> {
  x / y
}

#[unsafe(no_mangle)]
pub fn div_64(x: Posit<64, 2, i64>, y: Posit<64, 2, i64>) -> Posit<64, 2, i64> {
  x / y
}

//

#[unsafe(no_mangle)]
pub fn sqrt_kernel_8(x: Decoded<8, 2, 8, i8>) -> (Decoded<8, 2, 8, i8>, i8) {
  unsafe { Posit::<8, 2, i8>::sqrt_kernel(x) }
}

#[unsafe(no_mangle)]
pub fn sqrt_kernel_16(x: Decoded<16, 2, 16, i16>) -> (Decoded<16, 2, 16, i16>, i16) {
  unsafe { Posit::<16, 2, i16>::sqrt_kernel(x) }
}

#[unsafe(no_mangle)]
pub fn sqrt_kernel_32(x: Decoded<32, 2, 32, i32>) -> (Decoded<32, 2, 32, i32>, i32) {
  unsafe { Posit::<32, 2, i32>::sqrt_kernel(x) }
}

#[unsafe(no_mangle)]
pub fn sqrt_kernel_64(x: Decoded<64, 2, 64, i64>) -> (Decoded<64, 2, 64, i64>, i64) {
  unsafe { Posit::<64, 2, i64>::sqrt_kernel(x) }
}

#[unsafe(no_mangle)]
pub fn sqrt_8(x: Posit<8, 2, i8>) -> Posit<8, 2, i8> {
  x.sqrt()
}

#[unsafe(no_mangle)]
pub fn sqrt_16(x: Posit<16, 2, i16>) -> Posit<16, 2, i16> {
  x.sqrt()
}

#[unsafe(no_mangle)]
pub fn sqrt_32(x: Posit<32, 2, i32>) -> Posit<32, 2, i32> {
  x.sqrt()
}

#[unsafe(no_mangle)]
pub fn sqrt_64(x: Posit<64, 2, i64>) -> Posit<64, 2, i64> {
  x.sqrt()
}

//

#[unsafe(no_mangle)]
pub fn nearest_int_8(x: crate::p8) -> crate::p8 {
  x.nearest_int()
}

#[unsafe(no_mangle)]
pub fn nearest_int_16(x: crate::p16) -> crate::p16 {
  x.nearest_int()
}

#[unsafe(no_mangle)]
pub fn nearest_int_32(x: crate::p32) -> crate::p32 {
  x.nearest_int()
}

#[unsafe(no_mangle)]
pub fn nearest_int_64(x: crate::p64) -> crate::p64 {
  x.nearest_int()
}

#[unsafe(no_mangle)]
pub fn floor_8(x: crate::p8) -> crate::p8 {
  x.floor()
}

#[unsafe(no_mangle)]
pub fn floor_16(x: crate::p16) -> crate::p16 {
  x.floor()
}

#[unsafe(no_mangle)]
pub fn floor_32(x: crate::p32) -> crate::p32 {
  x.floor()
}

#[unsafe(no_mangle)]
pub fn floor_64(x: crate::p64) -> crate::p64 {
  x.floor()
}

#[unsafe(no_mangle)]
pub fn ceil_8(x: crate::p8) -> crate::p8 {
  x.ceil()
}

#[unsafe(no_mangle)]
pub fn ceil_16(x: crate::p16) -> crate::p16 {
  x.ceil()
}

#[unsafe(no_mangle)]
pub fn ceil_32(x: crate::p32) -> crate::p32 {
  x.ceil()
}

#[unsafe(no_mangle)]
pub fn ceil_64(x: crate::p64) -> crate::p64 {
  x.ceil()
}

//

#[unsafe(no_mangle)]
pub fn quire_add_8(quire: &mut crate::q8, posit: crate::p8) {
  *quire += posit
}

#[unsafe(no_mangle)]
pub fn quire_add_16(quire: &mut crate::q16, posit: crate::p16) {
  *quire += posit
}

#[unsafe(no_mangle)]
pub fn quire_add_32(quire: &mut crate::q32, posit: crate::p32) {
  *quire += posit
}

#[unsafe(no_mangle)]
pub fn quire_add_64(quire: &mut crate::q64, posit: crate::p64) {
  *quire += posit
}

#[unsafe(no_mangle)]
pub fn quire_add_kernel_8(quire: &mut crate::q8, decoded: Decoded<8, 2, 8, i8>) {
  unsafe { quire.add_posit_kernel(decoded) }
}

#[unsafe(no_mangle)]
pub fn quire_add_kernel_16(quire: &mut crate::q16, decoded: Decoded<16, 2, 16, i8>) {
  unsafe { quire.add_posit_kernel(decoded) }
}

#[unsafe(no_mangle)]
pub fn quire_add_kernel_32(quire: &mut crate::q32, decoded: Decoded<32, 2, 32, i8>) {
  unsafe { quire.add_posit_kernel(decoded) }
}

#[unsafe(no_mangle)]
pub fn quire_add_kernel_64(quire: &mut crate::q64, decoded: Decoded<64, 2, 64, i8>) {
  unsafe { quire.add_posit_kernel(decoded) }
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_8(quire: &mut crate::q8, a: crate::p8, b: crate::p8) {
  quire.add_prod(a, b)
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_16(quire: &mut crate::q16, a: crate::p16, b: crate::p16) {
  quire.add_prod(a, b)
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_32(quire: &mut crate::q32, a: crate::p32, b: crate::p32) {
  quire.add_prod(a, b)
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_64(quire: &mut crate::q64, a: crate::p64, b: crate::p64) {
  quire.add_prod(a, b)
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_kernel_8(quire: &mut crate::q8, a: Decoded<8, 2, 8, i8>, b: Decoded<8, 2, 8, i8>) {
  unsafe { quire.add_posit_prod_kernel(a, b) }
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_kernel_16(quire: &mut crate::q16, a: Decoded<16, 2, 16, i16>, b: Decoded<16, 2, 16, i16>) {
  unsafe { quire.add_posit_prod_kernel(a, b) }
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_kernel_32(quire: &mut crate::q32, a: Decoded<32, 2, 32, i32>, b: Decoded<32, 2, 32, i32>) {
  unsafe { quire.add_posit_prod_kernel(a, b) }
}

#[unsafe(no_mangle)]
pub fn quire_add_prod_kernel_64(quire: &mut crate::q64, a: Decoded<64, 2, 64, i64>, b: Decoded<64, 2, 64, i64>) {
  unsafe { quire.add_posit_prod_kernel(a, b) }
}

#[unsafe(no_mangle)]
pub fn quire_neg_8(quire: &mut crate::q8) {
  let q = core::mem::replace(quire, crate::q8::ZERO);
  core::mem::replace(quire, -q);
}

#[unsafe(no_mangle)]
pub fn quire_neg_16(quire: &mut crate::q16) {
  let q = core::mem::replace(quire, crate::q16::ZERO);
  core::mem::replace(quire, -q);
}

#[unsafe(no_mangle)]
pub fn quire_neg_32(quire: &mut crate::q32) {
  let q = core::mem::replace(quire, crate::q32::ZERO);
  core::mem::replace(quire, -q);
}

#[unsafe(no_mangle)]
pub fn quire_neg_64(quire: &mut crate::q64) {
  let q = core::mem::replace(quire, crate::q64::ZERO);
  core::mem::replace(quire, -q);
}

//

#[unsafe(no_mangle)]
pub fn round_i32_to_p8(num: i32) -> crate::p8 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_i32_to_p32(num: i32) -> crate::p32 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_i32_to_p64(num: i32) -> crate::p64 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_p32_to_i8(num: crate::p32) -> i8 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_p32_to_i32(num: crate::p32) -> i32 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_p32_to_i64(num: crate::p32) -> i64 {
  num.round_into()
}

//

#[unsafe(no_mangle)]
pub fn round_f32_to_p8(num: f32) -> crate::p8 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_f32_to_p32(num: f32) -> crate::p32 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_f32_to_p64(num: f32) -> crate::p64 {
  num.round_into()
}

//

#[unsafe(no_mangle)]
pub fn round_p8_to_f32(num: crate::p8) -> f32 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_p32_to_f32(num: crate::p32) -> f32 {
  num.round_into()
}

#[unsafe(no_mangle)]
pub fn round_p64_to_f32(num: crate::p64) -> f32 {
  num.round_into()
}

//

#[unsafe(no_mangle)]
pub fn round_p32_to_p16(num: crate::p32) -> crate::p16 {
  num.convert()
}

#[unsafe(no_mangle)]
pub fn round_p16_to_p32(num: crate::p16) -> crate::p32 {
  num.convert()
}