diff options
author | 2011-07-08 22:28:32 +0000 | |
---|---|---|
committer | 2011-07-08 22:28:32 +0000 | |
commit | aeb694e3afa554db8dd44ee8140d036724efada5 (patch) | |
tree | 27134d6d85c1d25448c6bbdb4cc7222c925034fe /lib/libc/gen | |
parent | Don't accidentally mask off the T_USER bit before calling trap(). (diff) | |
download | wireguard-openbsd-aeb694e3afa554db8dd44ee8140d036724efada5.tar.xz wireguard-openbsd-aeb694e3afa554db8dd44ee8140d036724efada5.zip |
Revert (leaving the complex math part alone). Some stuff is depending
on this historical behavior; so we're stuck in this stupid situation.
No cookie for me.
Diffstat (limited to 'lib/libc/gen')
-rw-r--r-- | lib/libc/gen/Makefile.inc | 13 | ||||
-rw-r--r-- | lib/libc/gen/frexp.3 | 95 | ||||
-rw-r--r-- | lib/libc/gen/frexp.c | 79 | ||||
-rw-r--r-- | lib/libc/gen/modf.3 | 76 | ||||
-rw-r--r-- | lib/libc/gen/modf.c | 104 |
5 files changed, 362 insertions, 5 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 03165e1a5b6..28dc6e4dce0 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.51 2011/07/08 19:21:42 martynas Exp $ +# $OpenBSD: Makefile.inc,v 1.52 2011/07/08 22:28:33 martynas Exp $ # gen sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/gen ${LIBCSRCDIR}/gen @@ -6,7 +6,7 @@ SRCS+= alarm.c assert.c auth_subr.c authenticate.c \ basename.c clock.c closedir.c confstr.c ctermid.c ctype_.c \ daemon.c devname.c dirname.c disklabel.c elf_hash.c err.c \ - errx.c errlist.c errno.c exec.c fnmatch.c fpclassify.c \ + errx.c errlist.c errno.c exec.c fnmatch.c fpclassify.c frexp.c \ fstab.c ftok.c fts.c ftw.c getbsize.c getcap.c getcwd.c \ getdomainname.c getgrent.c getgrouplist.c gethostname.c \ getloadavg.c getlogin.c getmntinfo.c getnetgrent.c getpagesize.c \ @@ -30,7 +30,8 @@ SRCS+= _sys_errlist.c _sys_nerr.c _sys_siglist.c # machine-dependent gen sources # m-d Makefile.inc must include sources for: -# _setjmp() infinity ldexp() nan setjmp() sigsetjmp() +# _setjmp() fabs() frexp() infinity ldexp() modf() nan +# setjmp() sigsetjmp() .include "${LIBCSRCDIR}/arch/${MACHINE_CPU}/gen/Makefile.inc" @@ -48,7 +49,7 @@ errlst.o errlst.po: MAN+= alarm.3 auth_subr.3 authenticate.3 basename.3 clock.3 confstr.3 \ ctermid.3 ctype.3 daemon.3 devname.3 directory.3 dirname.3 err.3 \ - exec.3 fnmatch.3 fpclassify.3 fpgetmask.3 ftok.3 fts.3 ftw.3 \ + exec.3 fnmatch.3 fpclassify.3 fpgetmask.3 frexp.3 ftok.3 fts.3 ftw.3 \ getbsize.3 getcap.3 getcwd.3 getdomainname.3 getdiskbyname.3 \ getfsent.3 getgrent.3 getgrouplist.3 gethostname.3 getloadavg.3 \ getmntinfo.3 getnetgrent.3 getpagesize.3 getpass.3 getpwent.3 \ @@ -56,7 +57,7 @@ MAN+= alarm.3 auth_subr.3 authenticate.3 basename.3 clock.3 confstr.3 \ getttyent.3 getusershell.3 glob.3 initgroups.3 isalnum.3 isalpha.3 \ isascii.3 isblank.3 iscntrl.3 isdigit.3 isfdtype.3 isgraph.3 \ isgreater.3 islower.3 isprint.3 ispunct.3 isspace.3 isupper.3 \ - isxdigit.3 ldexp.3 lockf.3 login_cap.3 nice.3 nlist.3 pause.3 \ + isxdigit.3 ldexp.3 lockf.3 login_cap.3 modf.3 nice.3 nlist.3 pause.3 \ popen.3 psignal.3 pw_dup.3 pwcache.3 raise.3 readpassphrase.3 \ scandir.3 setjmp.3 setmode.3 setproctitle.3 siginterrupt.3 signal.3 \ sigsetops.3 sleep.3 statvfs.3 sysconf.3 sysctl.3 strtofflags.3 \ @@ -91,6 +92,7 @@ MLINKS+=fpclassify.3 finite.3 fpclassify.3 finitef.3 fpclassify.3 isfinite.3 \ MLINKS+=fpgetmask.3 fpgetround.3 fpgetmask.3 fpgetsticky.3 \ fpgetmask.3 fpsetmask.3 fpgetmask.3 fpsetround.3 \ fpgetmask.3 fpsetsticky.3 +MLINKS+=frexp.3 frexpf.3 frexp.3 frexpl.3 MLINKS+=fts.3 fts_open.3 fts.3 fts_read.3 fts.3 fts_children.3 \ fts.3 fts_set.3 fts.3 fts_close.3 MLINKS+=ftw.3 nftw.3 @@ -122,6 +124,7 @@ MLINKS+=login_cap.3 login_getclass.3 login_cap.3 login_getstyle.3 login_cap.3 \ login_getcapsize.3 login_cap.3 login_getcapstr.3 login_cap.3 \ login_getcaptime.3 login_cap.3 login_close.3 login_cap.3 \ secure_path.3 login_cap.3 setclasscontext.3 login_cap.3 setusercontext.3 +MLINKS+=modf.3 modff.3 MLINKS+=popen.3 pclose.3 MLINKS+=psignal.3 sys_siglist.3 psignal.3 sys_signame.3 MLINKS+=pwcache.3 user_from_uid.3 pwcache.3 group_from_gid.3 diff --git a/lib/libc/gen/frexp.3 b/lib/libc/gen/frexp.3 new file mode 100644 index 00000000000..2ba0daa8ce2 --- /dev/null +++ b/lib/libc/gen/frexp.3 @@ -0,0 +1,95 @@ +.\" $OpenBSD: frexp.3,v 1.12 2011/07/08 22:28:33 martynas Exp $ +.\" +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.Dd $Mdocdate: July 8 2011 $ +.Dt FREXP 3 +.Os +.Sh NAME +.Nm frexp , +.Nm frexpf , +.Nm frexpl +.Nd convert floating-point number to fractional and integral components +.Sh SYNOPSIS +.Fd #include <math.h> +.Ft double +.Fn frexp "double value" "int *exp" +.Ft float +.Fn frexpf "float value" "int *exp" +.Ft long double +.Fn frexpl "long double value" "int *exp" +.Sh DESCRIPTION +The +.Fn frexp +function breaks a floating-point number into a normalized +fraction and an integral power of 2. +It stores the integer in the +.Li int +object pointed to by +.Fa exp . +The +.Fn frexpf +function is a single precision version of +.Fn frexp . +The +.Fn frexpl +function is an extended precision version of +.Fn frexp . +.Sh RETURN VALUES +The +.Fn frexp , +.Fn frexpf +and +.Fn frexpl +functions return the value +.Li x , +such that +.Li x +is a +.Li double +with magnitude in the interval [1/2,\ 1) or zero, and +.Fa value +equals +.Li x +times 2 raised to the power +.Fa *exp . +If +.Fa value +is zero, both parts of the result are zero. +.Sh SEE ALSO +.Xr ldexp 3 , +.Xr modf 3 +.Sh STANDARDS +The +.Fn frexp +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/frexp.c b/lib/libc/gen/frexp.c new file mode 100644 index 00000000000..97cc624035a --- /dev/null +++ b/lib/libc/gen/frexp.c @@ -0,0 +1,79 @@ +/* $OpenBSD: frexp.c,v 1.7 2011/07/08 22:28:33 martynas Exp $ */ + +/*- + * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG> + * 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: frexp.c,v 1.1 2004/07/18 21:23:39 das Exp $ + */ + +/* LINTLIBRARY */ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <machine/ieee.h> +#include <float.h> +#include <math.h> + +double +frexp(double v, int *ex) +{ + union { + double v; + struct ieee_double s; + } u; + + u.v = v; + switch (u.s.dbl_exp) { + case 0: /* 0 or subnormal */ + if ((u.s.dbl_fracl | u.s.dbl_frach) == 0) { + *ex = 0; + } else { + /* + * The power of 2 is arbitrary, any value from 54 to + * 1024 will do. + */ + u.v *= 0x1.0p514; + *ex = u.s.dbl_exp - (DBL_EXP_BIAS - 1 + 514); + u.s.dbl_exp = DBL_EXP_BIAS - 1; + } + break; + case DBL_EXP_INFNAN: /* Inf or NaN; value of *ex is unspecified */ + break; + default: /* normal */ + *ex = u.s.dbl_exp - (DBL_EXP_BIAS - 1); + u.s.dbl_exp = DBL_EXP_BIAS - 1; + break; + } + return (u.v); +} + +#if LDBL_MANT_DIG == 53 +#ifdef lint +/* PROTOLIB1 */ +long double frexpl(long double, int *); +#else /* lint */ +__weak_alias(frexpl, frexp); +#endif /* lint */ +#endif /* LDBL_MANT_DIG == 53 */ diff --git a/lib/libc/gen/modf.3 b/lib/libc/gen/modf.3 new file mode 100644 index 00000000000..aa5434b5d17 --- /dev/null +++ b/lib/libc/gen/modf.3 @@ -0,0 +1,76 @@ +.\" $OpenBSD: modf.3,v 1.10 2011/07/08 22:28:33 martynas Exp $ +.\" +.\" Copyright (c) 1991, 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the American National Standards Committee X3, on Information +.\" Processing Systems. +.\" +.\" 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. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. +.\" +.Dd $Mdocdate: July 8 2011 $ +.Dt MODF 3 +.Os +.Sh NAME +.Nm modf , +.Nm modff +.Nd extract signed integral and fractional values from floating-point number +.Sh SYNOPSIS +.Fd #include <math.h> +.Ft double +.Fn modf "double value" "double *iptr" +.Ft float +.Fn modff "float value" "float *iptr" +.Sh DESCRIPTION +The +.Fn modf +function breaks the argument +.Fa value +into integral and fractional parts, each of which has the +same sign as the argument. +It stores the integral part as a +.Li double +in the object pointed to by +.Fa iptr . +The +.Fn modff +function is a single precision version of +.Fn modf . +.Sh RETURN VALUES +The +.Fn modf +and +.Fn modff +functions return the signed fractional part of +.Fa value . +.Sh SEE ALSO +.Xr frexp 3 , +.Xr ldexp 3 +.Sh STANDARDS +The +.Fn modf +function conforms to +.St -ansiC . diff --git a/lib/libc/gen/modf.c b/lib/libc/gen/modf.c new file mode 100644 index 00000000000..24effd38045 --- /dev/null +++ b/lib/libc/gen/modf.c @@ -0,0 +1,104 @@ +/* $OpenBSD: modf.c,v 1.3 2011/07/08 22:28:33 martynas Exp $ */ +/* $NetBSD: modf.c,v 1.1 1995/02/10 17:50:25 cgd Exp $ */ + +/* + * Copyright (c) 1994, 1995 Carnegie-Mellon University. + * All rights reserved. + * + * Author: Chris G. Demetriou + * + * Permission to use, copy, modify and distribute this software and + * its documentation is hereby granted, provided that both the copyright + * notice and this permission notice appear in all copies of the + * software, derivative works or modified versions, and any portions + * thereof, and that both notices appear in supporting documentation. + * + * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" + * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND + * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. + * + * Carnegie Mellon requests users of this software to return to + * + * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU + * School of Computer Science + * Carnegie Mellon University + * Pittsburgh PA 15213-3890 + * + * any improvements or extensions that they make and grant Carnegie the + * rights to redistribute these changes. + */ + +#include <sys/types.h> +#include <machine/ieee.h> +#include <errno.h> +#include <math.h> + +/* + * double modf(double val, double *iptr) + * returns: f and i such that |f| < 1.0, (f + i) = val, and + * sign(f) == sign(i) == sign(val). + * + * Beware signedness when doing subtraction, and also operand size! + */ +double +modf(double val, double *iptr) +{ + union doub { + double v; + struct ieee_double s; + } u, v; + u_int64_t frac; + + /* + * If input is Inf or NaN, return it and leave i alone. + */ + u.v = val; + if (u.s.dbl_exp == DBL_EXP_INFNAN) + return (u.v); + + /* + * If input can't have a fractional part, return + * (appropriately signed) zero, and make i be the input. + */ + if ((int)u.s.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) { + *iptr = u.v; + v.v = 0.0; + v.s.dbl_sign = u.s.dbl_sign; + return (v.v); + } + + /* + * If |input| < 1.0, return it, and set i to the appropriately + * signed zero. + */ + if (u.s.dbl_exp < DBL_EXP_BIAS) { + v.v = 0.0; + v.s.dbl_sign = u.s.dbl_sign; + *iptr = v.v; + return (u.v); + } + + /* + * There can be a fractional part of the input. + * If you look at the math involved for a few seconds, it's + * plain to see that the integral part is the input, with the + * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed, + * the fractional part is the part with the rest of the + * bits zeroed. Just zeroing the high bits to get the + * fractional part would yield a fraction in need of + * normalization. Therefore, we take the easy way out, and + * just use subtraction to get the fractional part. + */ + v.v = u.v; + /* Zero the low bits of the fraction, the sleazy way. */ + frac = ((u_int64_t)v.s.dbl_frach << 32) + v.s.dbl_fracl; + frac >>= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS); + frac <<= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS); + v.s.dbl_fracl = frac & 0xffffffff; + v.s.dbl_frach = frac >> 32; + *iptr = v.v; + + u.v -= v.v; + u.s.dbl_sign = v.s.dbl_sign; + return (u.v); +} |