From 59d67522eabb778c773077a1645623c303751d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Tue, 2 Jun 2009 20:24:18 +0000 Subject: [PATCH] added lrint and llrint functions to math.h added generic implementations for ppc and m68k git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30945 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/posix/math.h | 8 +- .../posix/glibc/arch/generic/s_llrint.c | 93 +++++++++++++++++++ .../posix/glibc/arch/generic/s_llrintf.c | 76 +++++++++++++++ .../posix/glibc/arch/generic/s_llrintl.c | 77 +++++++++++++++ .../posix/glibc/arch/generic/s_lrint.c | 93 +++++++++++++++++++ .../posix/glibc/arch/generic/s_lrintf.c | 76 +++++++++++++++ .../posix/glibc/arch/generic/s_lrintl.c | 84 +++++++++++++++++ .../libroot/posix/glibc/arch/m68k/Jamfile | 2 + .../libroot/posix/glibc/arch/ppc/Jamfile | 2 + 9 files changed, 510 insertions(+), 1 deletion(-) create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_llrint.c create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_llrintf.c create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_llrintl.c create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_lrint.c create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_lrintf.c create mode 100644 src/system/libroot/posix/glibc/arch/generic/s_lrintl.c diff --git a/headers/posix/math.h b/headers/posix/math.h index e5c48de4b1..abad6a5b29 100644 --- a/headers/posix/math.h +++ b/headers/posix/math.h @@ -100,10 +100,12 @@ extern float gammaf(float x); extern float hypotf(float x, float y); extern float ldexpf(float x, int exponent); extern float lgammaf(float x); +extern long long llrintf(float x); extern float log10f(float x); extern float log1pf(float x); extern float logbf(float x); extern float logf(float x); +extern long lrintf(float x); extern long lroundf(float x); extern float modff(float x, float *y); extern float nearbyintf(float x); @@ -135,8 +137,10 @@ extern double frexp(double x, int *_exponent); extern double gamma(double x); extern double ldexp(double x, int exponent); extern double lgamma(double x); +extern long long llrint(double x); extern double log(double x); extern double log10(double x); +extern long lrint(double x); extern long lround(double x); extern double modf(double x, double *y); extern double nearbyint(double x); @@ -159,9 +163,11 @@ extern long double atanhl(long double x); extern long double atan2l(long double y, long double x); extern long double lgammal(long double x); extern long double nearbyintl(long double x); -extern long double roundl(long double x); +extern long long llrintl(long double x); +extern long lrintl(long double x); extern long lroundl(long double x); extern long double remquol(long double x, long double y, int *quo); +extern long double roundl(long double x); /* some BSD non-ANSI or POSIX math functions */ extern double cbrt(double x); diff --git a/src/system/libroot/posix/glibc/arch/generic/s_llrint.c b/src/system/libroot/posix/glibc/arch/generic/s_llrint.c new file mode 100644 index 0000000000..64c870eaaa --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_llrint.c @@ -0,0 +1,93 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2004, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const double two52[2] = +{ + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ +}; + + +long long int +__llrint (double x) +{ + int32_t j0; + u_int32_t i1, i0; + long long int result; + volatile double w; + double t; + int sx; + + EXTRACT_WORDS (i0, i1, x); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + sx = i0 >> 31; + i0 &= 0xfffff; + i0 |= 0x100000; + + if (j0 < 20) + { + w = two52[sx] + x; + t = w - two52[sx]; + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + i0 &= 0xfffff; + i0 |= 0x100000; + + result = (j0 < 0 ? 0 : i0 >> (20 - j0)); + } + else if (j0 < (int32_t) (8 * sizeof (long long int)) - 1) + { + if (j0 >= 52) + result = (((long long int) i0 << 32) | i1) << (j0 - 52); + else + { + w = two52[sx] + x; + t = w - two52[sx]; + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + i0 &= 0xfffff; + i0 |= 0x100000; + + if (j0 == 20) + result = (long long int) i0; + else + result = ((long long int) i0 << (j0 - 20)) | (i1 >> (52 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__llrint, llrint) +#ifdef NO_LONG_DOUBLE +strong_alias (__llrint, __llrintl) +weak_alias (__llrint, llrintl) +#endif diff --git a/src/system/libroot/posix/glibc/arch/generic/s_llrintf.c b/src/system/libroot/posix/glibc/arch/generic/s_llrintf.c new file mode 100644 index 0000000000..7c6e4bcda4 --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_llrintf.c @@ -0,0 +1,76 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const float two23[2] = +{ + 8.3886080000e+06, /* 0x4B000000 */ + -8.3886080000e+06, /* 0xCB000000 */ +}; + + +long long int +__llrintf (float x) +{ + int32_t j0; + u_int32_t i0; + volatile float w; + float t; + long long int result; + int sx; + + GET_FLOAT_WORD (i0, x); + + sx = i0 >> 31; + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + + if (j0 < (int32_t) (sizeof (long long int) * 8) - 1) + { + if (j0 >= 23) + result = (long long int) i0 << (j0 - 23); + else + { + w = two23[sx] + x; + t = w - two23[sx]; + GET_FLOAT_WORD (i0, t); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + + result = (j0 < 0 ? 0 : i0 >> (23 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__llrintf, llrintf) diff --git a/src/system/libroot/posix/glibc/arch/generic/s_llrintl.c b/src/system/libroot/posix/glibc/arch/generic/s_llrintl.c new file mode 100644 index 0000000000..d6eedf1f33 --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_llrintl.c @@ -0,0 +1,77 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2004, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const long double two63[2] = +{ + 9.223372036854775808000000e+18, /* 0x403E, 0x00000000, 0x00000000 */ + -9.223372036854775808000000e+18 /* 0xC03E, 0x00000000, 0x00000000 */ +}; + + +long long int +__llrintl (long double x) +{ + int32_t se,j0; + u_int32_t i0, i1; + long long int result; + volatile long double w; + long double t; + int sx; + + GET_LDOUBLE_WORDS (se, i0, i1, x); + + sx = (se >> 15) & 1; + j0 = (se & 0x7fff) - 0x3fff; + + if (j0 < (int32_t) (8 * sizeof (long long int)) - 1) + { + if (j0 >= 63) + result = (((long long int) i0 << 32) | i1) << (j0 - 63); + else + { + w = two63[sx] + x; + t = w - two63[sx]; + GET_LDOUBLE_WORDS (se, i0, i1, t); + j0 = (se & 0x7fff) - 0x3fff; + + if (j0 < 0) + result = 0; + else if (j0 <= 31) + result = i0 >> (31 - j0); + else + result = ((long long int) i0 << (j0 - 31)) | (i1 >> (63 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__llrintl, llrintl) diff --git a/src/system/libroot/posix/glibc/arch/generic/s_lrint.c b/src/system/libroot/posix/glibc/arch/generic/s_lrint.c new file mode 100644 index 0000000000..1084ed6e2d --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_lrint.c @@ -0,0 +1,93 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2004, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const double two52[2] = +{ + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ +}; + + +long int +__lrint (double x) +{ + int32_t j0; + u_int32_t i0,i1; + volatile double w; + double t; + long int result; + int sx; + + EXTRACT_WORDS (i0, i1, x); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + sx = i0 >> 31; + i0 &= 0xfffff; + i0 |= 0x100000; + + if (j0 < 20) + { + w = two52[sx] + x; + t = w - two52[sx]; + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + i0 &= 0xfffff; + i0 |= 0x100000; + + result = (j0 < 0 ? 0 : i0 >> (20 - j0)); + } + else if (j0 < (int32_t) (8 * sizeof (long int)) - 1) + { + if (j0 >= 52) + result = ((long int) i0 << (j0 - 20)) | (i1 << (j0 - 52)); + else + { + w = two52[sx] + x; + t = w - two52[sx]; + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + i0 &= 0xfffff; + i0 |= 0x100000; + + if (j0 == 20) + result = (long int) i0; + else + result = ((long int) i0 << (j0 - 20)) | (i1 >> (52 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__lrint, lrint) +#ifdef NO_LONG_DOUBLE +strong_alias (__lrint, __lrintl) +weak_alias (__lrint, lrintl) +#endif diff --git a/src/system/libroot/posix/glibc/arch/generic/s_lrintf.c b/src/system/libroot/posix/glibc/arch/generic/s_lrintf.c new file mode 100644 index 0000000000..64486a4c3e --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_lrintf.c @@ -0,0 +1,76 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const float two23[2] = +{ + 8.3886080000e+06, /* 0x4B000000 */ + -8.3886080000e+06, /* 0xCB000000 */ +}; + + +long int +__lrintf (float x) +{ + int32_t j0; + u_int32_t i0; + volatile float w; + float t; + long int result; + int sx; + + GET_FLOAT_WORD (i0, x); + + sx = i0 >> 31; + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + + if (j0 < (int32_t) (sizeof (long int) * 8) - 1) + { + if (j0 >= 23) + result = (long int) i0 << (j0 - 23); + else + { + w = two23[sx] + x; + t = w - two23[sx]; + GET_FLOAT_WORD (i0, t); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + + result = (j0 < 0 ? 0 : i0 >> (23 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__lrintf, lrintf) diff --git a/src/system/libroot/posix/glibc/arch/generic/s_lrintl.c b/src/system/libroot/posix/glibc/arch/generic/s_lrintl.c new file mode 100644 index 0000000000..621951d87b --- /dev/null +++ b/src/system/libroot/posix/glibc/arch/generic/s_lrintl.c @@ -0,0 +1,84 @@ +/* Round argument to nearest integral value according to current rounding + direction. + Copyright (C) 1997, 2004, 2006 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1997. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include + +#include "math_private.h" + +static const long double two63[2] = +{ + 9.223372036854775808000000e+18, /* 0x403E, 0x00000000, 0x00000000 */ + -9.223372036854775808000000e+18 /* 0xC03E, 0x00000000, 0x00000000 */ +}; + + +long int +__lrintl (long double x) +{ + int32_t se,j0; + u_int32_t i0, i1; + long int result; + volatile long double w; + long double t; + int sx; + + GET_LDOUBLE_WORDS (se, i0, i1, x); + + sx = (se >> 15) & 1; + j0 = (se & 0x7fff) - 0x3fff; + + if (j0 < 31) + { + w = two63[sx] + x; + t = w - two63[sx]; + GET_LDOUBLE_WORDS (se, i0, i1, t); + j0 = (se & 0x7fff) - 0x3fff; + + result = (j0 < 0 ? 0 : i0 >> (31 - j0)); + } + else if (j0 < (int32_t) (8 * sizeof (long int)) - 1) + { + if (j0 >= 63) + result = ((long int) i0 << (j0 - 31)) | (i1 << (j0 - 63)); + else + { + w = two63[sx] + x; + t = w - two63[sx]; + GET_LDOUBLE_WORDS (se, i0, i1, t); + j0 = (se & 0x7fff) - 0x3fff; + + if (j0 == 31) + result = (long int) i0; + else + result = ((long int) i0 << (j0 - 31)) | (i1 >> (63 - j0)); + } + } + else + { + /* The number is too large. It is left implementation defined + what happens. */ + return (long int) x; + } + + return sx ? -result : result; +} + +weak_alias (__lrintl, lrintl) diff --git a/src/system/libroot/posix/glibc/arch/m68k/Jamfile b/src/system/libroot/posix/glibc/arch/m68k/Jamfile index 20765f47d7..557db318da 100644 --- a/src/system/libroot/posix/glibc/arch/m68k/Jamfile +++ b/src/system/libroot/posix/glibc/arch/m68k/Jamfile @@ -77,8 +77,10 @@ local genericSources = s_ilogb.c s_ilogbf.c s_isinf.c s_isinff.c # s_isinfl.c s_ldexp.c s_ldexpf.c # s_ldexpl.c + s_llrint.c s_llrintf.c # s_llrintl.c s_log1p.c s_log1pf.c s_logb.c s_logbf.c # s_logbl.c + s_lrint.c s_lrintf.c # s_lrintl.c s_lround.c s_lroundf.c s_modf.c s_modff.c # s_modfl.c s_nan.c s_nanf.c # s_nanl.c diff --git a/src/system/libroot/posix/glibc/arch/ppc/Jamfile b/src/system/libroot/posix/glibc/arch/ppc/Jamfile index e8d94dca51..4750a7f34f 100644 --- a/src/system/libroot/posix/glibc/arch/ppc/Jamfile +++ b/src/system/libroot/posix/glibc/arch/ppc/Jamfile @@ -75,8 +75,10 @@ local genericSources = s_ilogb.c s_ilogbf.c s_isinf.c s_isinff.c # s_isinfl.c s_ldexp.c s_ldexpf.c # s_ldexpl.c + s_llrint.c s_llrintf.c # s_llrintl.c s_log1p.c s_log1pf.c s_logb.c s_logbf.c # s_logbl.c + s_lrint.c s_lrintf.c # s_lrintl.c s_lround.c s_lroundf.c s_modf.c s_modff.c # s_modfl.c s_nan.c s_nanf.c # s_nanl.c