implement sin_cos_pi_f32
[vector-math.git] / src / prim.rs
1 use crate::{
2 f16::F16,
3 traits::{ConvertFrom, ConvertTo},
4 };
5 use core::{fmt, hash, ops};
6
7 mod sealed {
8 use crate::f16::F16;
9
10 pub trait Sealed {}
11 impl Sealed for F16 {}
12 impl Sealed for f32 {}
13 impl Sealed for f64 {}
14 impl Sealed for u8 {}
15 impl Sealed for u16 {}
16 impl Sealed for u32 {}
17 impl Sealed for u64 {}
18 impl Sealed for i8 {}
19 impl Sealed for i16 {}
20 impl Sealed for i32 {}
21 impl Sealed for i64 {}
22 }
23
24 pub trait PrimBase:
25 sealed::Sealed
26 + Copy
27 + 'static
28 + Send
29 + Sync
30 + PartialOrd
31 + fmt::Debug
32 + fmt::Display
33 + ops::Add<Output = Self>
34 + ops::Sub<Output = Self>
35 + ops::Mul<Output = Self>
36 + ops::Div<Output = Self>
37 + ops::Rem<Output = Self>
38 + ops::AddAssign
39 + ops::SubAssign
40 + ops::MulAssign
41 + ops::DivAssign
42 + ops::RemAssign
43 + ConvertFrom<i8>
44 + ConvertFrom<u8>
45 + ConvertFrom<i16>
46 + ConvertFrom<u16>
47 + ConvertFrom<F16>
48 + ConvertFrom<i32>
49 + ConvertFrom<u32>
50 + ConvertFrom<f32>
51 + ConvertFrom<i64>
52 + ConvertFrom<u64>
53 + ConvertFrom<f64>
54 + ConvertTo<i8>
55 + ConvertTo<u8>
56 + ConvertTo<i16>
57 + ConvertTo<u16>
58 + ConvertTo<F16>
59 + ConvertTo<i32>
60 + ConvertTo<u32>
61 + ConvertTo<f32>
62 + ConvertTo<i64>
63 + ConvertTo<u64>
64 + ConvertTo<f64>
65 {
66 }
67
68 pub trait PrimInt:
69 PrimBase
70 + Ord
71 + hash::Hash
72 + fmt::Binary
73 + fmt::LowerHex
74 + fmt::Octal
75 + fmt::UpperHex
76 + ops::BitAnd<Output = Self>
77 + ops::BitOr<Output = Self>
78 + ops::BitXor<Output = Self>
79 + ops::Shl<Output = Self>
80 + ops::Shr<Output = Self>
81 + ops::Not<Output = Self>
82 + ops::BitAndAssign
83 + ops::BitOrAssign
84 + ops::BitXorAssign
85 + ops::ShlAssign
86 + ops::ShrAssign
87 {
88 }
89
90 pub trait PrimUInt: PrimInt + ConvertFrom<Self::SignedType> {
91 type SignedType: PrimSInt<UnsignedType = Self> + ConvertFrom<Self>;
92 }
93
94 pub trait PrimSInt: PrimInt + ops::Neg<Output = Self> + ConvertFrom<Self::UnsignedType> {
95 type UnsignedType: PrimUInt<SignedType = Self> + ConvertFrom<Self>;
96 }
97
98 macro_rules! impl_int {
99 ($uint:ident, $sint:ident) => {
100 impl PrimBase for $uint {}
101 impl PrimBase for $sint {}
102 impl PrimInt for $uint {}
103 impl PrimInt for $sint {}
104 impl PrimUInt for $uint {
105 type SignedType = $sint;
106 }
107 impl PrimSInt for $sint {
108 type UnsignedType = $uint;
109 }
110 };
111 }
112
113 impl_int!(u8, i8);
114 impl_int!(u16, i16);
115 impl_int!(u32, i32);
116 impl_int!(u64, i64);
117
118 pub trait PrimFloat:
119 PrimBase + ops::Neg<Output = Self> + ConvertFrom<Self::BitsType> + ConvertFrom<Self::SignedBitsType>
120 {
121 type BitsType: PrimUInt<SignedType = Self::SignedBitsType> + ConvertFrom<Self>;
122 type SignedBitsType: PrimSInt<UnsignedType = Self::BitsType> + ConvertFrom<Self>;
123 const EXPONENT_BIAS_UNSIGNED: Self::BitsType;
124 const EXPONENT_BIAS_SIGNED: Self::SignedBitsType;
125 const SIGN_FIELD_WIDTH: Self::BitsType;
126 const EXPONENT_FIELD_WIDTH: Self::BitsType;
127 const MANTISSA_FIELD_WIDTH: Self::BitsType;
128 const SIGN_FIELD_SHIFT: Self::BitsType;
129 const EXPONENT_FIELD_SHIFT: Self::BitsType;
130 const MANTISSA_FIELD_SHIFT: Self::BitsType;
131 const SIGN_FIELD_MASK: Self::BitsType;
132 const EXPONENT_FIELD_MASK: Self::BitsType;
133 const MANTISSA_FIELD_MASK: Self::BitsType;
134 const IMPLICIT_MANTISSA_BIT: Self::BitsType;
135 const ZERO_SUBNORMAL_EXPONENT: Self::BitsType;
136 const NAN_INFINITY_EXPONENT: Self::BitsType;
137 const INFINITY_BITS: Self::BitsType;
138 const NAN_BITS: Self::BitsType;
139 fn is_nan(self) -> bool;
140 fn from_bits(bits: Self::BitsType) -> Self;
141 fn to_bits(self) -> Self::BitsType;
142 fn abs(self) -> Self;
143 }
144
145 macro_rules! impl_float {
146 (
147 impl PrimFloat for $float:ident {
148 type BitsType = $bits_type:ident;
149 type SignedBitsType = $signed_bits_type:ident;
150 const EXPONENT_FIELD_WIDTH: u32 = $exponent_field_width:literal;
151 const MANTISSA_FIELD_WIDTH: u32 = $mantissa_field_width:literal;
152 }
153 ) => {
154 impl PrimBase for $float {}
155
156 impl PrimFloat for $float {
157 type BitsType = $bits_type;
158 type SignedBitsType = $signed_bits_type;
159 const EXPONENT_BIAS_UNSIGNED: Self::BitsType =
160 (1 << (Self::EXPONENT_FIELD_WIDTH - 1)) - 1;
161 const EXPONENT_BIAS_SIGNED: Self::SignedBitsType = Self::EXPONENT_BIAS_UNSIGNED as _;
162 const SIGN_FIELD_WIDTH: Self::BitsType = 1;
163 const EXPONENT_FIELD_WIDTH: Self::BitsType = $exponent_field_width;
164 const MANTISSA_FIELD_WIDTH: Self::BitsType = $mantissa_field_width;
165 const SIGN_FIELD_SHIFT: Self::BitsType =
166 Self::EXPONENT_FIELD_SHIFT + Self::EXPONENT_FIELD_WIDTH;
167 const EXPONENT_FIELD_SHIFT: Self::BitsType = Self::MANTISSA_FIELD_WIDTH;
168 const MANTISSA_FIELD_SHIFT: Self::BitsType = 0;
169 const SIGN_FIELD_MASK: Self::BitsType = 1 << Self::SIGN_FIELD_SHIFT;
170 const EXPONENT_FIELD_MASK: Self::BitsType =
171 ((1 << Self::EXPONENT_FIELD_WIDTH) - 1) << Self::EXPONENT_FIELD_SHIFT;
172 const MANTISSA_FIELD_MASK: Self::BitsType = (1 << Self::MANTISSA_FIELD_WIDTH) - 1;
173 const IMPLICIT_MANTISSA_BIT: Self::BitsType = 1 << Self::MANTISSA_FIELD_WIDTH;
174 const ZERO_SUBNORMAL_EXPONENT: Self::BitsType = 0;
175 const NAN_INFINITY_EXPONENT: Self::BitsType = (1 << Self::EXPONENT_FIELD_WIDTH) - 1;
176 const INFINITY_BITS: Self::BitsType =
177 Self::NAN_INFINITY_EXPONENT << Self::EXPONENT_FIELD_SHIFT;
178 const NAN_BITS: Self::BitsType =
179 Self::INFINITY_BITS | (1 << (Self::MANTISSA_FIELD_WIDTH - 1));
180 fn is_nan(self) -> bool {
181 $float::is_nan(self)
182 }
183 fn from_bits(bits: Self::BitsType) -> Self {
184 $float::from_bits(bits)
185 }
186 fn to_bits(self) -> Self::BitsType {
187 self.to_bits()
188 }
189 fn abs(self) -> Self {
190 $float::abs(self)
191 }
192 }
193 };
194 }
195
196 impl_float! {
197 impl PrimFloat for F16 {
198 type BitsType = u16;
199 type SignedBitsType = i16;
200 const EXPONENT_FIELD_WIDTH: u32 = 5;
201 const MANTISSA_FIELD_WIDTH: u32 = 10;
202 }
203 }
204
205 impl_float! {
206 impl PrimFloat for f32 {
207 type BitsType = u32;
208 type SignedBitsType = i32;
209 const EXPONENT_FIELD_WIDTH: u32 = 8;
210 const MANTISSA_FIELD_WIDTH: u32 = 23;
211 }
212 }
213
214 impl_float! {
215 impl PrimFloat for f64 {
216 type BitsType = u64;
217 type SignedBitsType = i64;
218 const EXPONENT_FIELD_WIDTH: u32 = 11;
219 const MANTISSA_FIELD_WIDTH: u32 = 52;
220 }
221 }