-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmath.v
More file actions
176 lines (153 loc) · 2.88 KB
/
bmath.v
File metadata and controls
176 lines (153 loc) · 2.88 KB
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
// An implementation of saturation arithmetic
// and other error mitigation functions
//
// https://en.wikipedia.org/wiki/Saturation_arithmetic
module bmath
// Saturation addition
pub fn add[T](a T, b T) T {
c := a + b
$match T {
u8, u16, u32, u64 {
if c < a {
return max[T]()
} else {
return c
}
}
i8, i16, i32, i64 {
aa := abs[T](a)
ba := abs[T](b)
if (a == aa && b != ba) || (a != aa && b == ba) {
return c
} else {
if a == aa && c < a {
return max[T]()
} else if a != aa && c > a {
return min[T]()
} else {
return c
}
}
}
$else { $compile_error('unsupported type') }
}
}
// Saturation subtraction
pub fn sub[T](a T, b T) T {
c := a - b
$match T {
u8, u16, u32, u64 {
if c > a {
return 0
} else {
return c
}
}
i8, i16, i32, i64 {
aa := abs[T](a)
ba := abs[T](b)
if (a == aa && b == ba) || (a != aa && b != ba) {
return c
} else {
if a == aa && c < a {
return max[T]()
} else if a != aa && c > a {
return min[T]()
} else {
return c
}
}
}
$else { $compile_error('unsupported type') }
}
}
// Saturation multiplication
pub fn mult[T](a T, b T) T {
c := a * b
if a != 0 && c / a != b {
$match T {
u8, u16, u32, u64 {
return max[T]()
}
i8, i16, i32, i64 {
aa := abs[T](a)
ba := abs[T](b)
if (a == aa && b == ba) || (a != aa && b != ba) {
return max[T]()
} else {
return min[T]()
}
}
$else { $compile_error('unsupported type') }
}
} else {
return c
}
}
// Division by zero // x / 0 == 0
// normally returns an error
pub fn div[T](a T, b T) T {
$match T {
u8, u16, u32, u64, i8, i16, i32, i64, f32, f64 {}
$else { $compile_error('unsupported type') }
}
if b == 0 {
return 0
} else {
return a / b
}
}
// Modulo by zero // x % 0 == x
// normally returns an error
pub fn mod[T](a T, b T) T {
$match T {
u8, u16, u32, u64, i8, i16, i32, i64, f32, f64 {}
$else { $compile_error('unsupported type') }
}
if b == 0 {
return a
} else {
return a % b
}
}
// Absolute value // min_ix == max_ix
// normally the math.abs() implementation will return min_ix if the input is min_ix
pub fn abs[T](a T) T {
$match T {
i8, i16, i32, i64 {}
$else { $compile_error('unsupported type') }
}
if a < 0 {
if a == min[T]() {
return max[T]()
}
else {
return a * -1
}
} else {
return a
}
}
// Automatic compile time max and min
fn max[T]() T {
$match T {
u8 { return max_u8 }
u16 { return max_u16 }
u32 { return max_u32 }
u64 { return max_u64 }
i8 { return max_i8 }
i16 { return max_i16 }
i32 { return max_i32 }
i64 { return max_i64 }
$else { $compile_error('unsupported type') }
}
}
fn min[T]() T {
$match T {
i8 { return min_i8 }
i16 { return min_i16 }
i32 { return min_i32 }
i64 { return min_i64 }
$else { $compile_error('unsupported type') }
}
}