commit 3fadb3583ea8037e97b70593b3863670505c86e9 (tree)
parent b5376050092bf63c870e8518db73717d0d183eba
Author: Motiejus <motiejus@jakstys.lt>
Date: Sat, 7 Mar 2026 09:12:17 +0000
sema: fix comptime_float handling in zirNegate, zirNegateWrap, zirAbs
zirNegate and zirNegateWrap: extend the float detection to include
IP_INDEX_COMPTIME_FLOAT_TYPE. Previously floatBits() returned 0 for
comptime_float, causing negate to fall through to integer path.
This fixes -(comptime_float) and -(comptime_float) wrap.
zirAbs: similarly extend float check to include comptime_float so
@abs on comptime_float values is properly folded at comptime.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/stage0/sema.c b/stage0/sema.c
@@ -980,7 +980,8 @@ static AirInstRef zirNegate(Sema* sema, SemaBlock* block, uint32_t inst) {
// Float negation: fold at comptime if known, else emit AIR_INST_NEG.
// Ported from src/Sema.zig zirNegate float branch (arith.negateFloat).
- if (floatBits(ty) > 0) {
+ // Handles both concrete float types and comptime_float.
+ if (floatBits(ty) > 0 || ty == IP_INDEX_COMPTIME_FLOAT_TYPE) {
if (AIR_REF_IS_IP(operand)) {
InternPoolKey src_key
= ipIndexToKey(sema->ip, AIR_REF_TO_IP(operand));
@@ -1024,7 +1025,7 @@ static AirInstRef zirNegateWrap(Sema* sema, SemaBlock* block, uint32_t inst) {
memset(&key, 0, sizeof(key));
// For floats: create float(0.0) zero; for ints: create int(0) zero.
// Ported from src/Sema.zig zirNegateWrap: pt.intValue(rhs_scalar_ty, 0).
- if (floatBits(ty) > 0) {
+ if (floatBits(ty) > 0 || ty == IP_INDEX_COMPTIME_FLOAT_TYPE) {
key.tag = IP_KEY_FLOAT;
key.data.float_val.ty = ty;
key.data.float_val.val = 0.0;
@@ -1319,7 +1320,11 @@ static AirInstRef zirAbs(Sema* sema, SemaBlock* block, uint32_t inst) {
// Works for both comptime_int and typed signed ints.
return internComptimeInt(sema, result_ty, ct_lo, ct_hi, false);
}
- if (AIR_REF_IS_IP(operand) && floatBits(operand_ty) > 0) {
+ // Comptime float folding: concrete float types and comptime_float.
+ // Ported from Sema.zig maybeConstantUnaryMath(Value.abs) float path.
+ if (AIR_REF_IS_IP(operand)
+ && (floatBits(operand_ty) > 0
+ || operand_ty == IP_INDEX_COMPTIME_FLOAT_TYPE)) {
InternPoolKey src_key = ipIndexToKey(sema->ip, AIR_REF_TO_IP(operand));
if (src_key.tag == IP_KEY_FLOAT) {
double val = src_key.data.float_val.val;