thermite 0.2.1

High-performance, generic, ISA-portable SIMD library with a policy-configurable transcendental math library
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
// Derived from:
//
// libdivide.h - Optimized integer division
// https://libdivide.com
//
// Copyright (C) 2010 - 2019 ridiculous_fish, <libdivide@ridiculousfish.com>
// Copyright (C) 2016 - 2019 Kim Walisch, <kim.walisch@gmail.com>

#![allow(unused)]

pub mod vector;

use core::ops::Deref;

macro_rules! decl_div_half {
    ($($t:ty => $dt:ty),*) => {
        paste::paste! {$(
            #[inline(always)]
            const fn [<div_ $dt _ $t _to_ $t>](u1: $t, u0: $t, v: $t) -> ($t, $t) {
                let v = v as $dt;
                let n = ((u1 as $dt) << <$t>::BITS) | (u0 as $dt);
                let res = (n / v) as $t; // truncate
                let rem = n.wrapping_sub((res as $dt).wrapping_mul(v));
                (res, rem as $t)
            }
        )*}
    };
}

decl_div_half!(u64 => u128, u32 => u64, u16 => u32, u8 => u16);

/// Trait for types that can be used as denominators in dividers.
pub trait Denominator: Sized {
    /// Create a divider for this denominator.
    fn to_divider(self) -> Divider<Self>;

    /// Create a branchfree divider for this denominator.
    fn to_branchfree_divider(self) -> BranchfreeDivider<Self>;

    /// Try to create a branchfree divider for this denominator.
    ///
    /// Branchfree dividers may not support all divisors, see the documentation of
    /// [`BranchfreeDivider`] for details.
    fn try_to_branchfree_divider(self) -> Result<BranchfreeDivider<Self>, UnsupportedDivisor>;

    /// Shift mask for this type.
    const SHIFT_MASK: u8;
}

/// Divider recommended for constant divisors.
///
/// When using constant divisors, divisions using this can remove extra branches
/// and generate ideal integer division code.
///
/// However, when used with dynamic input, the extra branches can be expensive,
/// therefore it is recommended to use the branchfree alternative for dynamic divisors.
#[repr(C, packed)]
pub struct Divider<T> {
    multiplier: T,
    shift: u8,
}

/// Divider without branching, useful for dynamic divisors where branches can be expensive,
/// at the cost of some extra work compared to the branching [`Divider`].
///
/// However, when used with constant input, this may perform extra unnecessary work that could
/// be removed in the branching [`Divider`].
///
/// Furthermore, the unsigned version of this divider does not support a divisor of 1,
/// due to the way the algorithm works.
#[repr(transparent)]
#[derive(Copy, PartialEq)]
pub struct BranchfreeDivider<T>(Divider<T>);

/// Error return by `TryFrom` implementations when the divisor is unsupported
/// by the branchfree divider. See the documentation of [`BranchfreeDivider`] for details.
#[derive(Debug, Clone, Copy)]
pub struct UnsupportedDivisor;

impl core::fmt::Display for UnsupportedDivisor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "unsupported divisor")
    }
}

impl core::error::Error for UnsupportedDivisor {}

impl<T: Copy> Clone for BranchfreeDivider<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Deref for BranchfreeDivider<T> {
    type Target = Divider<T>;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: Copy> Clone for Divider<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: Copy> Copy for Divider<T> {}

impl<T: PartialEq> PartialEq for Divider<T> {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        self.multiplier() == other.multiplier() && self.shift == other.shift
    }
}

impl<T> Divider<T> {
    #[inline(always)]
    pub const fn multiplier(&self) -> T {
        // unaligned access needs `&raw const` and `read_unaligned` to be safe
        unsafe { (&raw const self.multiplier).read_unaligned() }
    }

    #[inline(always)]
    pub const fn shift(&self) -> u8 {
        // shift has an alignment of 1 byte anyway, so it's fine to read normally
        self.shift
    }
}

