From 0ef21ef51c087909b39e3786a0efbb03915ad2d5 Mon Sep 17 00:00:00 2001 From: Mark Kettenis Date: Mon, 25 Sep 2006 20:25:42 +0000 Subject: Move llrint.c, llrintf.c, lrint.c and lrintf.c to s_llrint.c, s_llrintf.c, s_lrint.c and s_lrintf.c, and add i387-specific assembler versions. ok deraadt@, otto@ --- lib/libm/Makefile | 13 +++--- lib/libm/arch/i387/s_llrint.S | 35 ++++++++++++++++ lib/libm/arch/i387/s_llrintf.S | 35 ++++++++++++++++ lib/libm/arch/i387/s_lrint.S | 34 +++++++++++++++ lib/libm/arch/i387/s_lrintf.S | 34 +++++++++++++++ lib/libm/src/llrint.c | 14 ------- lib/libm/src/llrintf.c | 14 ------- lib/libm/src/lrint.c | 95 ------------------------------------------ lib/libm/src/lrintf.c | 92 ---------------------------------------- lib/libm/src/s_llrint.c | 14 +++++++ lib/libm/src/s_llrintf.c | 14 +++++++ lib/libm/src/s_lrint.c | 95 ++++++++++++++++++++++++++++++++++++++++++ lib/libm/src/s_lrintf.c | 92 ++++++++++++++++++++++++++++++++++++++++ 13 files changed, 360 insertions(+), 221 deletions(-) create mode 100644 lib/libm/arch/i387/s_llrint.S create mode 100644 lib/libm/arch/i387/s_llrintf.S create mode 100644 lib/libm/arch/i387/s_lrint.S create mode 100644 lib/libm/arch/i387/s_lrintf.S delete mode 100644 lib/libm/src/llrint.c delete mode 100644 lib/libm/src/llrintf.c delete mode 100644 lib/libm/src/lrint.c delete mode 100644 lib/libm/src/lrintf.c create mode 100644 lib/libm/src/s_llrint.c create mode 100644 lib/libm/src/s_llrintf.c create mode 100644 lib/libm/src/s_lrint.c create mode 100644 lib/libm/src/s_lrintf.c (limited to 'lib') diff --git a/lib/libm/Makefile b/lib/libm/Makefile index 617cc78cafe..57cb541cb30 100644 --- a/lib/libm/Makefile +++ b/lib/libm/Makefile @@ -1,5 +1,5 @@ # $NetBSD: Makefile,v 1.28 1995/11/20 22:06:19 jtc Exp $ -# $OpenBSD: Makefile,v 1.40 2006/07/12 07:26:08 brad Exp $ +# $OpenBSD: Makefile,v 1.41 2006/09/25 20:25:41 kettenis Exp $ # # @(#)Makefile 5.1beta 93/09/24 # @@ -61,8 +61,9 @@ ARCH_SRCS = e_acos.S e_asin.S e_atan2.S e_exp.S e_fmod.S e_log.S e_log10.S \ s_atan.S s_atanf.S s_ceil.S s_ceilf.S s_copysign.S s_copysignf.S \ s_cos.S s_cosf.S s_finite.S s_finitef.S s_floor.S s_floorf.S \ s_ilogb.S s_ilogbf.S s_log1p.S s_log1pf.S s_logb.S s_logbf.S \ - s_rint.S s_rintf.S s_scalbn.S s_scalbnf.S s_significand.S \ - s_significandf.S s_sin.S s_sinf.S s_tan.S s_tanf.S + s_llrint.S s_llrintf.S s_lrint.S s_lrintf.S s_rint.S s_rintf.S\ + s_scalbn.S s_scalbnf.S s_significand.S s_significandf.S \ + s_sin.S s_sinf.S s_tan.S s_tanf.S .elif (${MACHINE_ARCH} == "amd64") .PATH: ${.CURDIR}/arch/amd64 CPPFLAGS+=-I${.CURDIR}/arch/amd64 @@ -108,14 +109,14 @@ COMMON_SRCS = e_acos.c e_acosf.c e_acosh.c e_acoshf.c e_asin.c e_asinf.c \ e_sinh.c e_sinhf.c e_sqrt.c e_sqrtf.c \ k_cos.c k_cosf.c k_rem_pio2.c k_rem_pio2f.c k_sin.c k_sinf.c \ k_standard.c k_tan.c k_tanf.c \ - lrint.c lrintf.c llrint.c llrintf.c lround.c lroundf.c llround.c \ - llroundf.c \ + lround.c lroundf.c llround.c llroundf.c \ s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_cbrt.c s_cbrtf.c s_ceil.c \ s_ceilf.c s_copysign.c s_copysignf.c s_cos.c s_cosf.c s_erf.c s_erff.c \ s_expm1.c s_expm1f.c s_fabsf.c s_finite.c s_finitef.c \ s_floor.c s_floorf.c s_frexpf.c s_ilogb.c s_ilogbf.c \ s_isinff.c s_isnanf.c s_ldexpf.c s_lib_version.c s_log1p.c \ - s_log1pf.c s_logb.c s_logbf.c s_matherr.c s_modff.c \ + s_log1pf.c s_logb.c s_logbf.c s_llrint.c s_llrintf.c s_lrint.c \ + s_lrintf.c s_matherr.c s_modff.c \ s_nextafter.c s_nextafterf.c s_rint.c s_rintf.c s_round.c s_roundf.c \ s_scalbn.c s_scalbnf.c s_signgam.c s_significand.c s_significandf.c \ s_sin.c s_sinf.c s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_trunc.c s_truncf.c \ diff --git a/lib/libm/arch/i387/s_llrint.S b/lib/libm/arch/i387/s_llrint.S new file mode 100644 index 00000000000..6a801834b0c --- /dev/null +++ b/lib/libm/arch/i387/s_llrint.S @@ -0,0 +1,35 @@ +/*- + * Copyright (c) 2005 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +ENTRY(llrint) + fldl 4(%esp) + subl $8,%esp + fistpll (%esp) + popl %eax + popl %edx + ret diff --git a/lib/libm/arch/i387/s_llrintf.S b/lib/libm/arch/i387/s_llrintf.S new file mode 100644 index 00000000000..7d3e9500150 --- /dev/null +++ b/lib/libm/arch/i387/s_llrintf.S @@ -0,0 +1,35 @@ +/*- + * Copyright (c) 2005 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +ENTRY(llrintf) + flds 4(%esp) + subl $8,%esp + fistpll (%esp) + popl %eax + popl %edx + ret diff --git a/lib/libm/arch/i387/s_lrint.S b/lib/libm/arch/i387/s_lrint.S new file mode 100644 index 00000000000..fb43e6b57fb --- /dev/null +++ b/lib/libm/arch/i387/s_lrint.S @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2005 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +ENTRY(lrint) + fldl 4(%esp) + subl $4,%esp + fistpl (%esp) + popl %eax + ret diff --git a/lib/libm/arch/i387/s_lrintf.S b/lib/libm/arch/i387/s_lrintf.S new file mode 100644 index 00000000000..07e1ff4d2af --- /dev/null +++ b/lib/libm/arch/i387/s_lrintf.S @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2005 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +ENTRY(lrintf) + flds 4(%esp) + subl $4,%esp + fistpl (%esp) + popl %eax + ret diff --git a/lib/libm/src/llrint.c b/lib/libm/src/llrint.c deleted file mode 100644 index c92da9e3c7d..00000000000 --- a/lib/libm/src/llrint.c +++ /dev/null @@ -1,14 +0,0 @@ -/* $OpenBSD: llrint.c,v 1.1 2005/11/17 20:07:40 otto Exp $ */ -/* $NetBSD: llrint.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */ - -/* - * Written by Matthias Drochner . - * Public domain. - */ - -#define LRINTNAME llrint -#define RESTYPE long long int -#define RESTYPE_MIN LLONG_MIN -#define RESTYPE_MAX LLONG_MAX - -#include "lrint.c" diff --git a/lib/libm/src/llrintf.c b/lib/libm/src/llrintf.c deleted file mode 100644 index 2a242afb13d..00000000000 --- a/lib/libm/src/llrintf.c +++ /dev/null @@ -1,14 +0,0 @@ -/* $OpenBSD: llrintf.c,v 1.1 2005/11/17 20:07:40 otto Exp $ */ -/* $NetBSD: llrintf.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */ - -/* - * Written by Matthias Drochner . - * Public domain. - */ - -#define LRINTNAME llrintf -#define RESTYPE long long int -#define RESTYPE_MIN LLONG_MIN -#define RESTYPE_MAX LLONG_MAX - -#include "lrintf.c" diff --git a/lib/libm/src/lrint.c b/lib/libm/src/lrint.c deleted file mode 100644 index 146bc81b10f..00000000000 --- a/lib/libm/src/lrint.c +++ /dev/null @@ -1,95 +0,0 @@ -/* $OpenBSD: lrint.c,v 1.1 2005/11/17 20:07:40 otto Exp $ */ -/* $NetBSD: lrint.c,v 1.3 2004/10/13 15:18:32 drochner Exp $ */ - -/*- - * Copyright (c) 2004 - * Matthias Drochner. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include "math_private.h" - -#ifndef LRINTNAME -#define LRINTNAME lrint -#define RESTYPE long int -#define RESTYPE_MIN LONG_MIN -#define RESTYPE_MAX LONG_MAX -#endif - -#define RESTYPE_BITS (sizeof(RESTYPE) * 8) - -static const double -TWO52[2]={ - 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ - -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ -}; - -RESTYPE -LRINTNAME(double x) -{ - u_int32_t i0, i1; - int e, s, shift; - RESTYPE res; - - GET_HIGH_WORD(i0, x); - e = i0 >> 20; - s = e >> DBL_EXPBITS; - e = (e & 0x7ff) - DBL_EXP_BIAS; - - /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */ - if (e < -1) - return (0); - /* 1.0 x 2^31 (or 2^63) is already too large */ - if (e >= (int)RESTYPE_BITS - 1) - return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */ - - /* >= 2^52 is already an exact integer */ - if (e < DBL_FRACBITS) { - /* round, using current direction */ - x += TWO52[s]; - x -= TWO52[s]; - } - - EXTRACT_WORDS(i0, i1, x); - e = ((i0 >> 20) & 0x7ff) - DBL_EXP_BIAS; - i0 &= 0xfffff; - i0 |= (1 << 20); - - shift = e - DBL_FRACBITS; - if (shift >=0) - res = (shift < 32 ? (RESTYPE)i1 << shift : 0); - else - res = (shift > -32 ? i1 >> -shift : 0); - shift += 32; - if (shift >=0) - res |= (shift < 32 ? (RESTYPE)i0 << shift : 0); - else - res |= (shift > -32 ? i0 >> -shift : 0); - - return (s ? -res : res); -} diff --git a/lib/libm/src/lrintf.c b/lib/libm/src/lrintf.c deleted file mode 100644 index 2db9fa21342..00000000000 --- a/lib/libm/src/lrintf.c +++ /dev/null @@ -1,92 +0,0 @@ -/* $OpenBSD: lrintf.c,v 1.2 2005/11/17 20:51:56 otto Exp $ */ -/* $NetBSD: lrintf.c,v 1.3 2004/10/13 15:18:32 drochner Exp $ */ - -/*- - * Copyright (c) 2004 - * Matthias Drochner. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include "math_private.h" - -#ifndef LRINTNAME -#define LRINTNAME lrintf -#define RESTYPE long int -#define RESTYPE_MIN LONG_MIN -#define RESTYPE_MAX LONG_MAX -#endif - -#define RESTYPE_BITS (sizeof(RESTYPE) * 8) - -static const float -TWO23[2]={ - 8.3886080000e+06, /* 0x4b000000 */ - -8.3886080000e+06, /* 0xcb000000 */ -}; - -RESTYPE -LRINTNAME(float x) -{ - u_int32_t i0; - int e, s, shift; - RESTYPE res; - - GET_FLOAT_WORD(i0, x); - e = i0 >> SNG_FRACBITS; - s = e >> SNG_EXPBITS; - e = (e & 0xff) - SNG_EXP_BIAS; - - /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */ - if (e < -1) - return (0); - /* 1.0 x 2^31 (or 2^63) is already too large */ - if (e >= (int)RESTYPE_BITS - 1) - return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */ - - /* >= 2^23 is already an exact integer */ - if (e < SNG_FRACBITS) { - volatile float t = x; /* work around gcc problem */ - /* round, using current direction */ - t += TWO23[s]; - t -= TWO23[s]; - x = t; - } - - GET_FLOAT_WORD(i0, x); - e = ((i0 >> SNG_FRACBITS) & 0xff) - SNG_EXP_BIAS; - i0 &= 0x7fffff; - i0 |= (1 << SNG_FRACBITS); - - shift = e - SNG_FRACBITS; - if (shift >=0) - res = (shift < 32 ? (RESTYPE)i0 << shift : 0); - else - res = (shift > -32 ? i0 >> -shift : 0); - - return (s ? -res : res); -} diff --git a/lib/libm/src/s_llrint.c b/lib/libm/src/s_llrint.c new file mode 100644 index 00000000000..56c321f3d41 --- /dev/null +++ b/lib/libm/src/s_llrint.c @@ -0,0 +1,14 @@ +/* $OpenBSD: s_llrint.c,v 1.1 2006/09/25 20:25:41 kettenis Exp $ */ +/* $NetBSD: llrint.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */ + +/* + * Written by Matthias Drochner . + * Public domain. + */ + +#define LRINTNAME llrint +#define RESTYPE long long int +#define RESTYPE_MIN LLONG_MIN +#define RESTYPE_MAX LLONG_MAX + +#include "s_lrint.c" diff --git a/lib/libm/src/s_llrintf.c b/lib/libm/src/s_llrintf.c new file mode 100644 index 00000000000..36a806f3880 --- /dev/null +++ b/lib/libm/src/s_llrintf.c @@ -0,0 +1,14 @@ +/* $OpenBSD: s_llrintf.c,v 1.1 2006/09/25 20:25:41 kettenis Exp $ */ +/* $NetBSD: llrintf.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */ + +/* + * Written by Matthias Drochner . + * Public domain. + */ + +#define LRINTNAME llrintf +#define RESTYPE long long int +#define RESTYPE_MIN LLONG_MIN +#define RESTYPE_MAX LLONG_MAX + +#include "lrintf.c" diff --git a/lib/libm/src/s_lrint.c b/lib/libm/src/s_lrint.c new file mode 100644 index 00000000000..43684f3c503 --- /dev/null +++ b/lib/libm/src/s_lrint.c @@ -0,0 +1,95 @@ +/* $OpenBSD: s_lrint.c,v 1.1 2006/09/25 20:25:41 kettenis Exp $ */ +/* $NetBSD: lrint.c,v 1.3 2004/10/13 15:18:32 drochner Exp $ */ + +/*- + * Copyright (c) 2004 + * Matthias Drochner. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include "math_private.h" + +#ifndef LRINTNAME +#define LRINTNAME lrint +#define RESTYPE long int +#define RESTYPE_MIN LONG_MIN +#define RESTYPE_MAX LONG_MAX +#endif + +#define RESTYPE_BITS (sizeof(RESTYPE) * 8) + +static const double +TWO52[2]={ + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */ +}; + +RESTYPE +LRINTNAME(double x) +{ + u_int32_t i0, i1; + int e, s, shift; + RESTYPE res; + + GET_HIGH_WORD(i0, x); + e = i0 >> 20; + s = e >> DBL_EXPBITS; + e = (e & 0x7ff) - DBL_EXP_BIAS; + + /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */ + if (e < -1) + return (0); + /* 1.0 x 2^31 (or 2^63) is already too large */ + if (e >= (int)RESTYPE_BITS - 1) + return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */ + + /* >= 2^52 is already an exact integer */ + if (e < DBL_FRACBITS) { + /* round, using current direction */ + x += TWO52[s]; + x -= TWO52[s]; + } + + EXTRACT_WORDS(i0, i1, x); + e = ((i0 >> 20) & 0x7ff) - DBL_EXP_BIAS; + i0 &= 0xfffff; + i0 |= (1 << 20); + + shift = e - DBL_FRACBITS; + if (shift >=0) + res = (shift < 32 ? (RESTYPE)i1 << shift : 0); + else + res = (shift > -32 ? i1 >> -shift : 0); + shift += 32; + if (shift >=0) + res |= (shift < 32 ? (RESTYPE)i0 << shift : 0); + else + res |= (shift > -32 ? i0 >> -shift : 0); + + return (s ? -res : res); +} diff --git a/lib/libm/src/s_lrintf.c b/lib/libm/src/s_lrintf.c new file mode 100644 index 00000000000..d045e116517 --- /dev/null +++ b/lib/libm/src/s_lrintf.c @@ -0,0 +1,92 @@ +/* $OpenBSD: s_lrintf.c,v 1.1 2006/09/25 20:25:41 kettenis Exp $ */ +/* $NetBSD: lrintf.c,v 1.3 2004/10/13 15:18:32 drochner Exp $ */ + +/*- + * Copyright (c) 2004 + * Matthias Drochner. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include "math_private.h" + +#ifndef LRINTNAME +#define LRINTNAME lrintf +#define RESTYPE long int +#define RESTYPE_MIN LONG_MIN +#define RESTYPE_MAX LONG_MAX +#endif + +#define RESTYPE_BITS (sizeof(RESTYPE) * 8) + +static const float +TWO23[2]={ + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ +}; + +RESTYPE +LRINTNAME(float x) +{ + u_int32_t i0; + int e, s, shift; + RESTYPE res; + + GET_FLOAT_WORD(i0, x); + e = i0 >> SNG_FRACBITS; + s = e >> SNG_EXPBITS; + e = (e & 0xff) - SNG_EXP_BIAS; + + /* 1.0 x 2^-1 is the smallest number which can be rounded to 1 */ + if (e < -1) + return (0); + /* 1.0 x 2^31 (or 2^63) is already too large */ + if (e >= (int)RESTYPE_BITS - 1) + return (s ? RESTYPE_MIN : RESTYPE_MAX); /* ??? unspecified */ + + /* >= 2^23 is already an exact integer */ + if (e < SNG_FRACBITS) { + volatile float t = x; /* work around gcc problem */ + /* round, using current direction */ + t += TWO23[s]; + t -= TWO23[s]; + x = t; + } + + GET_FLOAT_WORD(i0, x); + e = ((i0 >> SNG_FRACBITS) & 0xff) - SNG_EXP_BIAS; + i0 &= 0x7fffff; + i0 |= (1 << SNG_FRACBITS); + + shift = e - SNG_FRACBITS; + if (shift >=0) + res = (shift < 32 ? (RESTYPE)i0 << shift : 0); + else + res = (shift > -32 ? i0 >> -shift : 0); + + return (s ? -res : res); +} -- cgit v1.2.3