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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
const std = @import("std");
const testing = std.testing;
const __truncsfhf2 = @import("truncsfhf2.zig").__truncsfhf2;
const __truncdfhf2 = @import("truncdfhf2.zig").__truncdfhf2;
const __truncdfsf2 = @import("truncdfsf2.zig").__truncdfsf2;
const __trunctfhf2 = @import("trunctfhf2.zig").__trunctfhf2;
const __trunctfsf2 = @import("trunctfsf2.zig").__trunctfsf2;
const __trunctfdf2 = @import("trunctfdf2.zig").__trunctfdf2;
const __trunctfxf2 = @import("trunctfxf2.zig").__trunctfxf2;
fn test__truncsfhf2(a: u32, expected: u16) !void {
const actual: u16 = @bitCast(__truncsfhf2(@bitCast(a)));
if (actual == expected) {
return;
}
return error.TestFailure;
}
test "truncsfhf2" {
try test__truncsfhf2(0x7fc00000, 0x7e00); // qNaN
try test__truncsfhf2(0x7fe00000, 0x7f00); // sNaN
try test__truncsfhf2(0, 0); // 0
try test__truncsfhf2(0x80000000, 0x8000); // -0
try test__truncsfhf2(0x7f800000, 0x7c00); // inf
try test__truncsfhf2(0xff800000, 0xfc00); // -inf
try test__truncsfhf2(0x477ff000, 0x7c00); // 65520 -> inf
try test__truncsfhf2(0xc77ff000, 0xfc00); // -65520 -> -inf
try test__truncsfhf2(0x71cc3892, 0x7c00); // 0x1.987124876876324p+100 -> inf
try test__truncsfhf2(0xf1cc3892, 0xfc00); // -0x1.987124876876324p+100 -> -inf
try test__truncsfhf2(0x38800000, 0x0400); // normal (min), 2**-14
try test__truncsfhf2(0xb8800000, 0x8400); // normal (min), -2**-14
try test__truncsfhf2(0x477fe000, 0x7bff); // normal (max), 65504
try test__truncsfhf2(0xc77fe000, 0xfbff); // normal (max), -65504
try test__truncsfhf2(0x477fe100, 0x7bff); // normal, 65505 -> 65504
try test__truncsfhf2(0xc77fe100, 0xfbff); // normal, -65505 -> -65504
try test__truncsfhf2(0x477fef00, 0x7bff); // normal, 65519 -> 65504
try test__truncsfhf2(0xc77fef00, 0xfbff); // normal, -65519 -> -65504
try test__truncsfhf2(0x3f802000, 0x3c01); // normal, 1 + 2**-10
try test__truncsfhf2(0xbf802000, 0xbc01); // normal, -1 - 2**-10
try test__truncsfhf2(0x3eaaa000, 0x3555); // normal, approx. 1/3
try test__truncsfhf2(0xbeaaa000, 0xb555); // normal, approx. -1/3
try test__truncsfhf2(0x40490fdb, 0x4248); // normal, 3.1415926535
try test__truncsfhf2(0xc0490fdb, 0xc248); // normal, -3.1415926535
try test__truncsfhf2(0x45cc3892, 0x6e62); // normal, 0x1.987124876876324p+12
try test__truncsfhf2(0x3f800000, 0x3c00); // normal, 1
try test__truncsfhf2(0x38800000, 0x0400); // normal, 0x1.0p-14
try test__truncsfhf2(0x33800000, 0x0001); // denormal (min), 2**-24
try test__truncsfhf2(0xb3800000, 0x8001); // denormal (min), -2**-24
try test__truncsfhf2(0x387fc000, 0x03ff); // denormal (max), 2**-14 - 2**-24
try test__truncsfhf2(0xb87fc000, 0x83ff); // denormal (max), -2**-14 + 2**-24
try test__truncsfhf2(0x35800000, 0x0010); // denormal, 0x1.0p-20
try test__truncsfhf2(0x33280000, 0x0001); // denormal, 0x1.5p-25 -> 0x1.0p-24
try test__truncsfhf2(0x33000000, 0x0000); // 0x1.0p-25 -> zero
}
fn test__truncdfhf2(a: f64, expected: u16) void {
const rep: u16 = @bitCast(__truncdfhf2(a));
if (rep == expected) {
return;
}
// test other possible NaN representation(signal NaN)
else if (expected == 0x7e00) {
if ((rep & 0x7c00) == 0x7c00 and (rep & 0x3ff) > 0) {
return;
}
}
@panic("__truncdfhf2 test failure");
}
fn test__truncdfhf2_raw(a: u64, expected: u16) void {
const actual: u16 = @bitCast(__truncdfhf2(@bitCast(a)));
if (actual == expected) {
return;
}
@panic("__truncdfhf2 test failure");
}
test "truncdfhf2" {
test__truncdfhf2_raw(0x7ff8000000000000, 0x7e00); // qNaN
test__truncdfhf2_raw(0x7ff0000000008000, 0x7e00); // NaN
test__truncdfhf2_raw(0x7ff0000000000000, 0x7c00); //inf
test__truncdfhf2_raw(0xfff0000000000000, 0xfc00); // -inf
test__truncdfhf2(0.0, 0x0); // zero
test__truncdfhf2_raw(0x80000000 << 32, 0x8000); // -zero
test__truncdfhf2(3.1415926535, 0x4248);
test__truncdfhf2(-3.1415926535, 0xc248);
test__truncdfhf2(0x1.987124876876324p+1000, 0x7c00);
test__truncdfhf2(0x1.987124876876324p+12, 0x6e62);
test__truncdfhf2(0x1.0p+0, 0x3c00);
test__truncdfhf2(0x1.0p-14, 0x0400);
// denormal
test__truncdfhf2(0x1.0p-20, 0x0010);
test__truncdfhf2(0x1.0p-24, 0x0001);
test__truncdfhf2(-0x1.0p-24, 0x8001);
test__truncdfhf2(0x1.5p-25, 0x0001);
// and back to zero
test__truncdfhf2(0x1.0p-25, 0x0000);
test__truncdfhf2(-0x1.0p-25, 0x8000);
// max (precise)
test__truncdfhf2(65504.0, 0x7bff);
// max (rounded)
test__truncdfhf2(65519.0, 0x7bff);
// max (to +inf)
test__truncdfhf2(65520.0, 0x7c00);
test__truncdfhf2(-65520.0, 0xfc00);
test__truncdfhf2(65536.0, 0x7c00);
}
fn test__trunctfsf2(a: f128, expected: u32) void {
const x = __trunctfsf2(a);
const rep: u32 = @bitCast(x);
if (rep == expected) {
return;
}
// test other possible NaN representation(signal NaN)
else if (expected == 0x7fc00000) {
if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
return;
}
}
@panic("__trunctfsf2 test failure");
}
test "trunctfsf2" {
// qnan
test__trunctfsf2(@bitCast(@as(u128, 0x7fff800000000000 << 64)), 0x7fc00000);
// nan
test__trunctfsf2(@bitCast(@as(u128, (0x7fff000000000000 | (0x810000000000 & 0xffffffffffff)) << 64)), 0x7fc08000);
// inf
test__trunctfsf2(@bitCast(@as(u128, 0x7fff000000000000 << 64)), 0x7f800000);
// zero
test__trunctfsf2(0.0, 0x0);
test__trunctfsf2(0x1.23a2abb4a2ddee355f36789abcdep+5, 0x4211d156);
test__trunctfsf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9, 0x3b71e9e2);
test__trunctfsf2(0x1.234eebb5faa678f4488693abcdefp+4534, 0x7f800000);
test__trunctfsf2(0x1.edcba9bb8c76a5a43dd21f334634p-435, 0x0);
}
fn test__trunctfdf2(a: f128, expected: u64) void {
const x = __trunctfdf2(a);
const rep: u64 = @bitCast(x);
if (rep == expected) {
return;
}
// test other possible NaN representation(signal NaN)
else if (expected == 0x7ff8000000000000) {
if ((rep & 0x7ff0000000000000) == 0x7ff0000000000000 and (rep & 0xfffffffffffff) > 0) {
return;
}
}
@panic("__trunctfsf2 test failure");
}
test "trunctfdf2" {
// qnan
test__trunctfdf2(@bitCast(@as(u128, 0x7fff800000000000 << 64)), 0x7ff8000000000000);
// nan
test__trunctfdf2(@bitCast(@as(u128, (0x7fff000000000000 | (0x810000000000 & 0xffffffffffff)) << 64)), 0x7ff8100000000000);
// inf
test__trunctfdf2(@bitCast(@as(u128, 0x7fff000000000000 << 64)), 0x7ff0000000000000);
// zero
test__trunctfdf2(0.0, 0x0);
test__trunctfdf2(0x1.af23456789bbaaab347645365cdep+5, 0x404af23456789bbb);
test__trunctfdf2(0x1.dedafcff354b6ae9758763545432p-9, 0x3f6dedafcff354b7);
test__trunctfdf2(0x1.2f34dd5f437e849b4baab754cdefp+4534, 0x7ff0000000000000);
test__trunctfdf2(0x1.edcbff8ad76ab5bf46463233214fp-435, 0x24cedcbff8ad76ab);
}
fn test__truncdfsf2(a: f64, expected: u32) void {
const x = __truncdfsf2(a);
const rep: u32 = @bitCast(x);
if (rep == expected) {
return;
}
// test other possible NaN representation(signal NaN)
else if (expected == 0x7fc00000) {
if ((rep & 0x7f800000) == 0x7f800000 and (rep & 0x7fffff) > 0) {
return;
}
}
std.debug.print("got 0x{x} wanted 0x{x}\n", .{ rep, expected });
@panic("__trunctfsf2 test failure");
}
test "truncdfsf2" {
// nan & qnan
test__truncdfsf2(@bitCast(@as(u64, 0x7ff8000000000000)), 0x7fc00000);
test__truncdfsf2(@bitCast(@as(u64, 0x7ff0000000000001)), 0x7fc00000);
// inf
test__truncdfsf2(@bitCast(@as(u64, 0x7ff0000000000000)), 0x7f800000);
test__truncdfsf2(@bitCast(@as(u64, 0xfff0000000000000)), 0xff800000);
test__truncdfsf2(0.0, 0x0);
test__truncdfsf2(1.0, 0x3f800000);
test__truncdfsf2(-1.0, 0xbf800000);
// huge number becomes inf
test__truncdfsf2(340282366920938463463374607431768211456.0, 0x7f800000);
}
fn test__trunctfhf2(a: f128, expected: u16) void {
const x = __trunctfhf2(a);
const rep: u16 = @bitCast(x);
if (rep == expected) {
return;
}
std.debug.print("got 0x{x} wanted 0x{x}\n", .{ rep, expected });
@panic("__trunctfhf2 test failure");
}
test "trunctfhf2" {
// qNaN
test__trunctfhf2(@bitCast(@as(u128, 0x7fff8000000000000000000000000000)), 0x7e00);
// NaN
test__trunctfhf2(@bitCast(@as(u128, 0x7fff0000000000000000000000000001)), 0x7e00);
// inf
test__trunctfhf2(@bitCast(@as(u128, 0x7fff0000000000000000000000000000)), 0x7c00);
test__trunctfhf2(-@as(f128, @bitCast(@as(u128, 0x7fff0000000000000000000000000000))), 0xfc00);
// zero
test__trunctfhf2(0.0, 0x0);
test__trunctfhf2(-0.0, 0x8000);
test__trunctfhf2(3.1415926535, 0x4248);
test__trunctfhf2(-3.1415926535, 0xc248);
test__trunctfhf2(0x1.987124876876324p+100, 0x7c00);
test__trunctfhf2(0x1.987124876876324p+12, 0x6e62);
test__trunctfhf2(0x1.0p+0, 0x3c00);
test__trunctfhf2(0x1.0p-14, 0x0400);
// denormal
test__trunctfhf2(0x1.0p-20, 0x0010);
test__trunctfhf2(0x1.0p-24, 0x0001);
test__trunctfhf2(-0x1.0p-24, 0x8001);
test__trunctfhf2(0x1.5p-25, 0x0001);
// and back to zero
test__trunctfhf2(0x1.0p-25, 0x0000);
test__trunctfhf2(-0x1.0p-25, 0x8000);
// max (precise)
test__trunctfhf2(65504.0, 0x7bff);
// max (rounded)
test__trunctfhf2(65519.0, 0x7bff);
// max (to +inf)
test__trunctfhf2(65520.0, 0x7c00);
test__trunctfhf2(65536.0, 0x7c00);
test__trunctfhf2(-65520.0, 0xfc00);
test__trunctfhf2(0x1.23a2abb4a2ddee355f36789abcdep+5, 0x508f);
test__trunctfhf2(0x1.e3d3c45bd3abfd98b76a54cc321fp-9, 0x1b8f);
test__trunctfhf2(0x1.234eebb5faa678f4488693abcdefp+453, 0x7c00);
test__trunctfhf2(0x1.edcba9bb8c76a5a43dd21f334634p-43, 0x0);
}
test "trunctfxf2" {
try test__trunctfxf2(1.5, 1.5);
try test__trunctfxf2(2.5, 2.5);
try test__trunctfxf2(-2.5, -2.5);
try test__trunctfxf2(0.0, 0.0);
}
fn test__trunctfxf2(a: f128, expected: f80) !void {
const x = __trunctfxf2(a);
try testing.expect(x == expected);
}
|