impl<T: Copy + core::fmt::Debug> core::fmt::Debug for Divider<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Divider")
            .field("multiplier", &self.multiplier())
            .field("shift", &self.shift())
            .finish()
    }
}

impl<T: Copy + core::fmt::Debug> core::fmt::Debug for BranchfreeDivider<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_tuple("BranchfreeDivider").field(&self.0).finish()
    }
}

pub(crate) const ADD_MARKER: u8 = 0x40;
pub(crate) const NEG_DIVISOR: u8 = 0x80;

macro_rules! impl_shift_mask {
    ($($ty:ty => $ut:ty),*) => {$(
        impl Divider<$ty> {
            const BITS: u32 = <$ty>::BITS as u32;

            /// !log2(N::BITS)
            pub(crate) const SHIFT_MASK: u8 = !(<$ut>::MAX << <$ty>::BITS.trailing_zeros()) as u8;
        }
    )*};
}

impl_shift_mask! {
    u8 => u8,
    u16 => u16,
    u32 => u32,
    u64 => u64,
    i8 => u8,
    i16 => u16,
    i32 => u32,
    i64 => u64
}

macro_rules! impl_unsigned_divider {
    ($($t:ty => $dt:ty),*) => {
        paste::paste! {$(
            impl BranchfreeDivider<$t> {
                /// Create a new branchfree divider for the given divisor.
                ///
                /// # Panics
                ///
                /// Panics if `d == 1`, as unsigned division by 1 is not supported in branchfree mode due
                /// to the way the algorithm works.
                #[inline(always)]
                pub const fn [<$t>](d: $t) -> Self {
                    BranchfreeDivider(Divider::[<$t _internal>](d, true))
                }

                /// Try to create a new branchfree divider for the given divisor.
                ///
                /// The unsigned branchfree divider does not support a divisor of 1, so this returns
                /// `None` in that case.
                #[inline(always)]
                pub const fn [<try_ $t>](d: $t) -> Option<Self> {
                    if d == 1 { None } else { Some(Self::[<$t>](d)) }
                }

                #[inline(always)]
                pub fn divide(self, x: $t) -> $t {
                    let q = Divider::<$t>::mullhi(x, self.multiplier());
                    let t = x.wrapping_sub(q) >> 1;
                    t.wrapping_add(q) >> self.shift()
                }
            }

            impl From<$t> for Divider<$t> {
                #[inline(always)]
                fn from(d: $t) -> Self {
                    Self::[<$t>](d)
                }
            }

            impl TryFrom<$t> for BranchfreeDivider<$t> {
                type Error = UnsupportedDivisor;

                #[inline(always)]
                fn try_from(d: $t) -> Result<Self, Self::Error> {
                    Self::[<try_ $t>](d).ok_or(UnsupportedDivisor)
                }
            }

            impl Denominator for $t {
                #[inline(always)]
                fn to_divider(self) -> Divider<Self> {
                    Divider::[<$t>](self)
                }

                #[inline(always)]
                fn to_branchfree_divider(self) -> BranchfreeDivider<Self> {
                    BranchfreeDivider::[<$t>](self)
                }

                #[inline(always)]
                fn try_to_branchfree_divider(self) -> Result<BranchfreeDivider<Self>, UnsupportedDivisor> {
                    BranchfreeDivider::[<try_ $t>](self).ok_or(UnsupportedDivisor)
                }

                const SHIFT_MASK: u8 = Divider::<$t>::SHIFT_MASK;
            }

            impl Divider<$t> {
                /// Create a new divider for the given divisor.
                #[inline(always)]
                pub const fn [<$t>](d: $t) -> Self {
                    Self::[<$t _internal>](d, false)
                }

                #[inline(always)]
                pub fn divide(self, x: $t) -> $t {
                    let multiplier = self.multiplier();
                    let shift = self.shift();

                    if multiplier == 0 {
                        return x >> shift;
                    }

                    let mut q = Self::mullhi(x, multiplier);

                    if (shift & ADD_MARKER) != 0 {
                        q = (x.wrapping_sub(q) >> 1).wrapping_add(q);
                    }

                    q >> (shift & Divider::<$t>::SHIFT_MASK)
                }

                #[inline(always)]
                const fn [<$t _internal>](d: $t, bf: bool) -> Self {
                    if d == 0 {
                        return Divider { multiplier: 0, shift: 0 };
                    }

                    if bf && d == 1 {
                        panic!("branchfree divider must be != 1");
                    }

                    let floor_log_2_d = Self::BITS - 1 - d.leading_zeros();

                    if d.is_power_of_two() {
                        return Divider {
                            multiplier: 0,
                            // We need to subtract 1 from the shift value in case of an unsigned
                            // branchfree divider because there is a hardcoded right shift by 1
                            // in its division algorithm.
                            shift: (floor_log_2_d - bf as u32) as u8,
                        };
                    }

                    let k = 1 << floor_log_2_d;
                    let (mut proposed_m, rem) = [<div_ $dt _ $t _to_ $t>](k, 0, d);

                    let e = d.wrapping_sub(rem);

                    let mut shift;

                    if !bf && e < k {
                        shift = floor_log_2_d as u8;
                    } else {
                        proposed_m = proposed_m.wrapping_add(proposed_m);
                        let rem2 = rem.wrapping_add(rem);

                        if rem2 >= d || rem2 < rem {
                            proposed_m = proposed_m.wrapping_add(1);
                        }

                        shift = floor_log_2_d as u8;

                        if !bf {
                            // instead of masking out the ADD_MARKER bit, we just don't set it
                            shift |= ADD_MARKER;
                        }
                    }

                    Divider { multiplier: proposed_m.wrapping_add(1), shift }
                }
            }
        )*}
    }
}

