xref: /openbsd/lib/libc/gen/modf.c (revision f78f1c7c)
1*f78f1c7cSmiod /*	$OpenBSD: modf.c,v 1.7 2023/08/13 06:55:37 miod Exp $	*/
2aeb694e3Smartynas /*	$NetBSD: modf.c,v 1.1 1995/02/10 17:50:25 cgd Exp $	*/
3aeb694e3Smartynas 
4aeb694e3Smartynas /*
5aeb694e3Smartynas  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
6aeb694e3Smartynas  * All rights reserved.
7aeb694e3Smartynas  *
8aeb694e3Smartynas  * Author: Chris G. Demetriou
9aeb694e3Smartynas  *
10aeb694e3Smartynas  * Permission to use, copy, modify and distribute this software and
11aeb694e3Smartynas  * its documentation is hereby granted, provided that both the copyright
12aeb694e3Smartynas  * notice and this permission notice appear in all copies of the
13aeb694e3Smartynas  * software, derivative works or modified versions, and any portions
14aeb694e3Smartynas  * thereof, and that both notices appear in supporting documentation.
15aeb694e3Smartynas  *
16aeb694e3Smartynas  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17aeb694e3Smartynas  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18aeb694e3Smartynas  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19aeb694e3Smartynas  *
20aeb694e3Smartynas  * Carnegie Mellon requests users of this software to return to
21aeb694e3Smartynas  *
22aeb694e3Smartynas  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23aeb694e3Smartynas  *  School of Computer Science
24aeb694e3Smartynas  *  Carnegie Mellon University
25aeb694e3Smartynas  *  Pittsburgh PA 15213-3890
26aeb694e3Smartynas  *
27aeb694e3Smartynas  * any improvements or extensions that they make and grant Carnegie the
28aeb694e3Smartynas  * rights to redistribute these changes.
29aeb694e3Smartynas  */
30aeb694e3Smartynas 
31aeb694e3Smartynas #include <sys/types.h>
32aeb694e3Smartynas #include <machine/ieee.h>
33aeb694e3Smartynas #include <errno.h>
34ca462540Smartynas #include <float.h>
35aeb694e3Smartynas #include <math.h>
36aeb694e3Smartynas 
37aeb694e3Smartynas /*
38aeb694e3Smartynas  * double modf(double val, double *iptr)
39aeb694e3Smartynas  * returns: f and i such that |f| < 1.0, (f + i) = val, and
40aeb694e3Smartynas  *	sign(f) == sign(i) == sign(val).
41aeb694e3Smartynas  *
42aeb694e3Smartynas  * Beware signedness when doing subtraction, and also operand size!
43aeb694e3Smartynas  */
44aeb694e3Smartynas double
modf(double val,double * iptr)45aeb694e3Smartynas modf(double val, double *iptr)
46aeb694e3Smartynas {
47aeb694e3Smartynas 	union doub {
48aeb694e3Smartynas 		double v;
49aeb694e3Smartynas 		struct ieee_double s;
50aeb694e3Smartynas 	} u, v;
51aeb694e3Smartynas 	u_int64_t frac;
52aeb694e3Smartynas 
53aeb694e3Smartynas 	/*
54*f78f1c7cSmiod 	 * If input is +/-Inf or NaN, return +/-0 or NaN.
55aeb694e3Smartynas 	 */
56aeb694e3Smartynas 	u.v = val;
57*f78f1c7cSmiod 	if (u.s.dbl_exp == DBL_EXP_INFNAN) {
58*f78f1c7cSmiod 		*iptr = u.v;
59*f78f1c7cSmiod 		return (0.0 / u.v);
60*f78f1c7cSmiod 	}
61aeb694e3Smartynas 
62aeb694e3Smartynas 	/*
63aeb694e3Smartynas 	 * If input can't have a fractional part, return
64aeb694e3Smartynas 	 * (appropriately signed) zero, and make i be the input.
65aeb694e3Smartynas 	 */
66aeb694e3Smartynas 	if ((int)u.s.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
67aeb694e3Smartynas 		*iptr = u.v;
68aeb694e3Smartynas 		v.v = 0.0;
69aeb694e3Smartynas 		v.s.dbl_sign = u.s.dbl_sign;
70aeb694e3Smartynas 		return (v.v);
71aeb694e3Smartynas 	}
72aeb694e3Smartynas 
73aeb694e3Smartynas 	/*
74aeb694e3Smartynas 	 * If |input| < 1.0, return it, and set i to the appropriately
75aeb694e3Smartynas 	 * signed zero.
76aeb694e3Smartynas 	 */
77aeb694e3Smartynas 	if (u.s.dbl_exp < DBL_EXP_BIAS) {
78aeb694e3Smartynas 		v.v = 0.0;
79aeb694e3Smartynas 		v.s.dbl_sign = u.s.dbl_sign;
80aeb694e3Smartynas 		*iptr = v.v;
81aeb694e3Smartynas 		return (u.v);
82aeb694e3Smartynas 	}
83aeb694e3Smartynas 
84aeb694e3Smartynas 	/*
85aeb694e3Smartynas 	 * There can be a fractional part of the input.
86aeb694e3Smartynas 	 * If you look at the math involved for a few seconds, it's
87aeb694e3Smartynas 	 * plain to see that the integral part is the input, with the
88aeb694e3Smartynas 	 * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
89aeb694e3Smartynas 	 * the fractional part is the part with the rest of the
90aeb694e3Smartynas 	 * bits zeroed.  Just zeroing the high bits to get the
91aeb694e3Smartynas 	 * fractional part would yield a fraction in need of
92aeb694e3Smartynas 	 * normalization.  Therefore, we take the easy way out, and
93aeb694e3Smartynas 	 * just use subtraction to get the fractional part.
94aeb694e3Smartynas 	 */
95aeb694e3Smartynas 	v.v = u.v;
96aeb694e3Smartynas 	/* Zero the low bits of the fraction, the sleazy way. */
97aeb694e3Smartynas 	frac = ((u_int64_t)v.s.dbl_frach << 32) + v.s.dbl_fracl;
98aeb694e3Smartynas 	frac >>= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
99aeb694e3Smartynas 	frac <<= DBL_FRACBITS - (u.s.dbl_exp - DBL_EXP_BIAS);
100aeb694e3Smartynas 	v.s.dbl_fracl = frac & 0xffffffff;
101aeb694e3Smartynas 	v.s.dbl_frach = frac >> 32;
102aeb694e3Smartynas 	*iptr = v.v;
103aeb694e3Smartynas 
104aeb694e3Smartynas 	u.v -= v.v;
105aeb694e3Smartynas 	u.s.dbl_sign = v.s.dbl_sign;
106aeb694e3Smartynas 	return (u.v);
107aeb694e3Smartynas }
108ca462540Smartynas 
10938ffb667Sespie #if	LDBL_MANT_DIG == DBL_MANT_DIG
1102fbf033eSmartynas __strong_alias(modfl, modf);
11138ffb667Sespie #endif	/* LDBL_MANT_DIG == DBL_MANT_DIG */
112