library/4597: libm additions for c99

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

library/4597: libm additions for c99

Jason Wright-2
>Number:         4597
>Category:       library
>Synopsis:       add {l,ll}{rint,round}{,f} and friends to libm
>Confidential:   yes
>Severity:       non-critical
>Priority:       low
>Responsible:    bugs
>State:          open
>Quarter:        
>Keywords:      
>Date-Required:
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Nov 04 23:30:01 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Jason Wright
>Release:        -current
>Organization:
Sadly, none.
>Environment:
       
        System      : OpenBSD 3.8
        Architecture: OpenBSD.i386
        Machine     : i386
>Description:
        libm is missing functions defined in c99
>How-To-Repeat:
        try to use a program that depends on lrintf().
>Fix:
        patch below... code is largely from NetBSD (which in turn is from
FreeBSD), with the addition of lround.3 directly from FreeBSD.

Future work:
1. there's an asm version of lrint for alpha/*86*.  Worth it? *shrug*
2. there are more missing functions: round{,f} and log2{,f}

Index: lib/libm/Makefile
===================================================================
RCS file: /cvs/src/lib/libm/Makefile,v
retrieving revision 1.34
diff -u -r1.34 Makefile
--- lib/libm/Makefile 6 Apr 2004 00:21:18 -0000 1.34
+++ lib/libm/Makefile 4 Nov 2005 23:06:30 -0000
@@ -123,7 +123,9 @@
  w_j0f.c w_j1.c w_j1f.c w_jn.c w_jnf.c w_lgamma.c w_lgamma_r.c \
  w_lgammaf.c w_lgammaf_r.c w_log.c w_log10.c w_log10f.c w_logf.c \
  w_pow.c w_powf.c w_remainder.c w_remainderf.c w_scalb.c w_scalbf.c \
- w_sinh.c w_sinhf.c w_sqrt.c w_sqrtf.c
+ w_sinh.c w_sinhf.c w_sqrt.c w_sqrtf.c \
+ lrint.c lrintf.c llrint.c llrintf.c lround.c lroundf.c llround.c \
+ llroundf.c
 
 # math routines for non-IEEE architectures.
 NOIEEE_SRCS = n_asincos.c n_acosh.c n_asinh.c n_atan.c n_atanh.c n_cosh.c \
@@ -157,9 +159,8 @@
 
 MAN+= acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 ceil.3 \
  cos.3 cosh.3 erf.3 exp.3 fabs.3 floor.3 fmod.3 hypot.3 ieee.3 \
- logb.3 j0.3 lgamma.3 math.3 rint.3 sin.3 sinh.3 sqrt.3 \
- tan.3 tanh.3
-MAN+= infnan.3
+ infnan.3 logb.3 j0.3 lgamma.3 lrint.3 lround.3 math.3 rint.3 \
+ sin.3 sinh.3 sqrt.3 tan.3 tanh.3
 
 MLINKS+=erf.3 erfc.3
 MLINKS+=exp.3 expm1.3 exp.3 log.3 exp.3 log10.3 exp.3 log1p.3 exp.3 pow.3
@@ -214,6 +215,12 @@
 MLINKS+=j0.3 ynf.3
 MLINKS+=lgamma.3 gammaf.3
 MLINKS+=lgamma.3 lgammaf.3
+MLINKS+=lrint.3 lrintf.3
+MLINKS+=lrint.3 llrint.3
+MLINKS+=lrint.3 llrintf.3
+MLINKS+=lround.3 lroundf.3
+MLINKS+=lround.3 llround.3
+MLINKS+=lround.3 llroundf.3
 MLINKS+=math.3 mathf.3
 MLINKS+=rint.3 rintf.3
 MLINKS+=sin.3 sinf.3
