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
use crate::prelude::*;
#[derive(Copy, Clone, Debug)]
pub enum Sample {
F16(f16),
F32(f32),
U32(u32)
}
impl Sample {
pub fn f32(f32: f32) -> Self { Sample::F32(f32) }
pub fn f16(f16: f16) -> Self { Sample::F16(f16) }
pub fn u32(u32: u32) -> Self { Sample::U32(u32) }
#[inline]
pub fn to_f16(self) -> f16 {
match self {
Sample::F16(sample) => sample,
Sample::F32(sample) => f16::from_f32(sample),
Sample::U32(sample) => f16::from_f32(sample as f32),
}
}
#[inline]
pub fn to_f32(self) -> f32 {
match self {
Sample::F32(sample) => sample,
Sample::F16(sample) => sample.to_f32(),
Sample::U32(sample) => sample as f32,
}
}
#[inline]
pub fn to_u32(self) -> u32 {
match self {
Sample::F16(sample) => sample.to_f32() as u32,
Sample::F32(sample) => sample as u32,
Sample::U32(sample) => sample,
}
}
#[inline]
pub fn is_nan(self) -> bool {
match self {
Sample::F16(value) => value.is_nan(),
Sample::F32(value) => value.is_nan(),
Sample::U32(_) => false,
}
}
#[inline]
pub fn is_zero(&self) -> bool {
match *self {
Sample::F16(value) => value == f16::ZERO || value == f16::NEG_ZERO,
Sample::F32(value) => value == 0.0,
Sample::U32(value) => value == 0,
}
}
}
impl PartialEq for Sample {
fn eq(&self, other: &Self) -> bool {
match *self {
Sample::F16(num) => num == other.to_f16(),
Sample::F32(num) => num == other.to_f32(),
Sample::U32(num) => num == other.to_u32(),
}
}
}
impl Default for Sample {
fn default() -> Self { Sample::F32(0.0) }
}
impl From<f16> for Sample { #[inline] fn from(f: f16) -> Self { Sample::F16(f) } }
impl From<f32> for Sample { #[inline] fn from(f: f32) -> Self { Sample::F32(f) } }
impl From<u32> for Sample { #[inline] fn from(f: u32) -> Self { Sample::U32(f) } }
impl<T> From<Option<T>> for Sample where T: Into<Sample> + Default {
#[inline] fn from(num: Option<T>) -> Self { num.unwrap_or_default().into() }
}
impl From<Sample> for f16 { #[inline] fn from(s: Sample) -> Self { s.to_f16() } }
impl From<Sample> for f32 { #[inline] fn from(s: Sample) -> Self { s.to_f32() } }
impl From<Sample> for u32 { #[inline] fn from(s: Sample) -> Self { s.to_u32() } }
pub trait FromNativeSample: Sized + Copy + Default + 'static {
fn from_f16(value: f16) -> Self;
fn from_f32(value: f32) -> Self;
fn from_u32(value: u32) -> Self;
}
impl FromNativeSample for f32 {
fn from_f16(value: f16) -> Self { value.to_f32() }
fn from_f32(value: f32) -> Self { value } fn from_u32(value: u32) -> Self { value as f32 }
}
impl FromNativeSample for u32 {
fn from_f16(value: f16) -> Self { value.to_f32() as u32 }
fn from_f32(value: f32) -> Self { value as u32 }
fn from_u32(value: u32) -> Self { value }
}
impl FromNativeSample for f16 {
fn from_f16(value: f16) -> Self { value }
fn from_f32(value: f32) -> Self { f16::from_f32(value) }
fn from_u32(value: u32) -> Self { f16::from_f32(value as f32) }
}
impl FromNativeSample for Sample {
fn from_f16(value: f16) -> Self { Self::from(value) }
fn from_f32(value: f32) -> Self { Self::from(value) }
fn from_u32(value: u32) -> Self { Self::from(value) }
}
pub trait IntoNativeSample: Copy + Default + Sync + 'static {
fn to_f16(&self) -> f16;
fn to_f32(&self) -> f32;
fn to_u32(&self) -> u32;
}
impl IntoNativeSample for f16 {
fn to_f16(&self) -> f16 { f16::from_f16(*self) }
fn to_f32(&self) -> f32 { f32::from_f16(*self) }
fn to_u32(&self) -> u32 { u32::from_f16(*self) }
}
impl IntoNativeSample for f32 {
fn to_f16(&self) -> f16 { f16::from_f32(*self) }
fn to_f32(&self) -> f32 { f32::from_f32(*self) }
fn to_u32(&self) -> u32 { u32::from_f32(*self) }
}
impl IntoNativeSample for u32 {
fn to_f16(&self) -> f16 { f16::from_u32(*self) }
fn to_f32(&self) -> f32 { f32::from_u32(*self) }
fn to_u32(&self) -> u32 { u32::from_u32(*self) }
}
impl IntoNativeSample for Sample {
fn to_f16(&self) -> f16 { Sample::to_f16(*self) }
fn to_f32(&self) -> f32 { Sample::to_f32(*self) }
fn to_u32(&self) -> u32 { Sample::to_u32(*self) }
}