| author | |
| committer | |
| log | cc8e49283d68075c4e842405d651d0d36b8d10af |
| tree | 9c53039e654cc99385f5886026b58dc468e02a4c |
| parent | 41efdc73b9a57dfcc57474310c64724f58a95493 |
Closes #7951
Closes #85744 files changed, 20 insertions(+), 10 deletions(-)
src/stage1/bigint.cpp+2-2| ... | ... | @@ -1446,10 +1446,10 @@ void bigint_negate(BigInt *dest, const BigInt *op) { |
| 1446 | 1446 | bigint_normalize(dest); |
| 1447 | 1447 | } |
| 1448 | 1448 | |
| 1449 | void bigint_negate_wrap(BigInt *dest, const BigInt *op, size_t bit_count) { | |
| 1449 | void bigint_negate_wrap(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed) { | |
| 1450 | 1450 | BigInt zero; |
| 1451 | 1451 | bigint_init_unsigned(&zero, 0); |
| 1452 | bigint_sub_wrap(dest, &zero, op, bit_count, true); | |
| 1452 | bigint_sub_wrap(dest, &zero, op, bit_count, is_signed); | |
| 1453 | 1453 | } |
| 1454 | 1454 | |
| 1455 | 1455 | void bigint_not(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed) { |
src/stage1/bigint.hpp+1-1| ... | ... | @@ -75,7 +75,7 @@ void bigint_shl_trunc(BigInt *dest, const BigInt *op1, const BigInt *op2, size_t |
| 75 | 75 | void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2); |
| 76 | 76 | |
| 77 | 77 | void bigint_negate(BigInt *dest, const BigInt *op); |
| 78 | void bigint_negate_wrap(BigInt *dest, const BigInt *op, size_t bit_count); | |
| 78 | void bigint_negate_wrap(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed); | |
| 79 | 79 | void bigint_not(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed); |
| 80 | 80 | void bigint_truncate(BigInt *dest, const BigInt *op, size_t bit_count, bool is_signed); |
| 81 | 81 |
src/stage1/ir.cpp+3-3| ... | ... | @@ -21660,8 +21660,8 @@ static ErrorMsg *ir_eval_negation_scalar(IrAnalyze *ira, IrInst* source_instr, Z |
| 21660 | 21660 | { |
| 21661 | 21661 | bool is_float = (scalar_type->id == ZigTypeIdFloat || scalar_type->id == ZigTypeIdComptimeFloat); |
| 21662 | 21662 | |
| 21663 | bool ok_type = ((scalar_type->id == ZigTypeIdInt && scalar_type->data.integral.is_signed) || | |
| 21664 | scalar_type->id == ZigTypeIdComptimeInt || (is_float && !is_wrap_op)); | |
| 21663 | bool ok_type = scalar_type->id == ZigTypeIdInt || scalar_type->id == ZigTypeIdComptimeInt || | |
| 21664 | (is_float && !is_wrap_op); | |
| 21665 | 21665 | |
| 21666 | 21666 | if (!ok_type) { |
| 21667 | 21667 | const char *fmt = is_wrap_op ? "invalid wrapping negation type: '%s'" : "invalid negation type: '%s'"; |
| ... | ... | @@ -21672,7 +21672,7 @@ static ErrorMsg *ir_eval_negation_scalar(IrAnalyze *ira, IrInst* source_instr, Z |
| 21672 | 21672 | float_negate(scalar_out_val, operand_val); |
| 21673 | 21673 | } else if (is_wrap_op) { |
| 21674 | 21674 | bigint_negate_wrap(&scalar_out_val->data.x_bigint, &operand_val->data.x_bigint, |
| 21675 | scalar_type->data.integral.bit_count); | |
| 21675 | scalar_type->data.integral.bit_count, scalar_type->data.integral.is_signed); | |
| 21676 | 21676 | } else { |
| 21677 | 21677 | bigint_negate(&scalar_out_val->data.x_bigint, &operand_val->data.x_bigint); |
| 21678 | 21678 | } |
test/stage1/behavior/math.zig+14-4| ... | ... | @@ -229,16 +229,26 @@ fn testSignedWrappingEval(x: i32) void { |
| 229 | 229 | expect(max_val == maxInt(i32)); |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | test "negation wrapping" { | |
| 233 | testNegationWrappingEval(minInt(i16)); | |
| 234 | comptime testNegationWrappingEval(minInt(i16)); | |
| 232 | test "signed negation wrapping" { | |
| 233 | testSignedNegationWrappingEval(minInt(i16)); | |
| 234 | comptime testSignedNegationWrappingEval(minInt(i16)); | |
| 235 | 235 | } |
| 236 | fn testNegationWrappingEval(x: i16) void { | |
| 236 | fn testSignedNegationWrappingEval(x: i16) void { | |
| 237 | 237 | expect(x == -32768); |
| 238 | 238 | const neg = -%x; |
| 239 | 239 | expect(neg == -32768); |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | test "unsigned negation wrapping" { | |
| 243 | testUnsignedNegationWrappingEval(1); | |
| 244 | comptime testUnsignedNegationWrappingEval(1); | |
| 245 | } | |
| 246 | fn testUnsignedNegationWrappingEval(x: u16) void { | |
| 247 | expect(x == 1); | |
| 248 | const neg = -%x; | |
| 249 | expect(neg == maxInt(u16)); | |
| 250 | } | |
| 251 | ||
| 242 | 252 | test "unsigned 64-bit division" { |
| 243 | 253 | test_u64_div(); |
| 244 | 254 | comptime test_u64_div(); |