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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use super::{P8E0, Q8E0};
use crate::WithSign;
use core::ops;

impl ops::Neg for P8E0 {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        Self::new(self.0.wrapping_neg())
    }
}

impl ops::AddAssign for P8E0 {
    #[inline]
    fn add_assign(&mut self, other: Self) {
        *self = *self + other
    }
}

impl ops::SubAssign for P8E0 {
    #[inline]
    fn sub_assign(&mut self, other: Self) {
        *self = *self - other
    }
}

impl ops::MulAssign for P8E0 {
    #[inline]
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other
    }
}

impl ops::DivAssign for P8E0 {
    #[inline]
    fn div_assign(&mut self, other: Self) {
        *self = *self / other
    }
}

impl ops::RemAssign for P8E0 {
    #[inline]
    fn rem_assign(&mut self, other: Self) {
        *self = *self % other
    }
}

impl ops::Add for P8E0 {
    type Output = Self;
    #[inline]
    fn add(self, other: Self) -> Self {
        let ui_a = self.to_bits();
        let ui_b = other.to_bits();

        //Zero or infinity
        if (ui_a == 0) || (ui_b == 0) {
            // Not required but put here for speed
            Self::from_bits(ui_a | ui_b)
        } else if (ui_a == 0x80) || (ui_b == 0x80) {
            Self::NAR
        } else {
            //different signs
            if Self::sign_ui(ui_a ^ ui_b) {
                sub_mags_p8(ui_a, ui_b)
            } else {
                add_mags_p8(ui_a, ui_b)
            }
        }
    }
}

impl ops::Sub for P8E0 {
    type Output = Self;
    #[inline]
    fn sub(self, other: Self) -> Self {
        let ui_a = self.to_bits();
        let ui_b = other.to_bits();

        //infinity
        if (ui_a == 0x80) || (ui_b == 0x80) {
            Self::NAR
        }
        //Zero
        else if (ui_a == 0) || (ui_b == 0) {
            Self::from_bits(ui_a | ui_b.wrapping_neg())
        } else {
            //different signs
            if Self::sign_ui(ui_a ^ ui_b) {
                add_mags_p8(ui_a, ui_b.wrapping_neg())
            } else {
                sub_mags_p8(ui_a, ui_b.wrapping_neg())
            }
        }
    }
}

impl ops::Div for P8E0 {
    type Output = Self;
    #[inline]
    fn div(self, other: Self) -> Self {
        let mut ui_a = self.to_bits();
        let mut ui_b = other.to_bits();

        //Zero or infinity
        if (ui_a == 0x80) || (ui_b == 0x80) || (ui_b == 0) {
            return Self::NAR;
        } else if ui_a == 0 {
            return Self::ZERO;
        }

        let sign_a = Self::sign_ui(ui_a);
        let sign_b = Self::sign_ui(ui_b);
        let sign_z = sign_a ^ sign_b;
        if sign_a {
            ui_a = ui_a.wrapping_neg();
        }
        if sign_b {
            ui_b = ui_b.wrapping_neg();
        }

        let (mut k_a, frac_a) = Self::separate_bits(ui_a);
        let (k_b, frac_b) = Self::separate_bits(ui_b);
        k_a -= k_b;

        let frac16_a = (frac_a as u16) << 7; //hidden bit 2nd bit

        let (quot, rem) = crate::div(frac16_a as i32, frac_b as i32);
        let mut frac16_z = quot as u16;

        if frac16_z != 0 {
            let rcarry = (frac16_z >> 7) != 0; // this is the hidden bit (7th bit) , extreme right bit is bit 0
            if !rcarry {
                k_a -= 1;
                frac16_z <<= 1;
            }
        }

        let (regime, reg_sa, reg_a) = Self::calculate_regime(k_a);

        let u_z = if reg_a > 6 {
            //max or min pos. exp and frac does not matter.
            if reg_sa {
                0x7F
            } else {
                0x1
            }
        } else {
            //remove carry and rcarry bits and shift to correct position
            frac16_z &= 0x7F;
            let frac_a = (frac16_z >> (reg_a + 1)) as u8;

            let bit_n_plus_one = (0x1 & (frac16_z >> reg_a)) != 0;
            let mut u_z = Self::pack_to_ui(regime, frac_a);

            if bit_n_plus_one {
                let bits_more = if rem != 0 {
                    true
                } else {
                    (((1 << reg_a) - 1) & frac16_z) != 0
                };
                //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
                u_z += (u_z & 1) | (bits_more as u8);
            }
            u_z
        };

        Self::from_bits(u_z.with_sign(sign_z))
    }
}

