From e5df2d5a9261603c27acafe9dd1bfc845bb6b643 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 10 Oct 2025 09:50:07 +0300 Subject: [PATCH] builtin.overflow: add {add,sub,mul}_{int,f32,f64} too, to avoid cgen errors for tests; cleanup cgen --- vlib/builtin/overflow/overflow.v | 48 ++++++++++++++++++++++++++++++++ vlib/v/gen/c/infix.v | 10 +++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/overflow/overflow.v b/vlib/builtin/overflow/overflow.v index 65c394f88..26f0663ed 100644 --- a/vlib/builtin/overflow/overflow.v +++ b/vlib/builtin/overflow/overflow.v @@ -444,3 +444,51 @@ fn mul_u64(x u64, y u64) u64 { } return res } + +///////////////////////////////////////////////////////////////////////////////////////////// +// These are here to prevent cgen errors for `./v -check-overflow vlib/math/vec/vec4_test.v` +// TODO: check for correctness and add tests. Improve markused so it keeps them, when they are used. +@[inline; markused] +fn add_int(x int, y int) int { + return add_i32(x, y) +} + +@[inline; markused] +fn sub_int(x int, y int) int { + return sub_i32(x, y) +} + +@[inline; markused] +fn mul_int(x int, y int) int { + return mul_i32(x, y) +} + +@[inline; markused] +fn add_f32(x f32, y f32) f32 { + return x + y +} + +@[inline; markused] +fn sub_f32(x f32, y f32) f32 { + return x - y +} + +@[inline; markused] +fn mul_f32(x f32, y f32) f32 { + return x * y +} + +@[inline; markused] +fn add_f64(x f64, y f64) f64 { + return x + y +} + +@[inline; markused] +fn sub_f64(x f64, y f64) f64 { + return x - y +} + +@[inline; markused] +fn mul_f64(x f64, y f64) f64 { + return x * y +} diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 191e771a4..62f61b76e 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1256,12 +1256,10 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) { } // do not use promoted_type for overflow detect left_type := g.unwrap_generic(node.left_type) - is_safe_add := node.op == .plus && g.pref.is_check_overflow && left_type.is_int() - && !g.is_builtin_overflow_mod - is_safe_sub := node.op == .minus && g.pref.is_check_overflow && left_type.is_int() - && !g.is_builtin_overflow_mod - is_safe_mul := node.op == .mul && g.pref.is_check_overflow && left_type.is_int() - && !g.is_builtin_overflow_mod + checkoverflow_op := g.pref.is_check_overflow && !g.is_builtin_overflow_mod && left_type.is_int() + is_safe_add := checkoverflow_op && node.op == .plus + is_safe_sub := checkoverflow_op && node.op == .minus + is_safe_mul := checkoverflow_op && node.op == .mul is_safe_div := node.op == .div && g.pref.div_by_zero_is_zero && typ.is_int() is_safe_mod := node.op == .mod && g.pref.div_by_zero_is_zero && typ.is_int() if node.left_type.is_ptr() && node.left.is_auto_deref_var() && !node.right_type.is_pointer() { -- 2.39.5