From 2e7f194ff20a4f9a46139997b3beda8d5203f50c Mon Sep 17 00:00:00 2001 From: gbeauche <> Date: Tue, 17 Jan 2006 23:09:18 +0000 Subject: [PATCH] add generic roundf() from glibc (for IRIX/mips with older libm) --- .../src/kpx_cpu/src/mathlib/mathlib.cpp | 52 +++++++++++++++++++ .../src/kpx_cpu/src/mathlib/mathlib.hpp | 3 ++ 2 files changed, 55 insertions(+) diff --git a/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.cpp b/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.cpp index 307ae200..8eef9d64 100644 --- a/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.cpp +++ b/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.cpp @@ -51,6 +51,14 @@ do { \ (i) = gf_u.word; \ } while (0) +// Set a float from a 32 bit int +#define SET_FLOAT_WORD(d,i) \ +do { \ + ieee_float_shape_type sf_u; \ + sf_u.word = (i); \ + (d) = sf_u.value; \ +} while (0) + /** * Representation of an IEEE 754 double @@ -184,3 +192,47 @@ int mathlib_signbitl(long double x) { unimplemented("signbitl"); } + + +/** + * 7.12.9.6 The round functions + **/ + +float mathlib_roundf(float x) +{ + int32 i0, j0; + static const float huge = 1.0e30; + + GET_FLOAT_WORD (i0, x); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { + if (huge + x > 0.0F) { + i0 &= 0x80000000; + if (j0 == -1) + i0 |= 0x3f800000; + } + } + else { + u_int32_t i = 0x007fffff >> j0; + if ((i0 & i) == 0) + /* X is integral. */ + return x; + if (huge + x > 0.0F) { + /* Raise inexact if x != 0. */ + i0 += 0x00400000 >> j0; + i0 &= ~i; + } + } + } + else { + if (j0 == 0x80) + /* Inf or NaN. */ + return x + x; + else + return x; + } + + SET_FLOAT_WORD (x, i0); + return x; +} diff --git a/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.hpp b/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.hpp index 4fa5bea4..e3bf4945 100644 --- a/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.hpp +++ b/SheepShaver/src/kpx_cpu/src/mathlib/mathlib.hpp @@ -134,6 +134,9 @@ extern "C" float roundf(float x); #ifdef HAVE_ROUND extern "C" double round(double x); #define roundf(x) (float)round(x) +#else +extern float mathlib_roundf(float); +#define roundf(x) mathlib_roundf(x) #endif #endif