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
#![no_std]
#![warn(missing_docs)]
#![allow(unused_imports)]
#![allow(clippy::inline_always)]

//! Lokathor's crate of core-only odds and ends.

use core::{
  marker::PhantomData,
  mem::{align_of, size_of, ManuallyDrop},
  num::*,
  ptr::NonNull,
};

pub use bytemuck::*;

pub mod arch;

pub mod math;
pub use math::*;

/// The
/// [wrapping](https://doc.rust-lang.org/std/primitive.i8.html#method.wrapping_abs)
/// absolute value of any signed integer.
///
/// * `branchless_abs!(a, type)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_abs {
  ($x:expr, $u:ty) => {{
    const SHIFT: u32 = core::mem::size_of::<$u>().wrapping_mul(8).wrapping_sub(1) as u32;
    let mask = ($x as $u).wrapping_shr(SHIFT);
    ($x as $u).wrapping_add(mask) ^ mask
  }};
}

/// The minimum of two integer values.
///
/// * `branchless_min!(a, b, type)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_min {
  ($x:expr, $y:expr, $u:ty) => {
    $y ^ (($x ^ $y) & (($x < $y) as $u).wrapping_neg())
  };
}

/// The maximum of two integer values.
///
/// * `branchless_max!(a, b, type)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_max {
  ($x:expr, $y:expr, $u:ty) => {
    $x ^ (($x ^ $y) & (($x < $y) as $u).wrapping_neg())
  };
}

/// If an unsigned value is a power of 2 (or is 0).
///
/// * `branchless_pow_of_2_or_zero!(a)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_pow_of_2_or_zero {
  ($v:expr) => {
    $v & $v.wrapping_sub(1) == 0
  };
}

/// If two signed values have opposite sign (zero counts as positive).
///
/// * `branchless_opposite_sign!(x, y)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_opposite_sign {
  ($x:expr, $y:expr) => {
    ($x ^ $y) < 0
  };
}

/// Force bits of an unsigned value off or on.
///
/// * `branchless_force_bits!(flag, mask, word, type)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_force_bits {
  ($f:expr, $m:expr, $w:expr, $t:ty) => {
    // Note(Lokathor): super-scalar form: (w & ~m) | (-f & m)
    $w ^ ((($f as $t).wrapping_neg() ^ $w) & $m)
  };
}

/// Negate a signed integer value or not.
///
/// * `branchless_negate!(negate, val, type)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_negate {
  ($f_negate:expr, $v:expr, $t:ty) => {
    ($v ^ ($f_negate as $t).wrapping_neg()).wrapping_add($f_negate as $t)
  };
}

/// Bitwise merge two values according to a bit mask.
///
/// * `branchless_masked_merge!(mask_yes, mask_no, mask)`
///
/// Branchless, so you can use it in a `const` context.
#[macro_export]
macro_rules! branchless_masked_merge {
  ($mask_yes:expr, $mask_no:expr, $mask:expr) => {
    (($mask_yes ^ $mask_no) & $mask) ^ $mask_no
  };
}

/// Wrap the inner value to a minimum alignment of 2.
///
/// This is for alignment shenanigans, you're not expected to use it in a
/// struct, more just in function arguments and such.
#[derive(Debug, Clone, Copy)]
#[repr(align(2))]
pub struct Align2<T>(pub T);

/// Wrap the inner value to a minimum alignment of 4.
///
/// This is for alignment shenanigans, you're not expected to use it in a
/// struct, more just in function arguments and such.
#[derive(Debug, Clone, Copy)]
#[repr(align(4))]
pub struct Align4<T>(pub T);

/// Wrap the inner value to a minimum alignment of 8.
///
/// This is for alignment shenanigans, you're not expected to use it in a
/// struct, more just in function arguments and such.
#[derive(Debug, Clone, Copy)]
#[repr(align(8))]
pub struct Align8<T>(pub T);

/// Wrap the inner value to a minimum alignment of 16.
///
/// This is for alignment shenanigans, you're not expected to use it in a
/// struct, more just in function arguments and such.
#[derive(Debug, Clone, Copy)]
#[repr(align(16))]
pub struct Align16<T>(pub T);

/// Wrap the inner value to a minimum alignment of 32.
///
/// This is for alignment shenanigans, you're not expected to use it in a
/// struct, more just in function arguments and such.
#[derive(Debug, Clone, Copy)]
#[repr(align(32))]
pub struct Align32<T>(pub T);

unsafe impl<T> Zeroable for Align2<T> where T: Zeroable {}
unsafe impl<T> Zeroable for Align4<T> where T: Zeroable {}
unsafe impl<T> Zeroable for Align8<T> where T: Zeroable {}
unsafe impl<T> Zeroable for Align16<T> where T: Zeroable {}
unsafe impl<T> Zeroable for Align32<T> where T: Zeroable {}
//
unsafe impl Pod for Align2<[u8; 2]> {}
unsafe impl Pod for Align2<[i8; 2]> {}
//
unsafe impl Pod for Align4<[u8; 4]> {}
unsafe impl Pod for Align4<[i8; 4]> {}
unsafe impl Pod for Align4<[u16; 2]> {}
unsafe impl Pod for Align4<[i16; 2]> {}
//
unsafe impl Pod for Align8<[u8; 8]> {}
unsafe impl Pod for Align8<[i8; 8]> {}
unsafe impl Pod for Align8<[u16; 4]> {}
unsafe impl Pod for Align8<[i16; 4]> {}
unsafe impl Pod for Align8<[u32; 2]> {}
unsafe impl Pod for Align8<[i32; 2]> {}
unsafe impl Pod for Align8<[f32; 2]> {}
//
unsafe impl Pod for Align16<[u8; 16]> {}
unsafe impl Pod for Align16<[i8; 16]> {}
unsafe impl Pod for Align16<[u16; 8]> {}
unsafe impl Pod for Align16<[i16; 8]> {}
unsafe impl Pod for Align16<[u32; 4]> {}
unsafe impl Pod for Align16<[i32; 4]> {}
unsafe impl Pod for Align16<[f32; 4]> {}
unsafe impl Pod for Align16<[u64; 2]> {}
unsafe impl Pod for Align16<[i64; 2]> {}
unsafe impl Pod for Align16<[f64; 2]> {}
unsafe impl Pod for Align16<u128> {}
unsafe impl Pod for Align16<i128> {}
//
unsafe impl Pod for Align32<[u8; 32]> {}
unsafe impl Pod for Align32<[i8; 32]> {}
unsafe impl Pod for Align32<[u16; 16]> {}
unsafe impl Pod for Align32<[i16; 16]> {}
unsafe impl Pod for Align32<[u32; 8]> {}
unsafe impl Pod for Align32<[i32; 8]> {}
unsafe impl Pod for Align32<[f32; 8]> {}
unsafe impl Pod for Align32<[u64; 4]> {}
unsafe impl Pod for Align32<[i64; 4]> {}
unsafe impl Pod for Align32<[f64; 4]> {}
unsafe impl Pod for Align32<[u128; 2]> {}
unsafe impl Pod for Align32<[i128; 2]> {}
//
#[cfg(target_pointer_width = "32")]
unsafe impl Pod for Align8<[usize; 2]> {}
#[cfg(target_pointer_width = "32")]
unsafe impl Pod for Align16<[usize; 4]> {}
#[cfg(target_pointer_width = "32")]
unsafe impl Pod for Align32<[usize; 8]> {}
//
#[cfg(target_pointer_width = "64")]
unsafe impl Pod for Align16<[usize; 2]> {}
#[cfg(target_pointer_width = "64")]
unsafe impl Pod for Align32<[usize; 4]> {}