Index: lib/libm/man/lrint.3
===================================================================
RCS file: lib/libm/man/lrint.3
diff -N lib/libm/man/lrint.3
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/man/lrint.3 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,104 @@
+.\" $OpenBSD$
+.\" $NetBSD: lrint.3,v 1.1 2005/09/16 15:26:47 wiz Exp $
+.\"
+.\" Copyright (c) 2005 David Schultz <[hidden email]>
+.\" 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.
+.\"
+.\" $FreeBSD: /repoman/r/ncvs/src/lib/msun/man/lrint.3,v 1.2.2.2 2005/03/01 16:18:39 brueffer Exp $
+.\"
+.Dd January 11, 2005
+.Dt LRINT 3
+.Os
+.Sh NAME
+.Nm llrint ,
+.Nm llrintf ,
+.Nm lrint ,
+.Nm lrintf
+.Nd convert to integer
+.Sh LIBRARY
+.Lb libm
+.Sh SYNOPSIS
+.In math.h
+.Ft long long
+.Fn llrint "double x"
+.Ft long long
+.Fn llrintf "float x"
+.Ft long
+.Fn lrint "double x"
+.Ft long
+.Fn lrintf "float x"
+.Sh DESCRIPTION
+The
+.Fn lrint
+function returns the integer nearest to its argument
+.Fa x
+according to the current rounding mode.
+.Pp
+The
+.Fn llrint ,
+.Fn llrintf ,
+and
+.Fn lrintf
+functions differ from
+.Fn lrint
+only in their input and output types.
+.Sh RETURN VALUES
+The
+.Nm llrint ,
+.Nm llrintf ,
+.Nm lrint ,
+and
+.Nm lrintf
+functions return the integer nearest to their argument
+.Fa x
+according to the current rounding mode.
+If the rounded result is too large to be represented as a
+.Vt long long
+or
+.Vt long
+value, respectively,
+.\" an invalid exception is raised and
+the return value is undefined.
+.\" Otherwise, if
+.\" .Fa x
+.\" is not an integer,
+.\" .Fn lrint
+.\" raises an inexact exception.
+.\" If
+.\" .Fa x
+.\" is too large, a range error may occur.
+.Sh SEE ALSO
+.\" .Xr lround 3 ,
+.Xr math 3 ,
+.Xr rint 3 ,
+.Xr round 3
+.Sh STANDARDS
+The
+.Fn llrint ,
+.Fn llrintf ,
+.Fn lrint ,
+and
+.Fn lrintf
+functions conform to
+.St -isoC-99 .
Index: lib/libm/man/lround.3
===================================================================
RCS file: lib/libm/man/lround.3
diff -N lib/libm/man/lround.3
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/man/lround.3 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,103 @@
+.\" $OpenBSD$
+.\"
+.\" Copyright (c) 2005 David Schultz <[hidden email]>
+.\" 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.
+.\"
+.\" $FreeBSD: /repoman/r/ncvs/src/lib/msun/man/lround.3,v 1.4 2005/06/15 19:04:04 ru Exp $
+.\"
+.Dd April 7, 2005
+.Dt LROUND 3
+.Os
+.Sh NAME
+.Nm llround ,
+.Nm llroundf ,
+.\" .Nm llroundl ,
+.Nm lround ,
+.Nm lroundf
+.\" .Nm lroundl
+.Nd "convert to nearest integral value"
+.Sh LIBRARY
+.Lb libm
+.Sh SYNOPSIS
+.In math.h
+.Ft "long long"
+.Fn llround "double x"
+.Ft "long long"
+.Fn llroundf "float x"
+.\" .Ft "long long"
+.\" .Fn llroundl "long double x"
+.Ft long
+.Fn lround "double x"
+.Ft long
+.Fn lroundf "float x"
+.\" .Ft long
+.\" .Fn lroundl "long double x"
+.Sh DESCRIPTION
+The
+.Fn lround
+function returns the integer nearest to its argument
+.Fa x ,
+rounding away from zero in halfway cases.
+If the rounded result is too large to be represented as a
+.Vt long
+value, an invalid exception is raised and the return value is undefined.
+Otherwise, if
+.Fa x
+is not an integer,
+.Fn lround
+may raise an inexact exception.
+When the rounded result is representable as a
+.Vt long ,
+the expression
+.Fn lround x
+is equivalent to
+.Po Vt long Pc Ns Fn round x
+(although the former may be more efficient).
+.Pp
+The
+.Fn llround ,
+.Fn llroundf ,
+.\" .Fn llroundl ,
+.\" .Fn lroundl
+and
+.Fn lroundf
+functions differ from
+.Fn lround
+only in their input and output types.
+.Sh SEE ALSO
+.Xr lrint 3 ,
+.Xr math 3 ,
+.Xr rint 3 ,
+.Xr round 3
+.Sh STANDARDS
+The
+.Fn llround ,
+.Fn llroundf ,
+.\" .Fn lroundl
+.\" .Fn llroundl ,
+.Fn lround ,
+and
+.Fn lroundf
+functions conform to
+.St -isoC-99 .
Index: lib/libm/src/llrint.c
===================================================================
RCS file: lib/libm/src/llrint.c
diff -N lib/libm/src/llrint.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/llrint.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,14 @@
+/* $OpenBSD$ */
+/* $NetBSD: llrint.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
+
+/*
+ * Written by Matthias Drochner <[hidden email]>.
+ * Public domain.
+ */
+
+#define LRINTNAME llrint
+#define RESTYPE long long int
+#define RESTYPE_MIN LLONG_MIN
+#define RESTYPE_MAX LLONG_MAX
+
+#include "lrint.c"
Index: lib/libm/src/llrintf.c
===================================================================
RCS file: lib/libm/src/llrintf.c
diff -N lib/libm/src/llrintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/llrintf.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,14 @@
+/* $OpenBSD$ */
+/* $NetBSD: llrintf.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
+
+/*
+ * Written by Matthias Drochner <[hidden email]>.
+ * Public domain.
+ */
+
+#define LRINTNAME llrintf
+#define RESTYPE long long int
+#define RESTYPE_MIN LLONG_MIN
+#define RESTYPE_MAX LLONG_MAX
+
+#include "lrintf.c"
Index: lib/libm/src/llround.c
===================================================================
RCS file: lib/libm/src/llround.c
diff -N lib/libm/src/llround.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/llround.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,14 @@
+/* $OpenBSD$ */
+/* $NetBSD: llround.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
+
+/*
+ * Written by Matthias Drochner <[hidden email]>.
+ * Public domain.
+ */
+
+#define LROUNDNAME llround
+#define RESTYPE long long int
+#define RESTYPE_MIN LLONG_MIN
+#define RESTYPE_MAX LLONG_MAX
+
+#include "lround.c"
Index: lib/libm/src/llroundf.c
===================================================================
RCS file: lib/libm/src/llroundf.c
diff -N lib/libm/src/llroundf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/llroundf.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,14 @@
+/* $OpenBSD$ */
+/* $NetBSD: llroundf.c,v 1.2 2004/10/13 15:18:32 drochner Exp $ */
+
+/*
+ * Written by Matthias Drochner <[hidden email]>.
+ * Public domain.
+ */
+
+#define LROUNDNAME llroundf
+#define RESTYPE long long int
+#define RESTYPE_MIN LLONG_MIN
+#define RESTYPE_MAX LLONG_MAX
+
+#include "lroundf.c"
Index: lib/libm/src/lrint.c
===================================================================
RCS file: lib/libm/src/lrint.c
diff -N lib/libm/src/lrint.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/lrint.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,95 @@
+/* $OpenBSD$ */
+/* $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 <sys/types.h>
+#include <sys/limits.h>
+#include <math.h>
+#include <ieeefp.h>
+#include <machine/ieee.h>
+#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);
+}
Index: lib/libm/src/lrintf.c
===================================================================
RCS file: lib/libm/src/lrintf.c
diff -N lib/libm/src/lrintf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/lrintf.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,90 @@
+/* $OpenBSD$ */
+/* $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 <sys/types.h>
+#include <sys/limits.h>
+#include <math.h>
+#include <ieeefp.h>
+#include <machine/ieee.h>
+#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) {
+ /* round, using current direction */
+ x += TWO23[s];
+ x -= TWO23[s];
+ }
+
+ 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);
+}
Index: lib/libm/src/lround.c
===================================================================
RCS file: lib/libm/src/lround.c
diff -N lib/libm/src/lround.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/lround.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,88 @@
+/* $OpenBSD$ */
+/* $NetBSD: lround.c,v 1.2 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 <sys/types.h>
+#include <sys/limits.h>
+#include <math.h>
+#include <ieeefp.h>
+#include <machine/ieee.h>
+#include "math_private.h"
+
+#ifndef LROUNDNAME
+#define LROUNDNAME lround
+#define RESTYPE long int
+#define RESTYPE_MIN LONG_MIN
+#define RESTYPE_MAX LONG_MAX
+#endif
+
+#define RESTYPE_BITS (sizeof(RESTYPE) * 8)
+
+RESTYPE
+LROUNDNAME(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) {
+ /* add 0.5, extraction below will truncate */
+ x += (s ? -0.5 : 0.5);
+ }
+
+ 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);
+}
Index: lib/libm/src/lroundf.c
===================================================================
RCS file: lib/libm/src/lroundf.c
diff -N lib/libm/src/lroundf.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libm/src/lroundf.c 4 Nov 2005 23:06:30 -0000
@@ -0,0 +1,83 @@
+/* $OpenBSD$ */
+/* $NetBSD: lroundf.c,v 1.2 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 <math.h>
+#include <sys/types.h>
+#include <sys/limits.h>
+#include <ieeefp.h>
+#include <machine/ieee.h>
+#include "math_private.h"
+
+#ifndef LROUNDNAME
+#define LROUNDNAME lroundf
+#define RESTYPE long int
+#define RESTYPE_MIN LONG_MIN
+#define RESTYPE_MAX LONG_MAX
+#endif
+
+#define RESTYPE_BITS (sizeof(RESTYPE) * 8)
+
+RESTYPE
+LROUNDNAME(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) {
+ /* add 0.5, extraction below will truncate */
+ x += (s ? -0.5 : 0.5);
+ }
+
+ 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);
+}
Index: include/math.h
===================================================================
RCS file: /cvs/src/include/math.h,v
retrieving revision 1.9
diff -u -r1.9 math.h
--- include/math.h 6 Jan 2005 20:36:23 -0000 1.9
+++ include/math.h 4 Nov 2005 23:06:30 -0000
@@ -182,6 +182,10 @@
 extern double copysign(double, double);
 extern int ilogb(double);
 extern double rint(double);
+extern long int lrint(double);
+extern long int lround(double);
+extern long long int llrint(double);
+extern long long int llround(double);
 extern double scalbn(double, int);
 
 /*
@@ -266,6 +270,10 @@
 extern float copysignf(float, float);
 extern int ilogbf(float);
 extern float rintf(float);
+extern long int lrintf(float);
+extern long int lroundf(float);
+extern long long int llrintf(float);
+extern long long int llroundf(float);
 extern float scalbnf(float, int);
 
 /*


>Release-Note:
>Audit-Trail:
>Unformatted: