forked from OpenXiangShan/NEMU
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* It provides better performance than softfloat. But exception and NaN handling is slow, so we disable it in host-fp. * FIXME: porvay in SPEC 2006 gets wrong answer
- Loading branch information
1 parent
dc35364
commit 795dd3c
Showing
7 changed files
with
244 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef __HOSTFP_H__ | ||
#include <rtl/fp.h> | ||
#include <math.h> | ||
#include <fenv.h> | ||
|
||
#define defaultNaNF32UI 0x7FC00000 | ||
|
||
typedef union { uint32_t v; float f; } float32_t; | ||
static inline float32_t float32(float f) { float32_t r = { .f = f }; return r; } | ||
|
||
static inline float32_t f32_add(float32_t a, float32_t b) { return float32(a.f + b.f); } | ||
static inline float32_t f32_sub(float32_t a, float32_t b) { return float32(a.f - b.f); } | ||
static inline float32_t f32_mul(float32_t a, float32_t b) { return float32(a.f * b.f); } | ||
static inline float32_t f32_div(float32_t a, float32_t b) { return float32(a.f / b.f); } | ||
static inline float32_t f32_sqrt(float32_t a) { return float32(sqrtf(a.f)); } | ||
static inline float32_t f32_mulAdd(float32_t a, float32_t b, | ||
float32_t c) { return float32(fmaf(a.f, b.f, c.f)); } | ||
static inline float32_t f32_min(float32_t a, float32_t b) { return float32(fminf(a.f, b.f)); } | ||
static inline float32_t f32_max(float32_t a, float32_t b) { return float32(fmaxf(a.f, b.f)); } | ||
static inline bool f32_le(float32_t a, float32_t b) { return a.f <= b.f; } | ||
static inline bool f32_lt(float32_t a, float32_t b) { return a.f < b.f; } | ||
static inline bool f32_eq(float32_t a, float32_t b) { return a.f == b.f; } | ||
static inline float32_t i32_to_f32 (rtlreg_t a) { return float32((int32_t)a); } | ||
static inline float32_t ui32_to_f32(rtlreg_t a) { return float32((uint32_t)a); } | ||
static inline float32_t i64_to_f32 (rtlreg_t a) { return float32((int64_t)a); } | ||
static inline float32_t ui64_to_f32(rtlreg_t a) { return float32((uint64_t)a); } | ||
static inline int32_t my_f32_to_i32 (float32_t a) { return (int32_t)a.f; } | ||
static inline uint32_t my_f32_to_ui32(float32_t a) { return (uint32_t)a.f; } | ||
static inline int64_t my_f32_to_i64 (float32_t a) { return (int64_t)a.f; } | ||
static inline uint64_t my_f32_to_ui64(float32_t a) { return (uint64_t)a.f; } | ||
|
||
|
||
typedef union { uint64_t v; double f; } float64_t; | ||
static inline float64_t float64(double f) { float64_t r = { .f = f }; return r; } | ||
|
||
static inline float64_t f64_add(float64_t a, float64_t b) { return float64(a.f + b.f); } | ||
static inline float64_t f64_sub(float64_t a, float64_t b) { return float64(a.f - b.f); } | ||
static inline float64_t f64_mul(float64_t a, float64_t b) { return float64(a.f * b.f); } | ||
static inline float64_t f64_div(float64_t a, float64_t b) { return float64(a.f / b.f); } | ||
static inline float64_t f64_sqrt(float64_t a) { return float64(sqrt(a.f)); } | ||
static inline float64_t f64_mulAdd(float64_t a, float64_t b, | ||
float64_t c) { return float64(fma(a.f, b.f, c.f)); } | ||
static inline float64_t f64_min(float64_t a, float64_t b) { return float64(fmin(a.f, b.f)); } | ||
static inline float64_t f64_max(float64_t a, float64_t b) { return float64(fmax(a.f, b.f)); } | ||
static inline bool f64_le(float64_t a, float64_t b) { return a.f <= b.f; } | ||
static inline bool f64_lt(float64_t a, float64_t b) { return a.f < b.f; } | ||
static inline bool f64_eq(float64_t a, float64_t b) { return a.f == b.f; } | ||
static inline float64_t i32_to_f64 (rtlreg_t a) { return float64((int32_t)a); } | ||
static inline float64_t ui32_to_f64(rtlreg_t a) { return float64((uint32_t)a); } | ||
static inline float64_t i64_to_f64 (rtlreg_t a) { return float64((int64_t)a); } | ||
static inline float64_t ui64_to_f64(rtlreg_t a) { return float64((uint64_t)a); } | ||
static inline int32_t my_f64_to_i32 (float64_t a) { return (int32_t)a.f; } | ||
static inline uint32_t my_f64_to_ui32(float64_t a) { return (uint32_t)a.f; } | ||
static inline int64_t my_f64_to_i64 (float64_t a) { return (int64_t)a.f; } | ||
static inline uint64_t my_f64_to_ui64(float64_t a) { return (uint64_t)a.f; } | ||
|
||
static inline float64_t f32_to_f64(float32_t a) { return float64(a.f); } | ||
static inline float32_t f64_to_f32(float64_t a) { return float32(a.f); } | ||
|
||
|
||
static inline void fp_set_rm(int rm) { | ||
switch (rm) { | ||
case FPCALL_RM_RNE: rm = FE_TONEAREST; break; | ||
case FPCALL_RM_RTZ: rm = FE_TOWARDZERO; break; | ||
case FPCALL_RM_RDN: rm = FE_DOWNWARD; break; | ||
case FPCALL_RM_RUP: rm = FE_UPWARD; break; | ||
case FPCALL_RM_RMM: rm = FE_TONEAREST; break; // x86 does not support RMM | ||
default: assert(0); | ||
} | ||
fesetround(rm); | ||
} | ||
|
||
static inline uint32_t fp_get_exception() { | ||
uint32_t ex = 0; | ||
uint32_t host_ex = fetestexcept(FE_ALL_EXCEPT); | ||
if (host_ex & FE_INEXACT ) ex |= FPCALL_EX_NX; | ||
if (host_ex & FE_UNDERFLOW) ex |= FPCALL_EX_UF; | ||
if (host_ex & FE_OVERFLOW ) ex |= FPCALL_EX_OF; | ||
if (host_ex & FE_DIVBYZERO) ex |= FPCALL_EX_DZ; | ||
if (host_ex & FE_INVALID ) ex |= FPCALL_EX_NV; | ||
return ex; | ||
} | ||
|
||
static inline void fp_clear_exception() { | ||
feclearexcept(FE_ALL_EXCEPT); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef __SOFTFLOAT_FP_H__ | ||
#define __SOFTFLOAT_FP_H__ | ||
|
||
#include <softfloat.h> | ||
#include <specialize.h> | ||
#include <internals.h> | ||
|
||
#define F32_SIGN ((uint64_t)1ul << 31) | ||
#define F64_SIGN ((uint64_t)1ul << 63) | ||
|
||
static inline float32_t rtlToF32(rtlreg_t r); | ||
static inline float64_t rtlToF64(rtlreg_t r); | ||
|
||
static inline float32_t f32_min(float32_t a, float32_t b){ | ||
bool less = f32_lt_quiet(a, b) || (f32_eq(a, b) && (a.v & F32_SIGN)); | ||
if(isNaNF32UI(a.v) && isNaNF32UI(b.v)) return rtlToF32(defaultNaNF32UI); | ||
else return(less || isNaNF32UI(b.v) ? a : b); | ||
} | ||
|
||
static inline float32_t f32_max(float32_t a, float32_t b){ | ||
bool greater = f32_lt_quiet(b, a) || (f32_eq(b, a) && (b.v & F32_SIGN)); | ||
if(isNaNF32UI(a.v) && isNaNF32UI(b.v)) return rtlToF32(defaultNaNF32UI); | ||
else return(greater || isNaNF32UI(b.v) ? a : b); | ||
} | ||
|
||
static inline float64_t f64_min(float64_t a, float64_t b){ | ||
bool less = f64_lt_quiet(a, b) || (f64_eq(a, b) && (a.v & F64_SIGN)); | ||
if(isNaNF64UI(a.v) && isNaNF64UI(b.v)) return rtlToF64(defaultNaNF64UI); | ||
else return(less || isNaNF64UI(b.v) ? a : b); | ||
} | ||
|
||
static inline float64_t f64_max(float64_t a, float64_t b){ | ||
bool greater = f64_lt_quiet(b, a) || (f64_eq(b, a) && (b.v & F64_SIGN)); | ||
if(isNaNF64UI(a.v) && isNaNF64UI(b.v)) return rtlToF64(defaultNaNF64UI); | ||
else return(greater || isNaNF64UI(b.v) ? a : b); | ||
} | ||
|
||
static inline int32_t my_f32_to_i32 (float32_t a) { | ||
return f32_to_i32 (a, softfloat_roundingMode, true); | ||
} | ||
static inline uint32_t my_f32_to_ui32(float32_t a) { | ||
return f32_to_ui32(a, softfloat_roundingMode, true); | ||
} | ||
static inline int64_t my_f32_to_i64 (float32_t a) { | ||
return f32_to_i64 (a, softfloat_roundingMode, true); | ||
} | ||
static inline uint64_t my_f32_to_ui64(float32_t a) { | ||
return f32_to_ui64(a, softfloat_roundingMode, true); | ||
} | ||
static inline int32_t my_f64_to_i32 (float64_t a) { | ||
return f64_to_i32 (a, softfloat_roundingMode, true); | ||
} | ||
static inline uint32_t my_f64_to_ui32(float64_t a) { | ||
return f64_to_ui32(a, softfloat_roundingMode, true); | ||
} | ||
static inline int64_t my_f64_to_i64 (float64_t a) { | ||
return f64_to_i64 (a, softfloat_roundingMode, true); | ||
} | ||
static inline uint64_t my_f64_to_ui64(float64_t a) { | ||
return f64_to_ui64(a, softfloat_roundingMode, true); | ||
} | ||
|
||
static inline void fp_set_rm(int rm) { | ||
switch (rm) { | ||
case FPCALL_RM_RNE: softfloat_roundingMode = softfloat_round_near_even; break; | ||
case FPCALL_RM_RTZ: softfloat_roundingMode = softfloat_round_minMag; break; | ||
case FPCALL_RM_RDN: softfloat_roundingMode = softfloat_round_min; break; | ||
case FPCALL_RM_RUP: softfloat_roundingMode = softfloat_round_max; break; | ||
case FPCALL_RM_RMM: softfloat_roundingMode = softfloat_round_near_maxMag; break; | ||
default: assert(0); | ||
} | ||
} | ||
|
||
static inline uint32_t fp_get_exception() { | ||
uint32_t ex = 0; | ||
uint32_t softfp_ex = softfloat_exceptionFlags; | ||
if (softfp_ex & softfloat_flag_inexact ) ex |= FPCALL_EX_NX; | ||
if (softfp_ex & softfloat_flag_underflow) ex |= FPCALL_EX_UF; | ||
if (softfp_ex & softfloat_flag_overflow ) ex |= FPCALL_EX_OF; | ||
if (softfp_ex & softfloat_flag_infinite ) ex |= FPCALL_EX_DZ; | ||
if (softfp_ex & softfloat_flag_invalid ) ex |= FPCALL_EX_NV; | ||
return ex; | ||
} | ||
|
||
static inline void fp_clear_exception() { | ||
softfloat_exceptionFlags = 0; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters