authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-29 17:20:41-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-29 17:20:41-05:00
logf97b398b654aecdb679a054d9078b92cb70ba3b5
treee4db4340a18e9a116f7e39e48ca9dc990f811575
parentfe4ef7b461bec5370169063ccf889873161f8c46
signaturelock-open Commit is signed but in an unrecognized format.

simplify int/float comparison


1 files changed, 41 insertions(+), 17 deletions(-)

src/ir.cpp+41-17
......@@ -11161,6 +11161,38 @@ void float_read_ieee597(ZigValue *val, uint8_t *buf, bool is_big_endian) {
1116111161 }
1116211162}
1116311163
11164static void value_to_bigfloat(BigFloat *out, ZigValue *val) {
11165 switch (val->type->id) {
11166 case ZigTypeIdInt:
11167 case ZigTypeIdComptimeInt:
11168 bigfloat_init_bigint(out, &val->data.x_bigint);
11169 return;
11170 case ZigTypeIdComptimeFloat:
11171 *out = val->data.x_bigfloat;
11172 return;
11173 case ZigTypeIdFloat: switch (val->type->data.floating.bit_count) {
11174 case 16:
11175 bigfloat_init_16(out, val->data.x_f16);
11176 return;
11177 case 32:
11178 bigfloat_init_32(out, val->data.x_f32);
11179 return;
11180 case 64:
11181 bigfloat_init_64(out, val->data.x_f64);
11182 return;
11183 case 80:
11184 zig_panic("TODO");
11185 case 128:
11186 bigfloat_init_128(out, val->data.x_f128);
11187 return;
11188 default:
11189 zig_unreachable();
11190 }
11191 default:
11192 zig_unreachable();
11193 }
11194}
11195
1116411196static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstGen *instruction, ZigType *other_type,
1116511197 bool explicit_cast)
1116611198{
......@@ -15825,39 +15857,31 @@ never_mind_just_calculate_it_normally:
1582515857
1582615858 // Handle the case where one of the two operands is a fp value and the other
1582715859 // is an integer value
15828 ZigValue **int_val, **float_val;
15829
15860 ZigValue *float_val;
1583015861 if (op1_is_int && op2_is_float) {
15831 int_val = &op1_val;
15832 float_val = &op2_val;
15862 float_val = op2_val;
1583315863 } else if (op1_is_float && op2_is_int) {
15834 int_val = &op2_val;
15835 float_val = &op1_val;
15864 float_val = op1_val;
1583615865 } else {
1583715866 zig_unreachable();
1583815867 }
1583915868
1584015869 // They can never be equal if the fp value has a non-zero decimal part
1584115870 if (op_id == IrBinOpCmpEq || op_id == IrBinOpCmpNotEq) {
15842 if (float_has_fraction(*float_val)) {
15871 if (float_has_fraction(float_val)) {
1584315872 out_val->special = ConstValSpecialStatic;
1584415873 out_val->data.x_bool = op_id == IrBinOpCmpNotEq;
15845
1584615874 return nullptr;
1584715875 }
1584815876 }
1584915877
1585015878 // 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;
15858 }
15879 BigFloat op1_bigfloat;
15880 BigFloat op2_bigfloat;
15881 value_to_bigfloat(&op1_bigfloat, op1_val);
15882 value_to_bigfloat(&op2_bigfloat, op2_val);
1585915883
15860 Cmp cmp_result = bigfloat_cmp(&op1_val->data.x_bigfloat, &op2_val->data.x_bigfloat);
15884 Cmp cmp_result = bigfloat_cmp(&op1_bigfloat, &op2_bigfloat);
1586115885 out_val->special = ConstValSpecialStatic;
1586215886 out_val->data.x_bool = resolve_cmp_op_id(op_id, cmp_result);
1586315887