impl ops::Mul for P8E0 {
    type Output = Self;
    #[inline]
    fn mul(self, other: Self) -> Self {
        let mut ui_a = self.to_bits();
        let mut ui_b = other.to_bits();

        //NaR or Zero
        if (ui_a == 0x80) || (ui_b == 0x80) {
            return Self::NAR;
        } else if (ui_a == 0) || (ui_b == 0) {
            return Self::ZERO;
        }

        let sign_a = Self::sign_ui(ui_a);
        let sign_b = Self::sign_ui(ui_b);
        let sign_z = sign_a ^ sign_b;

        if sign_a {
            ui_a = ui_a.wrapping_neg();
        }
        if sign_b {
            ui_b = ui_b.wrapping_neg();
        }

        let (mut k_a, frac_a) = Self::separate_bits(ui_a);
        let (k_b, frac_b) = Self::separate_bits(ui_b);
        k_a += k_b;

        let mut frac16_z = (frac_a as u16) * (frac_b as u16);

        let rcarry = (frac16_z & 0x_8000) != 0; //1st bit of frac32Z
        if rcarry {
            k_a += 1;
            frac16_z >>= 1;
        }

        let (regime, reg_sa, reg_a) = Self::calculate_regime(k_a);

        let u_z = if reg_a > 6 {
            //max or min pos. exp and frac does not matter.
            if reg_sa {
                0x7F
            } else {
                0x1
            }
        } else {
            //remove carry and rcarry bits and shift to correct position
            frac16_z = (frac16_z & 0x3FFF) >> reg_a;
            let frac_a = (frac16_z >> 8) as u8;
            let bit_n_plus_one = (frac16_z & 0x80) != 0;
            let mut u_z = Self::pack_to_ui(regime, frac_a);

            //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
            if bit_n_plus_one {
                let bits_more = (frac16_z & 0x7F) != 0;
                u_z += (u_z & 1) | (bits_more as u8);
            }
            u_z
        };

        Self::from_bits(u_z.with_sign(sign_z))
    }
}

#[inline]
fn add_mags_p8(mut ui_a: u8, mut ui_b: u8) -> P8E0 {
    let sign = P8E0::sign_ui(ui_a);
    if sign {
        ui_a = ui_a.wrapping_neg();
        ui_b = ui_b.wrapping_neg();
    }

    if (ui_a as i8) < (ui_b as i8) {
        ui_a ^= ui_b;
        ui_b ^= ui_a;
        ui_a ^= ui_b;
    }

    let (mut k_a, frac_a) = P8E0::separate_bits(ui_a);
    let mut frac16_a = (frac_a as u16) << 7;

    let (k_b, frac_b) = P8E0::separate_bits(ui_b);
    let shift_right = (k_a as i16) - (k_b as i16);

    //Manage CLANG (LLVM) compiler when shifting right more than number of bits
    let frac16_b = if shift_right > 7 {
        0
    } else {
        (frac_b as u16) << (7 - shift_right)
    };

    frac16_a += frac16_b;

    let rcarry = (0x8000 & frac16_a) != 0; //first left bit
    if rcarry {
        k_a += 1;
        frac16_a >>= 1;
    }

    let (regime, reg_sa, reg_a) = P8E0::calculate_regime(k_a);

    let u_z = if reg_a > 6 {
        //max or min pos. exp and frac does not matter.
        if reg_sa {
            0x7F
        } else {
            0x1
        }
    } else {
        frac16_a = (frac16_a & 0x3FFF) >> reg_a;
        let frac_a = (frac16_a >> 8) as u8;
        let bit_n_plus_one = (0x80 & frac16_a) != 0;
        let mut u_z = P8E0::pack_to_ui(regime, frac_a);

        //n+1 frac bit is 1. Need to check if another bit is 1 too if not round to even
        if bit_n_plus_one {
            let bits_more = (0x7F & frac16_a) != 0;
            u_z += (u_z & 1) | (bits_more as u8);
        }
        u_z
    };
    P8E0::from_bits(u_z.with_sign(sign))
}

