12fe8fb19SBen Gras /* @(#)s_ceil.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 defined(LIBM_SCCS) && !defined(lint)
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_ceil.c,v 1.14 2013/11/11 23:57:34 joerg Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras
182fe8fb19SBen Gras /*
192fe8fb19SBen Gras * ceil(x)
202fe8fb19SBen Gras * Return x rounded toward -inf to integral value
212fe8fb19SBen Gras * Method:
222fe8fb19SBen Gras * Bit twiddling.
232fe8fb19SBen Gras * Exception:
242fe8fb19SBen Gras * Inexact flag raised if x not equal to ceil(x).
252fe8fb19SBen Gras */
262fe8fb19SBen Gras
272fe8fb19SBen Gras #include "math.h"
282fe8fb19SBen Gras #include "math_private.h"
292fe8fb19SBen Gras
302fe8fb19SBen Gras static const double huge = 1.0e300;
312fe8fb19SBen Gras
32*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
__strong_alias(_ceill,ceil)33*84d9c625SLionel Sambuc __strong_alias(_ceill, ceil)
34*84d9c625SLionel Sambuc __weak_alias(ceill, ceil)
35*84d9c625SLionel Sambuc #endif
36*84d9c625SLionel Sambuc
372fe8fb19SBen Gras double
382fe8fb19SBen Gras ceil(double x)
392fe8fb19SBen Gras {
402fe8fb19SBen Gras int32_t i0,i1,jj0;
412fe8fb19SBen Gras u_int32_t i,j;
422fe8fb19SBen Gras EXTRACT_WORDS(i0,i1,x);
432fe8fb19SBen Gras jj0 = ((i0>>20)&0x7ff)-0x3ff;
442fe8fb19SBen Gras if(jj0<20) {
452fe8fb19SBen Gras if(jj0<0) { /* raise inexact if x != 0 */
462fe8fb19SBen Gras if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
472fe8fb19SBen Gras if(i0<0) {i0=0x80000000;i1=0;}
482fe8fb19SBen Gras else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
492fe8fb19SBen Gras }
502fe8fb19SBen Gras } else {
512fe8fb19SBen Gras i = (0x000fffff)>>jj0;
522fe8fb19SBen Gras if(((i0&i)|i1)==0) return x; /* x is integral */
532fe8fb19SBen Gras if(huge+x>0.0) { /* raise inexact flag */
542fe8fb19SBen Gras if(i0>0) i0 += (0x00100000)>>jj0;
552fe8fb19SBen Gras i0 &= (~i); i1=0;
562fe8fb19SBen Gras }
572fe8fb19SBen Gras }
582fe8fb19SBen Gras } else if (jj0>51) {
592fe8fb19SBen Gras if(jj0==0x400) return x+x; /* inf or NaN */
602fe8fb19SBen Gras else return x; /* x is integral */
612fe8fb19SBen Gras } else {
622fe8fb19SBen Gras i = ((u_int32_t)(0xffffffff))>>(jj0-20);
632fe8fb19SBen Gras if((i1&i)==0) return x; /* x is integral */
642fe8fb19SBen Gras if(huge+x>0.0) { /* raise inexact flag */
652fe8fb19SBen Gras if(i0>0) {
662fe8fb19SBen Gras if(jj0==20) i0+=1;
672fe8fb19SBen Gras else {
682fe8fb19SBen Gras j = i1 + (1<<(52-jj0));
692fe8fb19SBen Gras if(j<(u_int32_t)i1) i0+=1; /* got a carry */
702fe8fb19SBen Gras i1 = j;
712fe8fb19SBen Gras }
722fe8fb19SBen Gras }
732fe8fb19SBen Gras i1 &= (~i);
742fe8fb19SBen Gras }
752fe8fb19SBen Gras }
762fe8fb19SBen Gras INSERT_WORDS(x,i0,i1);
772fe8fb19SBen Gras return x;
782fe8fb19SBen Gras }
79