From: Peter Avalos Date: Sun, 17 Jun 2007 06:26:18 +0000 (+0000) Subject: Add trunc and truncf. X-Git-Tag: v2.0.1~2815 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/d0090fae4aa595c845a5c293998b10e94471621b Add trunc and truncf. Obtained-from: NetBSD --- diff --git a/include/math.h b/include/math.h index 04d399fc9a..c8fe368fb3 100644 --- a/include/math.h +++ b/include/math.h @@ -9,7 +9,7 @@ * ==================================================== * * $NetBSD: math.h,v 1.41 2005/07/21 12:56:29 christos Exp $ - * $DragonFly: src/include/math.h,v 1.9 2007/06/16 22:26:53 pavalos Exp $ + * $DragonFly: src/include/math.h,v 1.10 2007/06/17 06:26:18 pavalos Exp $ */ /* @@ -291,6 +291,8 @@ float floorf(float); float rintf(float); double round(double); float roundf(float); +double trunc(double); +float truncf(float); long int lrint(double); long int lrintf(float); /* LONGLONG */ diff --git a/lib/libm/man/Makefile.inc b/lib/libm/man/Makefile.inc index 6703737680..57abe6262d 100644 --- a/lib/libm/man/Makefile.inc +++ b/lib/libm/man/Makefile.inc @@ -1,11 +1,11 @@ -# $DragonFly: src/lib/libm/man/Makefile.inc,v 1.5 2007/06/16 23:04:43 pavalos Exp $ +# $DragonFly: src/lib/libm/man/Makefile.inc,v 1.6 2007/06/17 06:26:18 pavalos Exp $ .PATH: ${.CURDIR}/man 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 \ ieee_test.3 j0.3 lgamma.3 math.3 rint.3 round.3 sin.3 \ - sinh.3 sqrt.3 tan.3 tanh.3 + sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 MLINKS+= \ acos.3 acosf.3 acosh.3 acoshf.3 asin.3 asinf.3 asinh.3 asinhf.3 \ atan.3 atanf.3 atan2.3 atan2f.3 atanh.3 atanhf.3 ceil.3 ceilf.3 \ @@ -25,4 +25,4 @@ MLINKS+= \ lgamma.3 lgammaf_r.3 lgamma.3 gamma.3 lgamma.3 gammaf.3 \ lgamma.3 gamma_r.3 lgamma.3 gammaf_r.3 rint.3 rintf.3 \ round.3 roundf.3 sin.3 sinf.3 sinh.3 sinhf.3 sqrt.3 cbrt.3 \ - sqrt.3 cbrtf.3 sqrt.3 sqrtf.3 tan.3 tanf.3 tanh.3 tanhf.3 + sqrt.3 cbrtf.3 sqrt.3 sqrtf.3 tan.3 tanf.3 tanh.3 tanhf.3 trunc.3 truncf.3 diff --git a/lib/libm/man/math.3 b/lib/libm/man/math.3 index 3256e443d1..70a892cb32 100644 --- a/lib/libm/man/math.3 +++ b/lib/libm/man/math.3 @@ -27,7 +27,7 @@ .\" .\" from: @(#)math.3 6.10 (Berkeley) 5/6/91 .\" $NetBSD: math.3,v 1.18 2003/12/03 23:31:21 jschauma Exp $ -.\" $DragonFly: src/lib/libm/man/math.3,v 1.1 2005/07/26 21:15:20 joerg Exp $ +.\" $DragonFly: src/lib/libm/man/math.3,v 1.2 2007/06/17 06:26:18 pavalos Exp $ .\" .TH MATH 3 "Dec 3, 2003" .UC 4 @@ -97,6 +97,7 @@ sinh sinh.3 hyperbolic function 3 sqrt sqrt.3 square root 1 tan tan.3 trigonometric function 3 tanh tanh.3 hyperbolic function 3 +trunc trunc.3 nearest integral value 3 y0 j0.3 Bessel function ??? y1 j0.3 Bessel function ??? yn j0.3 Bessel function ??? diff --git a/lib/libm/man/trunc.3 b/lib/libm/man/trunc.3 new file mode 100644 index 0000000000..bf6c8eb6a2 --- /dev/null +++ b/lib/libm/man/trunc.3 @@ -0,0 +1,79 @@ +.\" Copyright (c) 2004, 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. +.\" +.\" $NetBSD: trunc.3,v 1.5 2006/04/04 20:26:33 wiz Exp $ +.\" $FreeBSD: src/lib/msun/man/trunc.3,v 1.3 2005/06/15 19:04:04 ru Exp $ +.\" $DragonFly: src/lib/libm/man/trunc.3,v 1.1 2007/06/17 06:26:18 pavalos Exp $ +.\" +.Dd March 31, 2006 +.Dt TRUNC 3 +.Os +.Sh NAME +.Nm trunc , +.Nm truncf +.\" .Nm truncl +.Nd "nearest integral value with magnitude less than or equal to |x|" +.Sh LIBRARY +.Lb libm +.Sh SYNOPSIS +.In math.h +.Ft double +.Fn trunc "double x" +.Ft float +.Fn truncf "float x" +.\" .Ft "long double" +.\" .Fn truncl "long double x" +.Sh DESCRIPTION +The +.Fn trunc +and +.Fn truncf +.\" .Fn truncl +functions return the nearest integral value with magnitude less than +or equal to +.Pf | Fa x Ns | . +They are equivalent to +.Fn rint +and +.Fn rintf +.\" .Fn rintl +respectively, in the +.Dv FP_RZ +rounding mode. +.Sh SEE ALSO +.Xr ceil 3 , +.Xr floor 3 , +.Xr fpsetround 3 , +.Xr math 3 , +.Xr nextafter 3 , +.Xr rint 3 , +.Xr round 3 +.Sh STANDARDS +The +.Fn trunc +and +.Fn truncf +.\" .Fn truncl +functions conform to +.St -isoC-99 . diff --git a/lib/libm/src/Makefile.inc b/lib/libm/src/Makefile.inc index 505c6489b3..ea6b0849d3 100644 --- a/lib/libm/src/Makefile.inc +++ b/lib/libm/src/Makefile.inc @@ -1,11 +1,11 @@ -# $DragonFly: src/lib/libm/src/Makefile.inc,v 1.2 2007/06/16 22:26:53 pavalos Exp $ +# $DragonFly: src/lib/libm/src/Makefile.inc,v 1.3 2007/06/17 06:26:18 pavalos Exp $ .PATH: ${.CURDIR}/src # This files are always used. SRCS+= k_cos.c k_cosf.c k_rem_pio2.c k_rem_pio2f.c k_sin.c k_sinf.c \ k_tan.c k_tanf.c llrint.c llrintf.c llround.c \ - llroundf.c lrint.c lrintf.c lround.c lroundf.c w_cabs.c w_cabsf.c \ + llroundf.c lrint.c lrintf.c lround.c lroundf.c s_trunc.c s_truncf.c w_cabs.c w_cabsf.c \ w_drem.c w_dremf.c w_gamma.c w_gamma_r.c w_gammaf.c w_gammaf_r.c \ w_lgamma.c w_lgammaf.c diff --git a/lib/libm/src/s_trunc.c b/lib/libm/src/s_trunc.c new file mode 100644 index 0000000000..032e0807f3 --- /dev/null +++ b/lib/libm/src/s_trunc.c @@ -0,0 +1,61 @@ +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + * $NetBSD: s_trunc.c,v 1.2 2007/01/17 23:24:22 hubertf Exp $ + * $DragonFly: src/lib/libm/src/s_trunc.c,v 1.1 2007/06/17 06:26:18 pavalos Exp $ + */ + +/* + * trunc(x) + * Return x rounded toward 0 to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to trunc(x). + */ + +#include +#include "math_private.h" + +static const double huge = 1.0e300; + +double +trunc(double x) +{ + int32_t i0,i1,j00; + uint32_t i; + EXTRACT_WORDS(i0,i1,x); + j00 = ((i0>>20)&0x7ff)-0x3ff; + if(j00<20) { + if(j00<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */ + i0 &= 0x80000000U; + i1 = 0; + } + } else { + i = (0x000fffff)>>j00; + if(((i0&i)|i1)==0) return x; /* x is integral */ + if(huge+x>0.0) { /* raise inexact flag */ + i0 &= (~i); i1=0; + } + } + } else if (j00>51) { + if(j00==0x400) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } else { + i = ((u_int32_t)(0xffffffff))>>(j00-20); + if((i1&i)==0) return x; /* x is integral */ + if(huge+x>0.0) /* raise inexact flag */ + i1 &= (~i); + } + INSERT_WORDS(x,i0,i1); + return x; +} diff --git a/lib/libm/src/s_truncf.c b/lib/libm/src/s_truncf.c new file mode 100644 index 0000000000..eaa7f114d4 --- /dev/null +++ b/lib/libm/src/s_truncf.c @@ -0,0 +1,53 @@ +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + * $NetBSD: s_truncf.c,v 1.3 2007/01/17 23:24:22 hubertf Exp $ + * $DragonFly: src/lib/libm/src/s_truncf.c,v 1.1 2007/06/17 06:26:18 pavalos Exp $ + */ + +/* + * truncf(x) + * Return x rounded toward 0 to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to truncf(x). + */ + +#include +#include "math_private.h" + +static const float huge = 1.0e30F; + +float +truncf(float x) +{ + int32_t i0,j00; + uint32_t i; + GET_FLOAT_WORD(i0,x); + j00 = ((i0>>23)&0xff)-0x7f; + if(j00<23) { + if(j00<0) { /* raise inexact if x != 0 */ + if(huge+x>0.0F) /* |x|<1, so return 0*sign(x) */ + i0 &= 0x80000000; + } else { + i = (0x007fffff)>>j00; + if((i0&i)==0) return x; /* x is integral */ + if(huge+x>0.0F) /* raise inexact flag */ + i0 &= (~i); + } + } else { + if(j00==0x80) return x+x; /* inf or NaN */ + else return x; /* x is integral */ + } + SET_FLOAT_WORD(x,i0); + return x; +}