12fe8fb19SBen Gras /* @(#)s_floor.c 5.1 93/09/24 */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * ====================================================
42fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
82fe8fb19SBen Gras * software is freely granted, provided that this notice
92fe8fb19SBen Gras * is preserved.
102fe8fb19SBen Gras * ====================================================
112fe8fb19SBen Gras */
122fe8fb19SBen Gras
132fe8fb19SBen Gras #include <sys/cdefs.h>
142fe8fb19SBen Gras #if 0
152fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/msun/src/s_trunc.c,v 1.1 2004/06/20 09:25:43 das Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
18*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_trunc.c,v 1.4 2013/11/13 12:58:11 joerg Exp $");
192fe8fb19SBen Gras #endif
202fe8fb19SBen Gras
212fe8fb19SBen Gras /*
222fe8fb19SBen Gras * trunc(x)
232fe8fb19SBen Gras * Return x rounded toward 0 to integral value
242fe8fb19SBen Gras * Method:
252fe8fb19SBen Gras * Bit twiddling.
262fe8fb19SBen Gras * Exception:
272fe8fb19SBen Gras * Inexact flag raised if x not equal to trunc(x).
282fe8fb19SBen Gras */
292fe8fb19SBen Gras
302fe8fb19SBen Gras #include "math.h"
312fe8fb19SBen Gras #include "math_private.h"
322fe8fb19SBen Gras
33*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
34*84d9c625SLionel Sambuc __strong_alias(_truncl, trunc)
35*84d9c625SLionel Sambuc __weak_alias(truncl, trunc)
36*84d9c625SLionel Sambuc #endif
37*84d9c625SLionel Sambuc
382fe8fb19SBen Gras static const double huge = 1.0e300;
392fe8fb19SBen Gras
402fe8fb19SBen Gras double
trunc(double x)412fe8fb19SBen Gras trunc(double x)
422fe8fb19SBen Gras {
432fe8fb19SBen Gras int32_t i0,i1,jj0;
442fe8fb19SBen Gras uint32_t i;
452fe8fb19SBen Gras EXTRACT_WORDS(i0,i1,x);
462fe8fb19SBen Gras jj0 = ((i0>>20)&0x7ff)-0x3ff;
472fe8fb19SBen Gras if(jj0<20) {
482fe8fb19SBen Gras if(jj0<0) { /* raise inexact if x != 0 */
492fe8fb19SBen Gras if(huge+x>0.0) {/* |x|<1, so return 0*sign(x) */
502fe8fb19SBen Gras i0 &= 0x80000000U;
512fe8fb19SBen Gras i1 = 0;
522fe8fb19SBen Gras }
532fe8fb19SBen Gras } else {
542fe8fb19SBen Gras i = (0x000fffff)>>jj0;
552fe8fb19SBen Gras if(((i0&i)|i1)==0) return x; /* x is integral */
562fe8fb19SBen Gras if(huge+x>0.0) { /* raise inexact flag */
572fe8fb19SBen Gras i0 &= (~i); i1=0;
582fe8fb19SBen Gras }
592fe8fb19SBen Gras }
602fe8fb19SBen Gras } else if (jj0>51) {
612fe8fb19SBen Gras if(jj0==0x400) return x+x; /* inf or NaN */
622fe8fb19SBen Gras else return x; /* x is integral */
632fe8fb19SBen Gras } else {
642fe8fb19SBen Gras i = ((u_int32_t)(0xffffffff))>>(jj0-20);
652fe8fb19SBen Gras if((i1&i)==0) return x; /* x is integral */
662fe8fb19SBen Gras if(huge+x>0.0) /* raise inexact flag */
672fe8fb19SBen Gras i1 &= (~i);
682fe8fb19SBen Gras }
692fe8fb19SBen Gras INSERT_WORDS(x,i0,i1);
702fe8fb19SBen Gras return x;
712fe8fb19SBen Gras }
72