From e28c5496ebba30194cf110a5157e62e57af34406 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Fri, 6 Aug 2010 01:18:01 +0000 Subject: [PATCH] Emulate C99 fenv.h on amd64 through the MXCSR. --- src/microcode/cmpauxmd/x86-64.m4 | 14 ++++++++++++++ src/microcode/cmpintmd/x86-64.h | 33 +++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/microcode/cmpauxmd/x86-64.m4 b/src/microcode/cmpauxmd/x86-64.m4 index fedb3e2ea..ff2238c57 100644 --- a/src/microcode/cmpauxmd/x86-64.m4 +++ b/src/microcode/cmpauxmd/x86-64.m4 @@ -966,6 +966,20 @@ asm_fixnum_rsh_overflow_negative: OP(mov,q) TW(IMM_DETAGGED_FIXNUM_MINUS_ONE,REG(rax)) ret +define_c_label(x86_64_read_mxcsr) + enter IMM(8),IMM(0) + stmxcsr IND(REG(rsp)) + OP(mov,l) TW(IND(REG(rsp)),REG(eax)) + leave + ret + +define_c_label(x86_64_write_mxcsr) + enter IMM(8),IMM(0) + OP(mov,l) TW(REG(eax),IND(REG(rsp))) + ldmxcsr IND(REG(rsp)) + leave + ret + IFDASM(`end') # Mark the stack nonexecutable. If we ever put code (e.g., diff --git a/src/microcode/cmpintmd/x86-64.h b/src/microcode/cmpintmd/x86-64.h index 55090c953..d2e3bc55b 100644 --- a/src/microcode/cmpintmd/x86-64.h +++ b/src/microcode/cmpintmd/x86-64.h @@ -239,6 +239,37 @@ extern void asm_trampoline_to_interface (void); extern insn_t * read_compiled_closure_target (insn_t *); extern insn_t * read_uuo_target (SCHEME_OBJECT *); extern void x86_64_reset_hook (void); - +extern int x86_64_read_mxcsr (void); +extern void x86_64_write_mxcsr (int); + +#ifndef HAVE_FENV_H + +# define FE_TONEAREST 0 +# define FE_DOWNWARD 1 +# define FE_UPWARD 2 +# define FE_TOWARDZERO 3 + +# define HAVE_FEGETROUND +# define HAVE_FESETROUND + +static inline int +fegetround (void) +{ + return (3 & ((x86_64_read_mxcsr ()) >> 13)); +} + +static inline void +fesetround (int mode) +{ + switch (mode) + { + case 0: case 1: case 2: case 3: break; + default: return (1); + } + x86_64_write_mxcsr ((mode << 13) | (0xffff9fff & (x86_64_read_mxcsr ()))); + return (0); +} + +#endif /* HAVE_FENV_H */ #endif /* !SCM_CMPINTMD_H_INCLUDED */ -- 2.25.1