1147191Sjkoshy/* @(#)s_ceil.c 5.1 93/09/24 */ 2147191Sjkoshy/* 3147191Sjkoshy * ==================================================== 4147191Sjkoshy * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5147191Sjkoshy * 6147191Sjkoshy * Developed at SunPro, a Sun Microsystems, Inc. business. 7147191Sjkoshy * Permission to use, copy, modify, and distribute this 8147191Sjkoshy * software is freely granted, provided that this notice 9147191Sjkoshy * is preserved. 10147191Sjkoshy * ==================================================== 11147191Sjkoshy */ 12147191Sjkoshy 13147191Sjkoshy#include <sys/cdefs.h> 14147191Sjkoshy__FBSDID("$FreeBSD$"); 15147191Sjkoshy 16147191Sjkoshy/* 17147191Sjkoshy * ceil(x) 18147191Sjkoshy * Return x rounded toward -inf to integral value 19147191Sjkoshy * Method: 20147191Sjkoshy * Bit twiddling. 21147191Sjkoshy * Exception: 22147191Sjkoshy * Inexact flag raised if x not equal to ceil(x). 23147191Sjkoshy */ 24147191Sjkoshy 25147191Sjkoshy#include <float.h> 26147191Sjkoshy 27147191Sjkoshy#include "math.h" 28147191Sjkoshy#include "math_private.h" 29147191Sjkoshy 30147191Sjkoshystatic const double huge = 1.0e300; 31147191Sjkoshy 32147191Sjkoshydouble 33147191Sjkoshyceil(double x) 34147191Sjkoshy{ 35147191Sjkoshy int32_t i0,i1,j0; 36147191Sjkoshy u_int32_t i,j; 37147191Sjkoshy EXTRACT_WORDS(i0,i1,x); 38147191Sjkoshy j0 = ((i0>>20)&0x7ff)-0x3ff; 39147191Sjkoshy if(j0<20) { 40147191Sjkoshy if(j0<0) { /* raise inexact if x != 0 */ 41147191Sjkoshy if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ 42147191Sjkoshy if(i0<0) {i0=0x80000000;i1=0;} 43147191Sjkoshy else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} 44147191Sjkoshy } 45147191Sjkoshy } else { 46147191Sjkoshy i = (0x000fffff)>>j0; 47184802Sjkoshy if(((i0&i)|i1)==0) return x; /* x is integral */ 48147191Sjkoshy if(huge+x>0.0) { /* raise inexact flag */ 49147191Sjkoshy if(i0>0) i0 += (0x00100000)>>j0; 50147191Sjkoshy i0 &= (~i); i1=0; 51147191Sjkoshy } 52147191Sjkoshy } 53147191Sjkoshy } else if (j0>51) { 54147191Sjkoshy if(j0==0x400) return x+x; /* inf or NaN */ 55147191Sjkoshy else return x; /* x is integral */ 56147191Sjkoshy } else { 57147191Sjkoshy i = ((u_int32_t)(0xffffffff))>>(j0-20); 58147191Sjkoshy if((i1&i)==0) return x; /* x is integral */ 59147191Sjkoshy if(huge+x>0.0) { /* raise inexact flag */ 60147191Sjkoshy if(i0>0) { 61147191Sjkoshy if(j0==20) i0+=1; 62147191Sjkoshy else { 63147191Sjkoshy j = i1 + (1<<(52-j0)); 64147191Sjkoshy if(j<i1) i0+=1; /* got a carry */ 65147191Sjkoshy i1 = j; 66147191Sjkoshy } 67147191Sjkoshy } 68147191Sjkoshy i1 &= (~i); 69147191Sjkoshy } 70147191Sjkoshy } 71147191Sjkoshy INSERT_WORDS(x,i0,i1); 72147191Sjkoshy return x; 73147191Sjkoshy} 74147191Sjkoshy 75147191Sjkoshy#if LDBL_MANT_DIG == 53 76147191Sjkoshy__weak_reference(ceil, ceill); 77147191Sjkoshy#endif 78147191Sjkoshy