authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-21 10:43:05+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-29 16:37:55-05:00
logfe4ef7b461bec5370169063ccf889873161f8c46
tree62e89820a4f2161cbc65d199b74609bd8a3e933b
parentd448c3d38af9f8f70daaa2817a5834e30da4b8ca
signature Commit is signed but in an unrecognized format.

Fix comptime float-int comparisons

Closes #4259

2 files changed, 81 insertions(+), 32 deletions(-)

src/ir.cpp+39-20
......@@ -15815,33 +15815,52 @@ never_mind_just_calculate_it_normally:
1581515815 bool op1_is_int = op1_val->type->id == ZigTypeIdInt || op1_val->type->id == ZigTypeIdComptimeInt;
1581615816 bool op2_is_int = op2_val->type->id == ZigTypeIdInt || op2_val->type->id == ZigTypeIdComptimeInt;
1581715817
15818 BigInt *op1_bigint;
15819 BigInt *op2_bigint;
15820 bool need_to_free_op1_bigint = false;
15821 bool need_to_free_op2_bigint = false;
15822 if (op1_is_float) {
15823 op1_bigint = allocate<BigInt>(1, "BigInt");
15824 need_to_free_op1_bigint = true;
15825 float_init_bigint(op1_bigint, op1_val);
15826 } else {
15827 assert(op1_is_int);
15828 op1_bigint = &op1_val->data.x_bigint;
15818 if (op1_is_int && op2_is_int) {
15819 Cmp cmp_result = bigint_cmp(&op1_val->data.x_bigint, &op2_val->data.x_bigint);
15820 out_val->special = ConstValSpecialStatic;
15821 out_val->data.x_bool = resolve_cmp_op_id(op_id, cmp_result);
15822
15823 return nullptr;
1582915824 }
15830 if (op2_is_float) {
15831 op2_bigint = allocate<BigInt>(1, "BigInt");
15832 need_to_free_op2_bigint = true;
15833 float_init_bigint(op2_bigint, op2_val);
15825
15826 // Handle the case where one of the two operands is a fp value and the other
15827 // is an integer value
15828 ZigValue **int_val, **float_val;
15829
15830 if (op1_is_int && op2_is_float) {
15831 int_val = &op1_val;
15832 float_val = &op2_val;
15833 } else if (op1_is_float && op2_is_int) {
15834 int_val = &op2_val;
15835 float_val = &op1_val;
1583415836 } else {
15835 assert(op2_is_int);
15836 op2_bigint = &op2_val->data.x_bigint;
15837 zig_unreachable();
15838 }
15839
15840 // They can never be equal if the fp value has a non-zero decimal part
15841 if (op_id == IrBinOpCmpEq || op_id == IrBinOpCmpNotEq) {
15842 if (float_has_fraction(*float_val)) {
15843 out_val->special = ConstValSpecialStatic;
15844 out_val->data.x_bool = op_id == IrBinOpCmpNotEq;
15845
15846 return nullptr;
15847 }
15848 }
15849
15850 // Cast the integer operand into a fp value to perform the comparison
15851 {
15852 IrInstruction *tmp = ir_const_noval(ira, source_instr);
15853 tmp->value = *int_val;
15854 IrInstruction *casted = ir_implicit_cast(ira, tmp, (*float_val)->type);
15855 if (casted == ira->codegen->invalid_instruction)
15856 return ira->codegen->trace_err;
15857 *int_val = casted->value;
1583715858 }
1583815859
15839 Cmp cmp_result = bigint_cmp(op1_bigint, op2_bigint);
15860 Cmp cmp_result = bigfloat_cmp(&op1_val->data.x_bigfloat, &op2_val->data.x_bigfloat);
1584015861 out_val->special = ConstValSpecialStatic;
1584115862 out_val->data.x_bool = resolve_cmp_op_id(op_id, cmp_result);
1584215863
15843 if (need_to_free_op1_bigint) destroy(op1_bigint, "BigInt");
15844 if (need_to_free_op2_bigint) destroy(op2_bigint, "BigInt");
1584515864 return nullptr;
1584615865}
1584715866
test/stage1/behavior/floatop.zig+42-12
......@@ -36,7 +36,7 @@ fn testSqrt() void {
3636 // expect(@sqrt(a) == 7);
3737 //}
3838 {
39 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 3.3, 4.4};
39 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
4040 var result = @sqrt(v);
4141 expect(math.approxEq(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon));
4242 expect(math.approxEq(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon));
......@@ -86,7 +86,7 @@ fn testSin() void {
8686 expect(@sin(a) == 0);
8787 }
8888 {
89 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 3.3, 4.4};
89 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
9090 var result = @sin(v);
9191 expect(math.approxEq(f32, @sin(@as(f32, 1.1)), result[0], epsilon));
9292 expect(math.approxEq(f32, @sin(@as(f32, 2.2)), result[1], epsilon));
......@@ -116,7 +116,7 @@ fn testCos() void {
116116 expect(@cos(a) == 1);
117117 }
118118 {
119 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 3.3, 4.4};
119 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 };
120120 var result = @cos(v);
121121 expect(math.approxEq(f32, @cos(@as(f32, 1.1)), result[0], epsilon));
122122 expect(math.approxEq(f32, @cos(@as(f32, 2.2)), result[1], epsilon));
......@@ -146,7 +146,7 @@ fn testExp() void {
146146 expect(@exp(a) == 1);
147147 }
148148 {
149 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 0.3, 0.4};
149 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
150150 var result = @exp(v);
151151 expect(math.approxEq(f32, @exp(@as(f32, 1.1)), result[0], epsilon));
152152 expect(math.approxEq(f32, @exp(@as(f32, 2.2)), result[1], epsilon));
......@@ -176,7 +176,7 @@ fn testExp2() void {
176176 expect(@exp2(a) == 4);
177177 }
178178 {
179 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 0.3, 0.4};
179 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
180180 var result = @exp2(v);
181181 expect(math.approxEq(f32, @exp2(@as(f32, 1.1)), result[0], epsilon));
182182 expect(math.approxEq(f32, @exp2(@as(f32, 2.2)), result[1], epsilon));
......@@ -208,7 +208,7 @@ fn testLog() void {
208208 expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000)));
209209 }
210210 {
211 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 0.3, 0.4};
211 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
212212 var result = @log(v);
213213 expect(math.approxEq(f32, @log(@as(f32, 1.1)), result[0], epsilon));
214214 expect(math.approxEq(f32, @log(@as(f32, 2.2)), result[1], epsilon));
......@@ -238,7 +238,7 @@ fn testLog2() void {
238238 expect(@log2(a) == 2);
239239 }
240240 {
241 var v: @Vector(4, f32) = [_]f32{1.1, 2.2, 0.3, 0.4};
241 var v: @Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 };
242242 var result = @log2(v);
243243 expect(math.approxEq(f32, @log2(@as(f32, 1.1)), result[0], epsilon));
244244 expect(math.approxEq(f32, @log2(@as(f32, 2.2)), result[1], epsilon));
......@@ -268,7 +268,7 @@ fn testLog10() void {
268268 expect(@log10(a) == 3);
269269 }
270270 {
271 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 };
272272 var result = @log10(v);
273273 expect(math.approxEq(f32, @log10(@as(f32, 1.1)), result[0], epsilon));
274274 expect(math.approxEq(f32, @log10(@as(f32, 2.2)), result[1], epsilon));
......@@ -304,7 +304,7 @@ fn testFabs() void {
304304 expect(@fabs(b) == 2.5);
305305 }
306306 {
307 var v: @Vector(4, f32) = [_]f32{1.1, -2.2, 0.3, -0.4};
307 var v: @Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
308308 var result = @fabs(v);
309309 expect(math.approxEq(f32, @fabs(@as(f32, 1.1)), result[0], epsilon));
310310 expect(math.approxEq(f32, @fabs(@as(f32, -2.2)), result[1], epsilon));
......@@ -334,7 +334,7 @@ fn testFloor() void {
334334 expect(@floor(a) == 3);
335335 }
336336 {
337 var v: @Vector(4, f32) = [_]f32{1.1, -2.2, 0.3, -0.4};
337 var v: @Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
338338 var result = @floor(v);
339339 expect(math.approxEq(f32, @floor(@as(f32, 1.1)), result[0], epsilon));
340340 expect(math.approxEq(f32, @floor(@as(f32, -2.2)), result[1], epsilon));
......@@ -364,7 +364,7 @@ fn testCeil() void {
364364 expect(@ceil(a) == 4);
365365 }
366366 {
367 var v: @Vector(4, f32) = [_]f32{1.1, -2.2, 0.3, -0.4};
367 var v: @Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
368368 var result = @ceil(v);
369369 expect(math.approxEq(f32, @ceil(@as(f32, 1.1)), result[0], epsilon));
370370 expect(math.approxEq(f32, @ceil(@as(f32, -2.2)), result[1], epsilon));
......@@ -394,7 +394,7 @@ fn testTrunc() void {
394394 expect(@trunc(a) == -3);
395395 }
396396 {
397 var v: @Vector(4, f32) = [_]f32{1.1, -2.2, 0.3, -0.4};
397 var v: @Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 };
398398 var result = @trunc(v);
399399 expect(math.approxEq(f32, @trunc(@as(f32, 1.1)), result[0], epsilon));
400400 expect(math.approxEq(f32, @trunc(@as(f32, -2.2)), result[1], epsilon));
......@@ -403,6 +403,36 @@ fn testTrunc() void {
403403 }
404404}
405405
406test "floating point comparisons" {
407 testFloatComparisons();
408 comptime testFloatComparisons();
409}
410
411fn testFloatComparisons() void {
412 inline for ([_]type{ f16, f32, f64, f128 }) |ty| {
413 // No decimal part
414 {
415 const x: ty = 1.0;
416 expect(x == 1);
417 expect(x != 0);
418 expect(x > 0);
419 expect(x < 2);
420 expect(x >= 1);
421 expect(x <= 1);
422 }
423 // Non-zero decimal part
424 {
425 const x: ty = 1.5;
426 expect(x != 1);
427 expect(x != 2);
428 expect(x > 1);
429 expect(x < 2);
430 expect(x >= 1);
431 expect(x <= 2);
432 }
433 }
434}
435
406436// TODO This is waiting on library support for the Windows build (not sure why the other's don't need it)
407437//test "@nearbyint" {
408438// comptime testNearbyInt();