-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathfma.rs
More file actions
176 lines (146 loc) · 4.99 KB
/
Copy pathfma.rs
File metadata and controls
176 lines (146 loc) · 4.99 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
#![feature(portable_simd)]
use core_simd::simd::prelude::*;
use std_float::StdFloat;
#[test]
fn test_mul_add_basic() {
let a = f32x4::from_array([2.0, 3.0, 4.0, 5.0]);
let b = f32x4::from_array([10.0, 10.0, 10.0, 10.0]);
let c = f32x4::from_array([1.0, 2.0, 3.0, 4.0]);
assert_eq!(a.mul_add(b, c), f32x4::from_array([21.0, 32.0, 43.0, 54.0]));
}
#[test]
fn test_mul_add_f64() {
let a = f64x4::from_array([2.0, 3.0, 4.0, 5.0]);
let b = f64x4::from_array([10.0, 10.0, 10.0, 10.0]);
let c = f64x4::from_array([1.0, 2.0, 3.0, 4.0]);
assert_eq!(a.mul_add(b, c), f64x4::from_array([21.0, 32.0, 43.0, 54.0]));
}
#[test]
fn test_mul_sub_basic() {
let a = f32x4::from_array([2.0, 3.0, 4.0, 5.0]);
let b = f32x4::from_array([10.0, 10.0, 10.0, 10.0]);
let c = f32x4::from_array([1.0, 2.0, 3.0, 4.0]);
assert_eq!(a.mul_sub(b, c), f32x4::from_array([19.0, 28.0, 37.0, 46.0]));
}
#[test]
fn test_mul_sub_f64() {
let a = f64x4::from_array([2.0, 3.0, 4.0, 5.0]);
let b = f64x4::from_array([10.0, 10.0, 10.0, 10.0]);
let c = f64x4::from_array([1.0, 2.0, 3.0, 4.0]);
assert_eq!(a.mul_sub(b, c), f64x4::from_array([19.0, 28.0, 37.0, 46.0]));
}
#[test]
fn test_fma_accuracy_catastrophic_cancellation() {
let epsilon = 1e-4_f32;
let x = 1.0 + epsilon;
let y = 1.0 - epsilon;
let a = f32x4::splat(x);
let b = f32x4::splat(y);
let c = f32x4::splat(-1.0);
let fma_result = a.mul_add(b, c);
let separate_result = a * b + c;
let expected = -epsilon * epsilon;
let fma_error = (fma_result[0] - expected).abs();
let sep_error = (separate_result[0] - expected).abs();
assert!(fma_error <= sep_error);
}
#[test]
fn test_fma_accuracy_discriminant() {
let b = f64x2::splat(1e8);
let four_ac = f64x2::splat(1.0);
let fma_discriminant = b.mul_add(b, -four_ac);
let sep_discriminant = b * b - four_ac;
let expected = 1e16 - 1.0;
let fma_error = ((fma_discriminant[0] - expected) / expected).abs();
let sep_error = ((sep_discriminant[0] - expected) / expected).abs();
assert!(fma_error <= sep_error);
}
#[test]
fn test_fma_accuracy_polynomial() {
let x = f64x2::splat(1.00001);
let a = f64x2::splat(1.0);
let b = f64x2::splat(-2.0);
let c = f64x2::splat(1.0);
let fma_result = a.mul_add(x, b).mul_add(x, c);
let sep_result = (a * x + b) * x + c;
let expected = (x[0] - 1.0) * (x[0] - 1.0);
let fma_error = (fma_result[0] - expected).abs();
let sep_error = (sep_result[0] - expected).abs();
assert!(fma_error < sep_error || (fma_error - sep_error).abs() < 1e-15);
}
#[test]
fn test_negative_values() {
let a = f32x4::from_array([-2.0, -3.0, -4.0, -5.0]);
let b = f32x4::splat(2.0);
let c = f32x4::splat(1.0);
assert_eq!(a.mul_add(b, c), f32x4::from_array([-3.0, -5.0, -7.0, -9.0]));
assert_eq!(
a.mul_sub(b, c),
f32x4::from_array([-5.0, -7.0, -9.0, -11.0])
);
}
#[test]
fn test_infinity() {
let a = f32x4::from_array([f32::INFINITY, 1.0, 2.0, 3.0]);
let b = f32x4::splat(2.0);
let c = f32x4::splat(1.0);
let result = a.mul_add(b, c);
assert_eq!(result[0], f32::INFINITY);
assert_eq!(result[1], 3.0);
}
#[test]
fn test_nan_propagation() {
let a = f32x4::from_array([f32::NAN, 2.0, 3.0, 4.0]);
let b = f32x4::splat(2.0);
let c = f32x4::splat(1.0);
let result = a.mul_add(b, c);
assert!(result[0].is_nan());
assert_eq!(result[1], 5.0);
}
#[test]
fn test_different_sizes() {
let a2 = f32x2::from_array([3.0, 4.0]);
let b2 = f32x2::from_array([2.0, 2.0]);
let c2 = f32x2::from_array([1.0, 1.0]);
assert_eq!(a2.mul_add(b2, c2), f32x2::from_array([7.0, 9.0]));
let a8 = f32x8::splat(2.0);
let b8 = f32x8::splat(3.0);
let c8 = f32x8::splat(4.0);
assert_eq!(a8.mul_add(b8, c8), f32x8::splat(10.0));
}
#[test]
fn test_polynomial_evaluation() {
let x = f32x4::from_array([1.0, 2.0, 3.0, 4.0]);
let result = f32x4::splat(2.0)
.mul_add(x, f32x4::splat(3.0))
.mul_add(x, f32x4::splat(5.0));
assert_eq!(result, f32x4::from_array([10.0, 19.0, 32.0, 49.0]));
}
#[test]
fn test_max_min_values() {
let a = f32x4::from_array([f32::MAX, f32::MIN, 1.0, -1.0]);
let b = f32x4::splat(1.0);
let c = f32x4::splat(0.0);
let result = a.mul_add(b, c);
assert_eq!(result[0], f32::MAX);
assert_eq!(result[1], f32::MIN);
}
#[test]
fn test_subnormal_values() {
let subnormal = f32::MIN_POSITIVE / 2.0;
let a = f32x4::splat(subnormal);
let b = f32x4::splat(2.0);
let c = f32x4::splat(0.0);
let result = a.mul_add(b, c);
assert!(result[0].is_finite());
// On platforms with flush-to-zero (FTZ) mode (e.g., ARM NEON), subnormal
// values in SIMD operations may be flushed to zero for performance.
// We accept either the mathematically correct result or zero.
let expected = subnormal * 2.0;
assert!(
result[0] == expected || result[0] == 0.0,
"Expected {} (or 0.0 due to FTZ), got {}",
expected,
result[0]
);
}