#[inline]
fn sub_mags_p8(mut ui_a: u8, mut ui_b: u8) -> P8E0 {
    //Both ui_a and ui_b are actually the same signs if ui_b inherits sign of sub
    //Make both positive
    let mut sign = P8E0::sign_ui(ui_a);
    if sign {
        ui_a = ui_a.wrapping_neg();
    } else {
        ui_b = ui_b.wrapping_neg();
    }
    if ui_a == ui_b {
        //essential, if not need special handling
        return P8E0::ZERO;
    }
    if ui_a < ui_b {
        ui_a ^= ui_b;
        ui_b ^= ui_a;
        ui_a ^= ui_b;
        sign = !sign; //A becomes B
    }

    let (mut k_a, frac_a) = P8E0::separate_bits(ui_a);
    let mut frac16_a = (frac_a as u16) << 7;

    let (k_b, frac_b) = P8E0::separate_bits(ui_b);
    let shift_right = (k_a as i16) - (k_b as i16);

    let mut frac16_b = (frac_b as u16) << 7;

    if shift_right >= 14 {
        return P8E0::from_bits(ui_a.with_sign(sign));
    } else {
        frac16_b >>= shift_right;
    }
    frac16_a -= frac16_b;

    while (frac16_a >> 14) == 0 {
        k_a -= 1;
        frac16_a <<= 1;
    }
    let ecarry = ((0x4000 & frac16_a) >> 14) != 0;
    if !ecarry {
        k_a -= 1;
        frac16_a <<= 1;
    }

    let (regime, reg_sa, reg_a) = P8E0::calculate_regime(k_a);

    let u_z = if reg_a > 6 {
        //max or min pos. exp and frac does not matter.
        if reg_sa {
            0x7F
        } else {
            0x1
        }
    } else {
        frac16_a = (frac16_a & 0x3FFF) >> reg_a;
        let frac_a = (frac16_a >> 8) as u8;
        let bit_n_plus_one = (0x80 & frac16_a) != 0;
        let mut u_z = P8E0::pack_to_ui(regime, frac_a);

        if bit_n_plus_one {
            let bits_more = (0x7F & frac16_a) != 0;
            u_z += (u_z & 1) | (bits_more as u8);
        }
        u_z
    };
    P8E0::from_bits(u_z.with_sign(sign))
}

impl ops::Rem for P8E0 {
    type Output = Self;
    fn rem(self, _other: Self) -> Self {
        unimplemented!()
    }
}

impl ops::AddAssign<(P8E0, P8E0)> for Q8E0 {
    #[inline]
    fn add_assign(&mut self, rhs: (P8E0, P8E0)) {
        q8_fdp_add(self, rhs.0, rhs.1);
    }
}

impl ops::SubAssign<(P8E0, P8E0)> for Q8E0 {
    #[inline]
    fn sub_assign(&mut self, rhs: (P8E0, P8E0)) {
        q8_fdp_sub(self, rhs.0, rhs.1);
    }
}

