1b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project
2b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project/* @(#)s_ceil.c 1.3 95/01/18 */
3b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project/*
4b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * ====================================================
5b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project *
7b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Developed at SunSoft, a Sun Microsystems, Inc. business.
8b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Permission to use, copy, modify, and distribute this
9b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * software is freely granted, provided that this notice
10b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * is preserved.
11b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * ====================================================
12b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project */
13b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project
14b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project/*
15b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * ieee_ceil(x)
16b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Return x rounded toward -inf to integral value
17b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Method:
18b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project *	Bit twiddling.
19b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project * Exception:
20b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project *	Inexact flag raised if x not equal to ieee_ceil(x).
21b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project */
22b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project
23b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#include "fdlibm.h"
24b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project
25b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#ifdef __STDC__
26b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Projectstatic const double huge = 1.0e300;
27b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#else
28b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Projectstatic double huge = 1.0e300;
29b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#endif
30b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project
31b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#ifdef __STDC__
32b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	double ieee_ceil(double x)
33b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#else
34b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	double ieee_ceil(x)
35b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	double x;
36b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project#endif
37b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project{
38b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	int i0,i1,j0;
39b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	unsigned i,j;
40b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	i0 =  __HI(x);
41b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	i1 =  __LO(x);
42b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	j0 = ((i0>>20)&0x7ff)-0x3ff;
43b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	if(j0<20) {
44b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    if(j0<0) { 	/* raise inexact if x != 0 */
45b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
46b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    if(i0<0) {i0=0x80000000;i1=0;}
47b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
48b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		}
49b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    } else {
50b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		i = (0x000fffff)>>j0;
51b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		if(((i0&i)|i1)==0) return x; /* x is integral */
52b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		if(huge+x>0.0) {	/* raise inexact flag */
53b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    if(i0>0) i0 += (0x00100000)>>j0;
54b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    i0 &= (~i); i1=0;
55b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		}
56b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    }
57b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	} else if (j0>51) {
58b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    if(j0==0x400) return x+x;	/* inf or NaN */
59b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    else return x;		/* x is integral */
60b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	} else {
61b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    i = ((unsigned)(0xffffffff))>>(j0-20);
62b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    if((i1&i)==0) return x;	/* x is integral */
63b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    if(huge+x>0.0) { 		/* raise inexact flag */
64b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		if(i0>0) {
65b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    if(j0==20) i0+=1;
66b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    else {
67b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project			j = i1 + (1<<(52-j0));
68b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project			if(j<i1) i0+=1;	/* got a carry */
69b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project			i1 = j;
70b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		    }
71b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		}
72b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project		i1 &= (~i);
73b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	    }
74b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	}
75b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	__HI(x) = i0;
76b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	__LO(x) = i1;
77b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project	return x;
78b07e1d9fd8d9e4e03698e0bd9bf77154c5390326The Android Open Source Project}
79