From caecbebd0f3fc0dc08f9ee2f898460e2e9c739d9 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 16 Jul 2021 15:21:56 +0200 Subject: [PATCH] Update float conversion into LLVM. --- src/compiler/llvm_codegen_expr.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 752871aa2..120907b67 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -3,6 +3,7 @@ // a copy of which can be found in the LICENSE file. #include "llvm_codegen_internal.h" +#include static void gencontext_emit_unary_expr(GenContext *context, BEValue *value, Expr *expr); static inline void llvm_emit_post_inc_dec(GenContext *c, BEValue *value, Expr *expr, int diff, bool use_mod); @@ -2178,7 +2179,20 @@ void gencontext_emit_ternary_expr(GenContext *c, BEValue *value, Expr *expr) llvm_value_set(value, phi, expr->type); } - +static LLVMValueRef llvm_emit_real(LLVMTypeRef type, long double f) +{ + if (isnan(f)) + { + return LLVMConstReal(type, nan("")); + } + if (isinf(f)) + { + return LLVMConstReal(type, f < 0 ? -INFINITY : INFINITY); + } + scratch_buffer_clear(); + global_context.scratch_buffer_len = sprintf(global_context.scratch_buffer, "%La", f); + return LLVMConstRealOfStringAndSize(type, global_context.scratch_buffer, global_context.scratch_buffer_len); +} static void llvm_emit_const_expr(GenContext *c, BEValue *be_value, Expr *expr) { @@ -2196,7 +2210,7 @@ static void llvm_emit_const_expr(GenContext *c, BEValue *be_value, Expr *expr) } return; case ALL_FLOATS: - llvm_value_set(be_value, LLVMConstReal(llvm_get_type(c, type), (double) expr->const_expr.f), type); + llvm_value_set(be_value, llvm_emit_real(llvm_get_type(c, type), expr->const_expr.f), type); return; case TYPE_POINTER: llvm_value_set(be_value, LLVMConstNull(llvm_get_type(c, type)), type);