pub(super) fn q8_fdp_add(q: &mut Q8E0, p_a: P8E0, p_b: P8E0) {
    let uq_z1 = q.to_bits();

    let mut ui_a = p_a.to_bits();
    let mut ui_b = p_b.to_bits();

    if q.is_nar() || p_a.is_nar() || p_b.is_nar() {
        *q = Q8E0::NAR;
        return;
    } else if (ui_a == 0) || (ui_b == 0) {
        return;
    }

    //max pos (sign plus and minus)
    let sign_a = P8E0::sign_ui(ui_a);
    let sign_b = P8E0::sign_ui(ui_b);
    let sign_z2 = sign_a ^ sign_b;

    if sign_a {
        ui_a = ui_a.wrapping_neg();
    }
    if sign_b {
        ui_b = ui_b.wrapping_neg();
    }

    let (mut k_a, frac_a) = P8E0::separate_bits(ui_a);

    let (k_b, frac_b) = P8E0::separate_bits(ui_b);
    k_a += k_b;

    let mut frac32_z = ((frac_a as u32) * (frac_b as u32)) << 16;

    let rcarry = (frac32_z & 0x8000_0000) != 0; //1st bit (position 2) of frac32_z, hidden bit is 4th bit (position 3)
    if rcarry {
        k_a += 1;
        frac32_z >>= 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-k_a, shift = firstPos -1 (because frac32_z start from 2nd bit)
    //int firstPos = 19 - k_a;
    let shift_right = 18 - k_a;

    let mut uq_z2 = frac32_z >> shift_right;

    if sign_z2 {
        uq_z2 = uq_z2.wrapping_neg();
    }

    //Addition
    let uq_z = uq_z2.wrapping_add(uq_z1);

    //Exception handling
    let q_z = Q8E0::from_bits(uq_z);
    *q = if q_z.is_nar() { Q8E0::ZERO } else { q_z }
}

//q - (p_a*p_b)

pub(super) fn q8_fdp_sub(q: &mut Q8E0, p_a: P8E0, p_b: P8E0) {
    let uq_z1 = q.to_bits();

    let mut ui_a = p_a.to_bits();
    let mut ui_b = p_b.to_bits();

    if q.is_nar() || p_a.is_nar() || p_b.is_nar() {
        *q = Q8E0::NAR;
        return;
    } else if (ui_a == 0) || (ui_b == 0) {
        return;
    }

    //max pos (sign plus and minus)
    let sign_a = P8E0::sign_ui(ui_a);
    let sign_b = P8E0::sign_ui(ui_b);
    let sign_z2 = sign_a ^ sign_b;

    if sign_a {
        ui_a = ui_a.wrapping_neg();
    }
    if sign_b {
        ui_b = ui_b.wrapping_neg();
    }

    let (mut k_a, frac_a) = P8E0::separate_bits(ui_a);

    let (k_b, frac_b) = P8E0::separate_bits(ui_b);
    k_a += k_b;

    let mut frac32_z = ((frac_a as u32) * (frac_b as u32)) << 16;

    let rcarry = (frac32_z & 0x8000_0000) != 0; //1st bit (position 2) of frac32_z, hidden bit is 4th bit (position 3)
    if rcarry {
        k_a += 1;
        frac32_z >>= 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-k_a, shift = firstPos -1 (because frac32_z start from 2nd bit)
    //int firstPos = 19 - k_a;
    let shift_right = 18 - k_a;

    let mut uq_z2 = frac32_z >> shift_right;

    //This is the only difference from ADD (sign_z2) and (!sign_z2)
    if !sign_z2 {
        uq_z2 = uq_z2.wrapping_neg();
    }

    //Addition
    let uq_z = uq_z2.wrapping_add(uq_z1);

    //Exception handling
    let q_z = Q8E0::from_bits(uq_z);
    *q = if q_z.is_nar() { Q8E0::ZERO } else { q_z }
}

#[test]
fn test_quire_mul_add() {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    for _ in 0..crate::NTESTS8 {
        let p_a: P8E0 = rng.gen();
        let p_b: P8E0 = rng.gen();
        let p_c: P8E0 = rng.gen();
        let f_a = f64::from(p_a);
        let f_b = f64::from(p_b);
        let f_c = f64::from(p_c);
        let mut q = Q8E0::init();
        q += (p_a, p_b);
        q += (p_c, P8E0::ONE);
        let p = q.to_posit();
        let f = f_a.mul_add(f_b, f_c);
        assert_eq!(p, P8E0::from(f));
    }
}

#[test]
fn test_quire_mul_sub() {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    for _ in 0..crate::NTESTS8 {
        let p_a: P8E0 = rng.gen();
        let p_b: P8E0 = rng.gen();
        let p_c: P8E0 = rng.gen();
        let f_a = f64::from(p_a);
        let f_b = f64::from(p_b);
        let f_c = f64::from(p_c);
        let mut q = Q8E0::init();
        q -= (p_a, p_b);
        q += (p_c, P8E0::ONE);
        let p = q.to_posit();
        let f = (-f_a).mul_add(f_b, f_c);
        assert_eq!(p, P8E0::from(f));
    }
}

#[cfg(test)]
fn test_ops(fun: fn(P8E0, P8E0, f64, f64) -> (P8E0, f64)) {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    for _ in 0..crate::NTESTS8 {
        let p_a: P8E0 = rng.gen();
        let p_b: P8E0 = rng.gen();
        let f_a = f64::from(p_a);
        let f_b = f64::from(p_b);
        let (p, f) = fun(p_a, p_b, f_a, f_b);
        assert_eq!(p, P8E0::from(f));
    }
}

#[test]
fn add() {
    test_ops(|p_a, p_b, f_a, f_b| (p_a + p_b, f_a + f_b));
}

#[test]
fn sub() {
    test_ops(|p_a, p_b, f_a, f_b| (p_a - p_b, f_a - f_b));
}

#[test]
fn mul() {
    test_ops(|p_a, p_b, f_a, f_b| (p_a * p_b, f_a * f_b));
}

#[test]
fn div() {
    test_ops(|p_a, p_b, f_a, f_b| (p_a / p_b, f_a / f_b));
}