macro_rules! impl_signed_divider {
    ($($t:ty => $ut:ty => $udt:ty),*) => {
        paste::paste!{$(
            impl BranchfreeDivider<$t> {
                /// Create a new branchfree divider for the given divisor.
                ///
                /// Unlike the unsigned version, this does support a divisor of 1.
                #[inline(always)]
                pub const fn [<$t>](d: $t) -> Self {
                    BranchfreeDivider(Divider::[<$t _internal>](d, true))
                }

                #[inline(always)]
                pub fn divide(self, x: $t) -> $t {
                    let multiplier = self.multiplier();
                    let shift = self.shift();

                    let masked_shift = shift & Divider::<$t>::SHIFT_MASK;

                    let mut q = Divider::<$t>::mullhi(x, multiplier).wrapping_add(x);

                    let is_power_of_2: $ut = (multiplier == 0) as $ut;
                    let q_sign = q >> (Divider::<$t>::BITS - 1); // extends sign to fill bits

                    q = q.wrapping_add( q_sign & ((1 as $ut) << masked_shift).wrapping_sub(is_power_of_2) as $t );

                    let sign = ((shift as i8) >> 7) as $t; // take last bit as sign, to convert this to 0 or -1

                    ((q >> masked_shift) ^ sign) - sign
                }
            }

            impl From<$t> for BranchfreeDivider<$t> {
                #[inline(always)]
                fn from(d: $t) -> Self {
                    BranchfreeDivider::[<$t>](d)
                }
            }

            impl From<$t> for Divider<$t> {
                #[inline(always)]
                fn from(d: $t) -> Self {
                    Self::[<$t>](d)
                }
            }

            impl Denominator for $t {
                #[inline(always)]
                fn to_divider(self) -> Divider<Self> {
                    Divider::[<$t>](self)
                }

                #[inline(always)]
                fn to_branchfree_divider(self) -> BranchfreeDivider<Self> {
                    BranchfreeDivider::[<$t>](self)
                }

                #[inline(always)]
                fn try_to_branchfree_divider(self) -> Result<BranchfreeDivider<Self>, UnsupportedDivisor> {
                    Ok(BranchfreeDivider::[<$t>](self))
                }

                const SHIFT_MASK: u8 = Divider::<$t>::SHIFT_MASK;
            }

            impl Divider<$t> {
                /// Create a new divider for the given divisor.
                #[inline(always)]
                pub const fn [<$t>](d: $t) -> Self {
                    Self::[<$t _internal>](d, false)
                }

                pub fn divide(self, x: $t) -> $t {
                    let multiplier = self.multiplier();
                    let shift = self.shift();

                    let masked_shift = shift & Divider::<$t>::SHIFT_MASK;

                    // take last bit as sign, to convert this to 0 or -1
                    let sign = ((shift as i8) >> 7) as $t;

                    if multiplier == 0 {
                        let mask = ((1 as $ut) << masked_shift).wrapping_sub(1) as $t;
                        let uq = x.wrapping_add((x >> (Self::BITS - 1)) & mask);

                        return ((uq as $t >> masked_shift) ^ sign) - sign;
                    }

                    let mut uq = Self::mullhi(x, multiplier) as $ut;

                    if (shift & ADD_MARKER) != 0 {
                        uq = uq.wrapping_add(x as $ut ^ sign as $ut).wrapping_sub(sign as $ut);
                    }

                    let q = uq as $t >> masked_shift;

                    q + (q < 0) as $t
                }

                #[inline(always)]
                const fn [<$t _internal>](d: $t, bf: bool) -> Self {
                    if d == 0 {
                        return Divider { multiplier: 0, shift: 0 };
                    }

                    let abs_d = d.unsigned_abs();

                    let floor_log_2_d = Divider::<$ut>::BITS - 1 - abs_d.leading_zeros();

                    if abs_d.is_power_of_two() {
                        return Divider {
                            multiplier: 0,
                            shift: floor_log_2_d as u8 | if d < 0 { NEG_DIVISOR } else { 0 },
                        };
                    }

                    let (mut proposed_m, rem) = [<div_ $udt _ $ut _to_ $ut>](1 << (floor_log_2_d - 1), 0, abs_d);

                    let e = abs_d.wrapping_sub(rem);

                    let mut shift;

                    if !bf && e < (1 << floor_log_2_d) {
                        shift = (floor_log_2_d - 1) as u8;
                    } else {
                        proposed_m = proposed_m.wrapping_add(proposed_m);
                        let rem2 = rem.wrapping_add(rem);

                        if rem2 >= abs_d || rem2 < rem {
                            proposed_m = proposed_m.wrapping_add(1);
                        }

                        shift = floor_log_2_d as u8 | ADD_MARKER;
                    }

                    proposed_m = proposed_m.wrapping_add(1);

                    let mut multiplier = proposed_m as $t;

                    if d < 0 {
                        shift |= NEG_DIVISOR;

                        if !bf {
                            multiplier = -multiplier;
                        }
                    }

                    Divider { multiplier, shift }
                }
            }
        )*}
    }
}

macro_rules! impl_divider {
    ($($t:ty => $dt:ty),*) => {paste::paste! {$(
        impl Divider<$t> {
            #[inline(always)]
            pub(crate) const fn mullhi(x: $t, y: $t) -> $t {
                (((x as $dt) * (y as $dt)) >> <$t>::BITS) as $t
            }

            #[inline(always)]
            pub(crate) const fn new(m: $t, s: u8) -> Self {
                Divider { multiplier: m, shift: s }
            }
        }

        impl BranchfreeDivider<$t> {
            #[inline(always)]
            pub(crate) const fn new(m: $t, s: u8) -> Self {
                BranchfreeDivider(Divider { multiplier: m, shift: s })
            }
        }
    )*}};
}

impl_unsigned_divider! {
    u8 => u16,
    u16 => u32,
    u32 => u64,
    u64 => u128
}

impl_signed_divider! {
    i8 => u8 => u16,
    i16 => u16 => u32,
    i32 => u32 => u64,
    i64 => u64 => u128
}

impl_divider! {
    u8 => u16,
    u16 => u32,
    u32 => u64,
    u64 => u128,
    i8 => i16,
    i16 => i32,
    i32 => i64,
    i64 => i128
}