From c0cd25ba76d23fc6b1ce04302994a4b4bc89c168 Mon Sep 17 00:00:00 2001 From: Mihail Moskov <28909106+corleypc@users.noreply.github.com> Date: Wed, 9 Sep 2026 17:13:20 +0300 Subject: [PATCH] simd_abs optimization + fix for unsigned --- src/llvm_backend_proc.cpp | 19 +++++++++++-------- tests/internal/test_abs.odin | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index f7e4ebe69..c662a0a95 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1816,15 +1816,18 @@ gb_internal lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAn return res; case BuiltinProc_simd_abs: if (is_float) { - LLVMValueRef pos = arg0.value; - LLVMValueRef neg = LLVMBuildFNeg(p->builder, pos, ""); - LLVMValueRef cond = LLVMBuildFCmp(p->builder, LLVMRealOGT, pos, neg, ""); - res.value = LLVMBuildSelect(p->builder, cond, pos, neg, ""); + LLVMTypeRef types[1] = {LLVMTypeOf(arg0.value)}; + LLVMValueRef args[1] = {arg0.value}; + res.value = lb_call_intrinsic(p, "llvm.fabs", args, gb_count_of(args), types, gb_count_of(types)); + } else if (is_signed) { + LLVMTypeRef types[1] = {LLVMTypeOf(arg0.value)}; + // is_int_min_poison=false, so abs(min(T)) = min(T) and not poison + LLVMValueRef is_int_min_poison = lb_const_bool(p->module, t_llvm_bool, false).value; + LLVMValueRef args[2] = {arg0.value, is_int_min_poison}; + res.value = lb_call_intrinsic(p, "llvm.abs", args, gb_count_of(args), types, gb_count_of(types)); } else { - LLVMValueRef pos = arg0.value; - LLVMValueRef neg = LLVMBuildNeg(p->builder, pos, ""); - LLVMValueRef cond = LLVMBuildICmp(p->builder, is_signed ? LLVMIntSGT : LLVMIntUGT, pos, neg, ""); - res.value = LLVMBuildSelect(p->builder, cond, pos, neg, ""); + // unsigned integers -> |x| = x + res.value = arg0.value; } return res; case BuiltinProc_simd_min: diff --git a/tests/internal/test_abs.odin b/tests/internal/test_abs.odin index 56db10c09..347ab5ac0 100644 --- a/tests/internal/test_abs.odin +++ b/tests/internal/test_abs.odin @@ -1,5 +1,6 @@ package test_internal +import "base:intrinsics" import "core:testing" @(private="file") @@ -166,3 +167,26 @@ abs_f64_variable :: proc(t: ^testing.T) { testing.expect_value(t, abs(not_const(max(f64be))), max(f64be)) testing.expect_value(t, abs(not_const(f64be(-.12345))), .12345) } + +@(test) +simd_abs_variable :: proc(t: ^testing.T) { + // signed ints -> abs(min(T)) wraps to min(T) + i32x4 := intrinsics.simd_abs(not_const(#simd[4]i32{min(i32), -1, 0, max(i32)})) + testing.expect_value(t, transmute([4]i32)i32x4, [4]i32{min(i32), 1, 0, max(i32)}) + i8x16 := intrinsics.simd_abs(not_const(#simd[16]i8{min(i8), -1, 0, max(i8), -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7})) + testing.expect_value(t, transmute([16]i8)i8x16, [16]i8{min(i8), 1, 0, max(i8), 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}) + + // unsigned ints + u32x4 := intrinsics.simd_abs(not_const(#simd[4]u32{0, 1, 100, max(u32)})) + testing.expect_value(t, transmute([4]u32)u32x4, [4]u32{0, 1, 100, max(u32)}) + u8x16 := intrinsics.simd_abs(not_const(#simd[16]u8{0, 1, 100, 200, 255, 128, 127, 2, 3, 4, 5, 6, 7, 8, 9, 10})) + testing.expect_value(t, transmute([16]u8)u8x16, [16]u8{0, 1, 100, 200, 255, 128, 127, 2, 3, 4, 5, 6, 7, 8, 9, 10}) + + // floats + f32x4 := intrinsics.simd_abs(not_const(#simd[4]f32{0., -0., -1., -.12345})) + f32_expected := [4]f32{0., 0., 1., .12345} + testing.expect_value(t, transmute([4]u32)f32x4, transmute([4]u32)f32_expected) + f64x2 := intrinsics.simd_abs(not_const(#simd[2]f64{-0., min(f64)})) + f64_expected := [2]f64{0., max(f64)} + testing.expect_value(t, transmute([2]u64)f64x2, transmute([2]u64)f64_expected) +}