c1d8095f2ca78bcde9e13ee19132afd676b25448
[vector-math.git] / src / traits.rs
1 use core::ops::{
2 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
3 Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
4 };
5
6 use crate::f16;
7
8 #[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823
9 macro_rules! make_float_type {
10 (
11 #[u32 = $u32:ident]
12 #[bool = $bool:ident]
13 [
14 $({
15 #[uint]
16 $uint_smaller:ident;
17 #[int]
18 $int_smaller:ident;
19 $(
20 #[float]
21 $float_smaller:ident;
22 )?
23 },)*
24 ],
25 {
26 #[uint]
27 $uint:ident;
28 #[int]
29 $int:ident;
30 #[float(prim = $float_prim:ident $(, scalar = $float_scalar:ident)?)]
31 $float:ident;
32 },
33 [
34 $({
35 #[uint]
36 $uint_larger:ident;
37 #[int]
38 $int_larger:ident;
39 $(
40 #[float]
41 $float_larger:ident;
42 )?
43 },)*
44 ]
45 ) => {
46 type $float: Float<Self::$u32, BitsType = Self::$uint>
47 $(+ From<Self::$float_scalar>)?
48 + Compare<Bool = Self::$bool>
49 + Make<Self, Prim = $float_prim>
50 $(+ ConvertTo<Self::$uint_smaller>)*
51 $(+ ConvertTo<Self::$int_smaller>)*
52 $($(+ ConvertTo<Self::$float_smaller>)?)*
53 + ConvertTo<Self::$uint>
54 + ConvertTo<Self::$int>
55 $(+ ConvertTo<Self::$uint_larger>)*
56 $(+ ConvertTo<Self::$int_larger>)*
57 $($(+ Into<Self::$float_larger>)?)*;
58 };
59 (
60 #[u32 = $u32:ident]
61 #[bool = $bool:ident]
62 [$($smaller:tt,)*],
63 {
64 #[uint]
65 $uint:ident;
66 #[int]
67 $int:ident;
68 },
69 [$($larger:tt,)*]
70 ) => {};
71 }
72
73 #[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823
74 macro_rules! make_uint_int_float_type {
75 (
76 #[u32 = $u32:ident]
77 #[bool = $bool:ident]
78 [
79 $({
80 #[uint($($uint_smaller_traits:tt)*)]
81 $uint_smaller:ident;
82 #[int($($int_smaller_traits:tt)*)]
83 $int_smaller:ident;
84 $(
85 #[float($($float_smaller_traits:tt)*)]
86 $float_smaller:ident;
87 )?
88 },)*
89 ],
90 {
91 #[uint(prim = $uint_prim:ident $(, scalar = $uint_scalar:ident)?)]
92 $uint:ident;
93 #[int(prim = $int_prim:ident $(, scalar = $int_scalar:ident)?)]
94 $int:ident;
95 $(
96 #[float(prim = $float_prim:ident $(, scalar = $float_scalar:ident)?)]
97 $float:ident;
98 )?
99 },
100 [
101 $({
102 #[uint($($uint_larger_traits:tt)*)]
103 $uint_larger:ident;
104 #[int($($int_larger_traits:tt)*)]
105 $int_larger:ident;
106 $(
107 #[float($($float_larger_traits:tt)*)]
108 $float_larger:ident;
109 )?
110 },)*
111 ]
112 ) => {
113 type $uint: UInt<Self::$u32>
114 $(+ From<Self::$uint_scalar>)?
115 + Compare<Bool = Self::$bool>
116 + Make<Self, Prim = $uint_prim>
117 $(+ ConvertTo<Self::$uint_smaller>)*
118 $(+ ConvertTo<Self::$int_smaller>)*
119 $($(+ ConvertTo<Self::$float_smaller>)?)*
120 + ConvertTo<Self::$int>
121 $(+ ConvertTo<Self::$float>)?
122 $(+ Into<Self::$uint_larger>)*
123 $(+ Into<Self::$int_larger>)*
124 $($(+ Into<Self::$float_larger>)?)*;
125 type $int: SInt<Self::$u32>
126 $(+ From<Self::$int_scalar>)?
127 + Compare<Bool = Self::$bool>
128 + Make<Self, Prim = $int_prim>
129 $(+ ConvertTo<Self::$uint_smaller>)*
130 $(+ ConvertTo<Self::$int_smaller>)*
131 $($(+ ConvertTo<Self::$float_smaller>)?)*
132 + ConvertTo<Self::$uint>
133 $(+ ConvertTo<Self::$float>)?
134 $(+ ConvertTo<Self::$uint_larger>)*
135 $(+ Into<Self::$int_larger>)*
136 $($(+ Into<Self::$float_larger>)?)*;
137 make_float_type! {
138 #[u32 = $u32]
139 #[bool = $bool]
140 [
141 $({
142 #[uint]
143 $uint_smaller;
144 #[int]
145 $int_smaller;
146 $(
147 #[float]
148 $float_smaller;
149 )?
150 },)*
151 ],
152 {
153 #[uint]
154 $uint;
155 #[int]
156 $int;
157 $(
158 #[float(prim = $float_prim $(, scalar = $float_scalar)?)]
159 $float;
160 )?
161 },
162 [
163 $({
164 #[uint]
165 $uint_larger;
166 #[int]
167 $int_larger;
168 $(
169 #[float]
170 $float_larger;
171 )?
172 },)*
173 ]
174 }
175 };
176 }
177
178 macro_rules! make_uint_int_float_types {
179 (
180 #[u32 = $u32:ident]
181 #[bool = $bool:ident]
182 [$($smaller:tt,)*],
183 $current:tt,
184 [$first_larger:tt, $($larger:tt,)*]
185 ) => {
186 make_uint_int_float_type! {
187 #[u32 = $u32]
188 #[bool = $bool]
189 [$($smaller,)*],
190 $current,
191 [$first_larger, $($larger,)*]
192 }
193 make_uint_int_float_types! {
194 #[u32 = $u32]
195 #[bool = $bool]
196 [$($smaller,)* $current,],
197 $first_larger,
198 [$($larger,)*]
199 }
200 };
201 (
202 #[u32 = $u32:ident]
203 #[bool = $bool:ident]
204 [$($smaller:tt,)*],
205 $current:tt,
206 []
207 ) => {
208 make_uint_int_float_type! {
209 #[u32 = $u32]
210 #[bool = $bool]
211 [$($smaller,)*],
212 $current,
213 []
214 }
215 };
216 }
217
218 #[rustfmt::skip] // work around for https://github.com/rust-lang/rustfmt/issues/4823
219 macro_rules! make_types {
220 (
221 #[bool]
222 $(#[scalar = $ScalarBool:ident])?
223 type $Bool:ident;
224
225 #[u8]
226 $(#[scalar = $ScalarU8:ident])?
227 type $U8:ident;
228
229 #[u16]
230 $(#[scalar = $ScalarU16:ident])?
231 type $U16:ident;
232
233 #[u32]
234 $(#[scalar = $ScalarU32:ident])?
235 type $U32:ident;
236
237 #[u64]
238 $(#[scalar = $ScalarU64:ident])?
239 type $U64:ident;
240
241 #[i8]
242 $(#[scalar = $ScalarI8:ident])?
243 type $I8:ident;
244
245 #[i16]
246 $(#[scalar = $ScalarI16:ident])?
247 type $I16:ident;
248
249 #[i32]
250 $(#[scalar = $ScalarI32:ident])?
251 type $I32:ident;
252
253 #[i64]
254 $(#[scalar = $ScalarI64:ident])?
255 type $I64:ident;
256
257 #[f16]
258 $(#[scalar = $ScalarF16:ident])?
259 type $F16:ident;
260
261 #[f32]
262 $(#[scalar = $ScalarF32:ident])?
263 type $F32:ident;
264
265 #[f64]
266 $(#[scalar = $ScalarF64:ident])?
267 type $F64:ident;
268 ) => {
269 type $Bool: Bool
270 $(+ From<Self::$ScalarBool>)?
271 + Make<Self, Prim = bool>
272 + Select<Self::$Bool>;
273 make_uint_int_float_types! {
274 #[u32 = $U32]
275 #[bool = $Bool]
276 [],
277 {
278 #[uint(prim = u8 $(, scalar = $ScalarU8)?)]
279 $U8;
280 #[int(prim = i8 $(, scalar = $ScalarI8)?)]
281 $I8;
282 },
283 [
284 {
285 #[uint(prim = u16 $(, scalar = $ScalarU16)?)]
286 $U16;
287 #[int(prim = i16 $(, scalar = $ScalarI16)?)]
288 $I16;
289 #[float(prim = f16 $(, scalar = $ScalarF16)?)]
290 $F16;
291 },
292 {
293 #[uint(prim = u32 $(, scalar = $ScalarU32)?)]
294 $U32;
295 #[int(prim = i32 $(, scalar = $ScalarI32)?)]
296 $I32;
297 #[float(prim = f32 $(, scalar = $ScalarF32)?)]
298 $F32;
299 },
300 {
301 #[uint(prim = u64 $(, scalar = $ScalarU64)?)]
302 $U64;
303 #[int(prim = i64 $(, scalar = $ScalarI64)?)]
304 $I64;
305 #[float(prim = f64 $(, scalar = $ScalarF64)?)]
306 $F64;
307 },
308 ]
309 }
310 };
311 }
312
313 /// reference used to build IR for Kazan; an empty type for `core::simd`
314 pub trait Context: Copy {
315 make_types! {
316 #[bool]
317 type Bool;
318
319 #[u8]
320 type U8;
321
322 #[u16]
323 type U16;
324
325 #[u32]
326 type U32;
327
328 #[u64]
329 type U64;
330
331 #[i8]
332 type I8;
333
334 #[i16]
335 type I16;
336
337 #[i32]
338 type I32;
339
340 #[i64]
341 type I64;
342
343 #[f16]
344 type F16;
345
346 #[f32]
347 type F32;
348
349 #[f64]
350 type F64;
351 }
352 make_types! {
353 #[bool]
354 #[scalar = Bool]
355 type VecBool;
356
357 #[u8]
358 #[scalar = U8]
359 type VecU8;
360
361 #[u16]
362 #[scalar = U16]
363 type VecU16;
364
365 #[u32]
366 #[scalar = U32]
367 type VecU32;
368
369 #[u64]
370 #[scalar = U64]
371 type VecU64;
372
373 #[i8]
374 #[scalar = I8]
375 type VecI8;
376
377 #[i16]
378 #[scalar = I16]
379 type VecI16;
380
381 #[i32]
382 #[scalar = I32]
383 type VecI32;
384
385 #[i64]
386 #[scalar = I64]
387 type VecI64;
388
389 #[f16]
390 #[scalar = F16]
391 type VecF16;
392
393 #[f32]
394 #[scalar = F32]
395 type VecF32;
396
397 #[f64]
398 #[scalar = F64]
399 type VecF64;
400 }
401 fn make<T: Make<Self>>(self, v: T::Prim) -> T {
402 T::make(self, v)
403 }
404 }
405
406 pub trait Make<Context>: Sized {
407 type Prim;
408 fn make(ctx: Context, v: Self::Prim) -> Self;
409 }
410
411 pub trait ConvertTo<T> {
412 fn to(self) -> T;
413 }
414
415 impl<T, U: Into<T>> ConvertTo<T> for U {
416 fn to(self) -> T {
417 self.into()
418 }
419 }
420
421 pub trait Number:
422 Compare
423 + Add<Output = Self>
424 + Sub<Output = Self>
425 + Mul<Output = Self>
426 + Div<Output = Self>
427 + Rem<Output = Self>
428 + AddAssign
429 + SubAssign
430 + MulAssign
431 + DivAssign
432 + RemAssign
433 {
434 }
435
436 pub trait BitOps:
437 Copy
438 + BitAnd<Output = Self>
439 + BitOr<Output = Self>
440 + BitXor<Output = Self>
441 + Not<Output = Self>
442 + BitAndAssign
443 + BitOrAssign
444 + BitXorAssign
445 {
446 }
447
448 pub trait Int<ShiftRhs>:
449 Number
450 + BitOps
451 + Shl<ShiftRhs, Output = Self>
452 + Shr<ShiftRhs, Output = Self>
453 + ShlAssign<ShiftRhs>
454 + ShrAssign<ShiftRhs>
455 {
456 }
457
458 pub trait UInt<ShiftRhs>: Int<ShiftRhs> {}
459
460 pub trait SInt<ShiftRhs>: Int<ShiftRhs> + Neg<Output = Self> {}
461
462 pub trait Float<BitsShiftRhs>: Number + Neg<Output = Self> {
463 type BitsType: UInt<BitsShiftRhs>;
464 fn abs(self) -> Self;
465 fn trunc(self) -> Self;
466 fn ceil(self) -> Self;
467 fn floor(self) -> Self;
468 fn round(self) -> Self;
469 fn fma(self, a: Self, b: Self) -> Self;
470 fn is_nan(self) -> Self::Bool;
471 fn is_infinity(self) -> Self::Bool;
472 fn is_finite(self) -> Self::Bool;
473 fn from_bits(v: Self::BitsType) -> Self;
474 fn to_bits(self) -> Self::BitsType;
475 }
476
477 pub trait Bool: BitOps {}
478
479 pub trait Select<T>: Bool {
480 fn select(self, true_v: T, false_v: T) -> T;
481 }
482
483 pub trait Compare: Copy {
484 type Bool: Bool + Select<Self>;
485 fn eq(self, rhs: Self) -> Self::Bool;
486 fn ne(self, rhs: Self) -> Self::Bool;
487 fn lt(self, rhs: Self) -> Self::Bool;
488 fn gt(self, rhs: Self) -> Self::Bool;
489 fn le(self, rhs: Self) -> Self::Bool;
490 fn ge(self, rhs: Self) -> Self::Bool;
491 }