s_ceil.c revision 97409
1204635Sgnn/* @(#)s_ceil.c 5.1 93/09/24 */ 2204635Sgnn/* 3204635Sgnn * ==================================================== 4204635Sgnn * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5204635Sgnn * 6204635Sgnn * Developed at SunPro, a Sun Microsystems, Inc. business. 7204635Sgnn * Permission to use, copy, modify, and distribute this 8204635Sgnn * software is freely granted, provided that this notice 9204635Sgnn * is preserved. 10204635Sgnn * ==================================================== 11204635Sgnn */ 12204635Sgnn 13204635Sgnn#ifndef lint 14204635Sgnnstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_ceil.c 97409 2002-05-28 17:51:46Z alfred $"; 15204635Sgnn#endif 16204635Sgnn 17204635Sgnn/* 18204635Sgnn * ceil(x) 19204635Sgnn * Return x rounded toward -inf to integral value 20204635Sgnn * Method: 21204635Sgnn * Bit twiddling. 22204635Sgnn * Exception: 23204635Sgnn * Inexact flag raised if x not equal to ceil(x). 24204635Sgnn */ 25204635Sgnn 26204635Sgnn#include "math.h" 27204635Sgnn#include "math_private.h" 28204635Sgnn 29204635Sgnnstatic const double huge = 1.0e300; 30204635Sgnn 31204635Sgnn double __generic_ceil(double x) 32204635Sgnn{ 33204635Sgnn int32_t i0,i1,j0; 34204635Sgnn u_int32_t i,j; 35204635Sgnn EXTRACT_WORDS(i0,i1,x); 36204635Sgnn j0 = ((i0>>20)&0x7ff)-0x3ff; 37204635Sgnn if(j0<20) { 38204635Sgnn if(j0<0) { /* raise inexact if x != 0 */ 39204635Sgnn if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */ 40233319Sgonzo if(i0<0) {i0=0x80000000;i1=0;} 41233319Sgonzo else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;} 42233319Sgonzo } 43233319Sgonzo } else { 44233319Sgonzo i = (0x000fffff)>>j0; 45204635Sgnn if(((i0&i)|i1)==0) return x; /* x is integral */ 46233319Sgonzo if(huge+x>0.0) { /* raise inexact flag */ 47233319Sgonzo if(i0>0) i0 += (0x00100000)>>j0; 48233319Sgonzo i0 &= (~i); i1=0; 49233319Sgonzo } 50233319Sgonzo } 51233319Sgonzo } else if (j0>51) { 52233319Sgonzo if(j0==0x400) return x+x; /* inf or NaN */ 53204635Sgnn else return x; /* x is integral */ 54233319Sgonzo } else { 55204635Sgnn i = ((u_int32_t)(0xffffffff))>>(j0-20); 56233319Sgonzo if((i1&i)==0) return x; /* x is integral */ 57233319Sgonzo if(huge+x>0.0) { /* raise inexact flag */ 58233319Sgonzo if(i0>0) { 59233319Sgonzo if(j0==20) i0+=1; 60233319Sgonzo else { 61233319Sgonzo j = i1 + (1<<(52-j0)); 62233319Sgonzo if(j<i1) i0+=1; /* got a carry */ 63233319Sgonzo i1 = j; 64233319Sgonzo } 65233319Sgonzo } 66233319Sgonzo i1 &= (~i); 67233319Sgonzo } 68233319Sgonzo } 69233319Sgonzo INSERT_WORDS(x,i0,i1); 70233319Sgonzo return x; 71233319Sgonzo} 72233319Sgonzo