1*f14fb602SLionel Sambuc /* $NetBSD: modf_ieee754.c,v 1.4 2012/03/22 13:25:45 he Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1994, 1995 Carnegie-Mellon University.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Author: Chris G. Demetriou
82fe8fb19SBen Gras *
92fe8fb19SBen Gras * Permission to use, copy, modify and distribute this software and
102fe8fb19SBen Gras * its documentation is hereby granted, provided that both the copyright
112fe8fb19SBen Gras * notice and this permission notice appear in all copies of the
122fe8fb19SBen Gras * software, derivative works or modified versions, and any portions
132fe8fb19SBen Gras * thereof, and that both notices appear in supporting documentation.
142fe8fb19SBen Gras *
152fe8fb19SBen Gras * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
162fe8fb19SBen Gras * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
172fe8fb19SBen Gras * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * Carnegie Mellon requests users of this software to return to
202fe8fb19SBen Gras *
212fe8fb19SBen Gras * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
222fe8fb19SBen Gras * School of Computer Science
232fe8fb19SBen Gras * Carnegie Mellon University
242fe8fb19SBen Gras * Pittsburgh PA 15213-3890
252fe8fb19SBen Gras *
262fe8fb19SBen Gras * any improvements or extensions that they make and grant Carnegie the
272fe8fb19SBen Gras * rights to redistribute these changes.
282fe8fb19SBen Gras */
292fe8fb19SBen Gras
302fe8fb19SBen Gras #include <sys/types.h>
312fe8fb19SBen Gras #include <machine/ieee.h>
322fe8fb19SBen Gras #include <errno.h>
332fe8fb19SBen Gras #include <math.h>
342fe8fb19SBen Gras
352fe8fb19SBen Gras /*
362fe8fb19SBen Gras * double modf(double val, double *iptr)
372fe8fb19SBen Gras * returns: f and i such that |f| < 1.0, (f + i) = val, and
382fe8fb19SBen Gras * sign(f) == sign(i) == sign(val).
392fe8fb19SBen Gras *
402fe8fb19SBen Gras * Beware signedness when doing subtraction, and also operand size!
412fe8fb19SBen Gras */
422fe8fb19SBen Gras double
modf(double val,double * iptr)432fe8fb19SBen Gras modf(double val, double *iptr)
442fe8fb19SBen Gras {
452fe8fb19SBen Gras union ieee_double_u u, v;
462fe8fb19SBen Gras u_int64_t frac;
472fe8fb19SBen Gras
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras * If input is +/-Inf or NaN, return +/-0 or NaN.
502fe8fb19SBen Gras */
512fe8fb19SBen Gras u.dblu_d = val;
522fe8fb19SBen Gras if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN) {
532fe8fb19SBen Gras *iptr = u.dblu_d;
542fe8fb19SBen Gras return (0.0 / u.dblu_d);
552fe8fb19SBen Gras }
562fe8fb19SBen Gras
572fe8fb19SBen Gras /*
582fe8fb19SBen Gras * If input can't have a fractional part, return
592fe8fb19SBen Gras * (appropriately signed) zero, and make i be the input.
602fe8fb19SBen Gras */
612fe8fb19SBen Gras if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
622fe8fb19SBen Gras *iptr = u.dblu_d;
632fe8fb19SBen Gras v.dblu_d = 0.0;
642fe8fb19SBen Gras v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
652fe8fb19SBen Gras return (v.dblu_d);
662fe8fb19SBen Gras }
672fe8fb19SBen Gras
682fe8fb19SBen Gras /*
692fe8fb19SBen Gras * If |input| < 1.0, return it, and set i to the appropriately
702fe8fb19SBen Gras * signed zero.
712fe8fb19SBen Gras */
722fe8fb19SBen Gras if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) {
732fe8fb19SBen Gras v.dblu_d = 0.0;
742fe8fb19SBen Gras v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
752fe8fb19SBen Gras *iptr = v.dblu_d;
762fe8fb19SBen Gras return (u.dblu_d);
772fe8fb19SBen Gras }
782fe8fb19SBen Gras
792fe8fb19SBen Gras /*
802fe8fb19SBen Gras * There can be a fractional part of the input.
812fe8fb19SBen Gras * If you look at the math involved for a few seconds, it's
822fe8fb19SBen Gras * plain to see that the integral part is the input, with the
832fe8fb19SBen Gras * low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
842fe8fb19SBen Gras * the fractional part is the part with the rest of the
852fe8fb19SBen Gras * bits zeroed. Just zeroing the high bits to get the
862fe8fb19SBen Gras * fractional part would yield a fraction in need of
872fe8fb19SBen Gras * normalization. Therefore, we take the easy way out, and
882fe8fb19SBen Gras * just use subtraction to get the fractional part.
892fe8fb19SBen Gras */
902fe8fb19SBen Gras v.dblu_d = u.dblu_d;
912fe8fb19SBen Gras /* Zero the low bits of the fraction, the sleazy way. */
922fe8fb19SBen Gras frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl;
932fe8fb19SBen Gras frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
942fe8fb19SBen Gras frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
95*f14fb602SLionel Sambuc v.dblu_dbl.dbl_fracl = (u_int) (frac & 0xffffffffULL);
96*f14fb602SLionel Sambuc v.dblu_dbl.dbl_frach = (u_int) (frac >> 32);
972fe8fb19SBen Gras *iptr = v.dblu_d;
982fe8fb19SBen Gras
992fe8fb19SBen Gras u.dblu_d -= v.dblu_d;
1002fe8fb19SBen Gras u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign;
1012fe8fb19SBen Gras return (u.dblu_d);
1022fe8fb19SBen Gras }
103