diff options
| author | Laytan Laats <laytanlaats@hotmail.com> | 2025-02-05 21:17:48 +0100 |
|---|---|---|
| committer | Laytan Laats <laytanlaats@hotmail.com> | 2025-02-05 21:17:48 +0100 |
| commit | b1068c7f2e71552b0d49b89945c7eabce4b5f878 (patch) | |
| tree | c03b7ac770addb0e1a18fd9d4caefdde0d62ebb7 /src/llvm_backend_proc.cpp | |
| parent | b86d2c30b600023ae643c4dcc2dcca10b08c29dc (diff) | |
improve abs() on floats for more correct and faster results
Diffstat (limited to 'src/llvm_backend_proc.cpp')
| -rw-r--r-- | src/llvm_backend_proc.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 7e44a0046..2a6c1414e 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -2174,7 +2174,35 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu case 128: return lb_emit_runtime_call(p, "abs_complex128", args); } GB_PANIC("Unknown complex type"); + } else if (is_type_float(t)) { + Type *t_float; + Type *t_unsigned; + lbValue mask; + switch (type_size_of(t)) { + case 2: + t_float = t_f16; + t_unsigned = t_u16; + mask = lb_const_int(p->module, t_unsigned, 0x7FFF); + break; + case 4: + t_float = t_f32; + t_unsigned = t_u32; + mask = lb_const_int(p->module, t_unsigned, 0x7FFFFFFF); + break; + case 8: + t_float = t_f64; + t_unsigned = t_u64; + mask = lb_const_int(p->module, t_unsigned, 0x7FFFFFFFFFFFFFFF); + break; + default: + GB_PANIC("abs: unhandled float size"); + } + + lbValue as_unsigned = lb_emit_transmute(p, x, t_unsigned); + lbValue abs = lb_emit_arith(p, Token_And, as_unsigned, mask, t_unsigned); + return lb_emit_transmute(p, abs, t_float); } + lbValue zero = lb_const_nil(p->module, t); lbValue cond = lb_emit_comp(p, Token_Lt, x, zero); lbValue neg = lb_emit_unary_arith(p, Token_Sub, x, t); |