authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-20 10:29:24+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-28 11:45:04-07:00
log4411f9c019f8f41baeacd39be01af9f961df4e4e
tree1e811c386b03682fbe32f7ce61214d6fb3027073
parentf8b204bb189125e85a7f14f08c4bab60a462e76e

add behavior tests for f80


6 files changed, 114 insertions(+), 2 deletions(-)

src/stage1/softfloat.hpp+1-1
...@@ -57,7 +57,7 @@ static inline bool zig_f128_isNaN(float128_t *aPtr) {...@@ -57,7 +57,7 @@ static inline bool zig_f128_isNaN(float128_t *aPtr) {
57}57}
5858
59static inline bool zig_extF80_isNaN(extFloat80_t *aPtr) {59static inline bool zig_extF80_isNaN(extFloat80_t *aPtr) {
60 return aPtr->signExp & 0x7FFF && aPtr->signif;60 return (aPtr->signExp & 0x7FFF) == 0x7FFF && aPtr->signif & UINT64_C(0x7FFFFFFFFFFFFFFF);
61}61}
6262
63#endif63#endif
test/behavior/floatop_stage1.zig+81
...@@ -4,6 +4,7 @@ const math = std.math;...@@ -4,6 +4,7 @@ const math = std.math;
4const pi = std.math.pi;4const pi = std.math.pi;
5const e = std.math.e;5const e = std.math.e;
6const Vector = std.meta.Vector;6const Vector = std.meta.Vector;
7const has_f80_rt = @import("builtin").cpu.arch == .x86_64;
78
8const epsilon = 0.000001;9const epsilon = 0.000001;
910
...@@ -27,6 +28,10 @@ fn testSqrt() !void {...@@ -27,6 +28,10 @@ fn testSqrt() !void {
27 var a: f64 = 25;28 var a: f64 = 25;
28 try expect(@sqrt(a) == 5);29 try expect(@sqrt(a) == 5);
29 }30 }
31 if (has_f80_rt) {
32 var a: f80 = 25;
33 try expect(@sqrt(a) == 5);
34 }
30 {35 {
31 const a: comptime_float = 25.0;36 const a: comptime_float = 25.0;
32 try expect(@sqrt(a) == 5.0);37 try expect(@sqrt(a) == 5.0);
...@@ -86,6 +91,10 @@ fn testSin() !void {...@@ -86,6 +91,10 @@ fn testSin() !void {
86 var a: f64 = 0;91 var a: f64 = 0;
87 try expect(@sin(a) == 0);92 try expect(@sin(a) == 0);
88 }93 }
94 // {
95 // var a: f80 = 0;
96 // try expect(@sin(a) == 0);
97 // }
89 {98 {
90 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };99 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
91 var result = @sin(v);100 var result = @sin(v);
...@@ -116,6 +125,10 @@ fn testCos() !void {...@@ -116,6 +125,10 @@ fn testCos() !void {
116 var a: f64 = 0;125 var a: f64 = 0;
117 try expect(@cos(a) == 1);126 try expect(@cos(a) == 1);
118 }127 }
128 // {
129 // var a: f80 = 0;
130 // try expect(@cos(a) == 1);
131 // }
119 {132 {
120 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };133 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
121 var result = @cos(v);134 var result = @cos(v);
...@@ -146,6 +159,10 @@ fn testExp() !void {...@@ -146,6 +159,10 @@ fn testExp() !void {
146 var a: f64 = 0;159 var a: f64 = 0;
147 try expect(@exp(a) == 1);160 try expect(@exp(a) == 1);
148 }161 }
162 // {
163 // var a: f80 = 0;
164 // try expect(@exp(a) == 1);
165 // }
149 {166 {
150 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };167 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
151 var result = @exp(v);168 var result = @exp(v);
...@@ -176,6 +193,10 @@ fn testExp2() !void {...@@ -176,6 +193,10 @@ fn testExp2() !void {
176 var a: f64 = 2;193 var a: f64 = 2;
177 try expect(@exp2(a) == 4);194 try expect(@exp2(a) == 4);
178 }195 }
196 // {
197 // var a: f80 = 2;
198 // try expect(@exp2(a) == 4);
199 // }
179 {200 {
180 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };201 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
181 var result = @exp2(v);202 var result = @exp2(v);
...@@ -208,6 +229,10 @@ fn testLog() !void {...@@ -208,6 +229,10 @@ fn testLog() !void {
208 var a: f64 = e;229 var a: f64 = e;
209 try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));230 try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));
210 }231 }
232 // {
233 // var a: f80 = e;
234 // try expect(@log(a) == 1);
235 // }
211 {236 {
212 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };237 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
213 var result = @log(v);238 var result = @log(v);
...@@ -238,6 +263,10 @@ fn testLog2() !void {...@@ -238,6 +263,10 @@ fn testLog2() !void {
238 var a: f64 = 4;263 var a: f64 = 4;
239 try expect(@log2(a) == 2);264 try expect(@log2(a) == 2);
240 }265 }
266 // {
267 // var a: f80 = 4;
268 // try expect(@log2(a) == 2);
269 // }
241 {270 {
242 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };271 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
243 var result = @log2(v);272 var result = @log2(v);
...@@ -268,6 +297,10 @@ fn testLog10() !void {...@@ -268,6 +297,10 @@ fn testLog10() !void {
268 var a: f64 = 1000;297 var a: f64 = 1000;
269 try expect(@log10(a) == 3);298 try expect(@log10(a) == 3);
270 }299 }
300 // {
301 // var a: f80 = 1000;
302 // try expect(@log10(a) == 3);
303 // }
271 {304 {
272 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };305 var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
273 var result = @log10(v);306 var result = @log10(v);
...@@ -304,6 +337,12 @@ fn testFabs() !void {...@@ -304,6 +337,12 @@ fn testFabs() !void {
304 try expect(@fabs(a) == 2.5);337 try expect(@fabs(a) == 2.5);
305 try expect(@fabs(b) == 2.5);338 try expect(@fabs(b) == 2.5);
306 }339 }
340 // {
341 // var a: f80 = -2.5;
342 // var b: f80 = 2.5;
343 // try expect(@fabs(a) == 2.5);
344 // try expect(@fabs(b) == 2.5);
345 // }
307 {346 {
308 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };347 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
309 var result = @fabs(v);348 var result = @fabs(v);
...@@ -334,6 +373,10 @@ fn testFloor() !void {...@@ -334,6 +373,10 @@ fn testFloor() !void {
334 var a: f64 = 3.5;373 var a: f64 = 3.5;
335 try expect(@floor(a) == 3);374 try expect(@floor(a) == 3);
336 }375 }
376 // {
377 // var a: f80 = 3.5;
378 // try expect(@floor(a) == 3);
379 // }
337 {380 {
338 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };381 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
339 var result = @floor(v);382 var result = @floor(v);
...@@ -364,6 +407,10 @@ fn testCeil() !void {...@@ -364,6 +407,10 @@ fn testCeil() !void {
364 var a: f64 = 3.5;407 var a: f64 = 3.5;
365 try expect(@ceil(a) == 4);408 try expect(@ceil(a) == 4);
366 }409 }
410 // {
411 // var a: f80 = 3.5;
412 // try expect(@ceil(a) == 4);
413 // }
367 {414 {
368 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };415 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
369 var result = @ceil(v);416 var result = @ceil(v);
...@@ -394,6 +441,10 @@ fn testTrunc() !void {...@@ -394,6 +441,10 @@ fn testTrunc() !void {
394 var a: f64 = -3.5;441 var a: f64 = -3.5;
395 try expect(@trunc(a) == -3);442 try expect(@trunc(a) == -3);
396 }443 }
444 // {
445 // var a: f80 = -3.5;
446 // try expect(@trunc(a) == -3);
447 // }
397 {448 {
398 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };449 var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
399 var result = @trunc(v);450 var result = @trunc(v);
...@@ -403,3 +454,33 @@ fn testTrunc() !void {...@@ -403,3 +454,33 @@ fn testTrunc() !void {
403 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));454 try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon));
404 }455 }
405}456}
457
458test "floating point comparisons" {
459 if (has_f80_rt) try testFloatComparisons();
460 comptime try testFloatComparisons();
461}
462
463fn testFloatComparisons() !void {
464 inline for ([_]type{ f16, f32, f64, f80, f128 }) |ty| {
465 // No decimal part
466 {
467 const x: ty = 1.0;
468 try expect(x == 1);
469 try expect(x != 0);
470 try expect(x > 0);
471 try expect(x < 2);
472 try expect(x >= 1);
473 try expect(x <= 1);
474 }
475 // Non-zero decimal part
476 {
477 const x: ty = 1.5;
478 try expect(x != 1);
479 try expect(x != 2);
480 try expect(x > 1);
481 try expect(x < 2);
482 try expect(x >= 1);
483 try expect(x <= 2);
484 }
485 }
486}
test/behavior/math_stage1.zig+19
...@@ -5,6 +5,7 @@ const expectEqualSlices = std.testing.expectEqualSlices;...@@ -5,6 +5,7 @@ const expectEqualSlices = std.testing.expectEqualSlices;
5const maxInt = std.math.maxInt;5const maxInt = std.math.maxInt;
6const minInt = std.math.minInt;6const minInt = std.math.minInt;
7const mem = std.mem;7const mem = std.mem;
8const has_f80_rt = @import("builtin").cpu.arch == .x86_64;
89
9test "allow signed integer division/remainder when values are comptime known and positive or exact" {10test "allow signed integer division/remainder when values are comptime known and positive or exact" {
10 try expect(5 / 3 == 1);11 try expect(5 / 3 == 1);
...@@ -194,6 +195,8 @@ fn testSqrt(comptime T: type, x: T) !void {...@@ -194,6 +195,8 @@ fn testSqrt(comptime T: type, x: T) !void {
194test "@fabs" {195test "@fabs" {
195 try testFabs(f128, 12.0);196 try testFabs(f128, 12.0);
196 comptime try testFabs(f128, 12.0);197 comptime try testFabs(f128, 12.0);
198 if (has_f80_rt) try testFabs(f80, 12.0);
199 // comptime try testFabs(f80, 12.0);
197 try testFabs(f64, 12.0);200 try testFabs(f64, 12.0);
198 comptime try testFabs(f64, 12.0);201 comptime try testFabs(f64, 12.0);
199 try testFabs(f32, 12.0);202 try testFabs(f32, 12.0);
...@@ -217,6 +220,8 @@ test "@floor" {...@@ -217,6 +220,8 @@ test "@floor" {
217 // FIXME: Generates a floorl function call220 // FIXME: Generates a floorl function call
218 // testFloor(f128, 12.0);221 // testFloor(f128, 12.0);
219 comptime try testFloor(f128, 12.0);222 comptime try testFloor(f128, 12.0);
223 // try testFloor(f80, 12.0);
224 comptime try testFloor(f80, 12.0);
220 try testFloor(f64, 12.0);225 try testFloor(f64, 12.0);
221 comptime try testFloor(f64, 12.0);226 comptime try testFloor(f64, 12.0);
222 try testFloor(f32, 12.0);227 try testFloor(f32, 12.0);
...@@ -240,6 +245,8 @@ test "@ceil" {...@@ -240,6 +245,8 @@ test "@ceil" {
240 // FIXME: Generates a ceill function call245 // FIXME: Generates a ceill function call
241 //testCeil(f128, 12.0);246 //testCeil(f128, 12.0);
242 comptime try testCeil(f128, 12.0);247 comptime try testCeil(f128, 12.0);
248 // try testCeil(f80, 12.0);
249 comptime try testCeil(f80, 12.0);
243 try testCeil(f64, 12.0);250 try testCeil(f64, 12.0);
244 comptime try testCeil(f64, 12.0);251 comptime try testCeil(f64, 12.0);
245 try testCeil(f32, 12.0);252 try testCeil(f32, 12.0);
...@@ -263,6 +270,14 @@ test "@trunc" {...@@ -263,6 +270,14 @@ test "@trunc" {
263 // FIXME: Generates a truncl function call270 // FIXME: Generates a truncl function call
264 //testTrunc(f128, 12.0);271 //testTrunc(f128, 12.0);
265 comptime try testTrunc(f128, 12.0);272 comptime try testTrunc(f128, 12.0);
273 // try testTrunc(f80, 12.0);
274 // comptime try testTrunc(f80, 12.0);
275 comptime {
276 const x: f80 = 12.0;
277 const y = x + 0.8;
278 const z = @trunc(y);
279 try expectEqual(x, z);
280 }
266 try testTrunc(f64, 12.0);281 try testTrunc(f64, 12.0);
267 comptime try testTrunc(f64, 12.0);282 comptime try testTrunc(f64, 12.0);
268 try testTrunc(f32, 12.0);283 try testTrunc(f32, 12.0);
...@@ -294,6 +309,8 @@ test "@round" {...@@ -294,6 +309,8 @@ test "@round" {
294 // FIXME: Generates a roundl function call309 // FIXME: Generates a roundl function call
295 //testRound(f128, 12.0);310 //testRound(f128, 12.0);
296 comptime try testRound(f128, 12.0);311 comptime try testRound(f128, 12.0);
312 // try testRound(f80, 12.0);
313 comptime try testRound(f80, 12.0);
297 try testRound(f64, 12.0);314 try testRound(f64, 12.0);
298 comptime try testRound(f64, 12.0);315 comptime try testRound(f64, 12.0);
299 try testRound(f32, 12.0);316 try testRound(f32, 12.0);
...@@ -333,10 +350,12 @@ test "NaN comparison" {...@@ -333,10 +350,12 @@ test "NaN comparison" {
333 try testNanEqNan(f32);350 try testNanEqNan(f32);
334 try testNanEqNan(f64);351 try testNanEqNan(f64);
335 try testNanEqNan(f128);352 try testNanEqNan(f128);
353 if (has_f80_rt) try testNanEqNan(f80);
336 comptime try testNanEqNan(f16);354 comptime try testNanEqNan(f16);
337 comptime try testNanEqNan(f32);355 comptime try testNanEqNan(f32);
338 comptime try testNanEqNan(f64);356 comptime try testNanEqNan(f64);
339 comptime try testNanEqNan(f128);357 comptime try testNanEqNan(f128);
358 // comptime try testNanEqNan(f80);
340}359}
341360
342fn testNanEqNan(comptime F: type) !void {361fn testNanEqNan(comptime F: type) !void {
test/behavior/muladd.zig+6
...@@ -25,6 +25,12 @@ fn testMulAdd() !void {...@@ -25,6 +25,12 @@ fn testMulAdd() !void {
25 var c: f64 = 6.25;25 var c: f64 = 6.25;
26 try expect(@mulAdd(f64, a, b, c) == 20);26 try expect(@mulAdd(f64, a, b, c) == 20);
27 }27 }
28 // {
29 // var a: f16 = 5.5;
30 // var b: f80 = 2.5;
31 // var c: f80 = 6.25;
32 // try expect(@mulAdd(f80, a, b, c) == 20);
33 // }
28 if (builtin.os.tag == .macos and builtin.cpu.arch == .aarch64) {34 if (builtin.os.tag == .macos and builtin.cpu.arch == .aarch64) {
29 // https://github.com/ziglang/zig/issues/990035 // https://github.com/ziglang/zig/issues/9900
30 return error.SkipZigTest;36 return error.SkipZigTest;
test/behavior/type_stage1.zig+2-1
...@@ -13,8 +13,9 @@ test "Type.Float" {...@@ -13,8 +13,9 @@ test "Type.Float" {
13 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));13 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
14 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));14 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
15 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));15 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
16 try testing.expect(f80 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 80 } }));
16 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));17 try testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
17 try testTypes(&[_]type{ f16, f32, f64, f128 });18 try testTypes(&[_]type{ f16, f32, f64, f80, f128 });
18}19}
1920
20test "Type.Array" {21test "Type.Array" {
test/behavior/widening.zig+5
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const mem = std.mem;3const mem = std.mem;
4const builtin = @import("builtin");4const builtin = @import("builtin");
5const has_f80_rt = @import("builtin").cpu.arch == .x86_64;
56
6test "integer widening" {7test "integer widening" {
7 var a: u8 = 250;8 var a: u8 = 250;
...@@ -27,6 +28,10 @@ test "float widening" {...@@ -27,6 +28,10 @@ test "float widening" {
27 try expect(a == b);28 try expect(a == b);
28 try expect(b == c);29 try expect(b == c);
29 try expect(c == d);30 try expect(c == d);
31 if (has_f80_rt) {
32 var e: f80 = c;
33 try expect(c == e);
34 }
30}35}
3136
32test "float widening f16 to f128" {37test "float widening f16 to f128" {