amath  1.6.2
Simple command line calculator
ceil.c
Go to the documentation of this file.
1 /* @(#)s_ceil.c 1.3 95/01/18 */
2 
3 /*
4  * Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The origin source code can be obtained from:
28  * http://www.netlib.org/fdlibm/s_ceil.c
29  *
30  */
31 
32 /*
33  * ====================================================
34  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
35  *
36  * Developed at SunSoft, a Sun Microsystems, Inc. business.
37  * Permission to use, copy, modify, and distribute this
38  * software is freely granted, provided that this notice
39  * is preserved.
40  * ====================================================
41  *
42  */
43 
44 #include "prim.h"
45 
46 static const double huge = 1.0e300;
47 
48 /**
49  * @brief Mathematical ceiling function.
50  * @version 1.3
51  * @date 95/01/18
52  * @details
53  * <pre>
54  * Return x rounded toward -inf to integral value
55  * Method:
56  * Bit twiddling.
57  * Exception:
58  * Inexact flag raised if x not equal to ceil(x).
59  * </pre>
60  * @copyright Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
61  * @license Developed at SunSoft, a Sun Microsystems, Inc. business. Permission
62  * to use, copy, modify, and distribute this software is freely granted,
63  * provided that this notice is preserved.
64  */
65 double ceil(double x)
66 {
67  sword i0,i1,j0;
68  uword i,j;
69  EXTRACT_WORDS(i0,i1,x);
70  j0 = ((i0>>20)&0x7ff)-0x3ff;
71  if(j0<20) {
72  if(j0<0) { /* raise inexact if x != 0 */
73  if(huge+x>0.0) { /* return 0*sign(x) if |x|<1 */
74  if(i0<0) {
75  i0=0x80000000;
76  i1=0;
77  }
78  else if((i0|i1)!=0) {
79  i0=0x3ff00000;
80  i1=0;
81  }
82  }
83  } else {
84  i = (0x000fffff)>>j0;
85  if(((i0&i)|i1)==0) return x; /* x is integral */
86  if(huge+x>0.0) { /* raise inexact flag */
87  if(i0>0) i0 += (0x00100000)>>j0;
88  i0 &= (~i);
89  i1=0;
90  }
91  }
92  } else if (j0>51) {
93  if(j0==0x400) return x+x; /* inf or NaN */
94  else return x; /* x is integral */
95  } else {
96  i = ((uword)(0xffffffff))>>(j0-20);
97  if((i1&i)==0) return x; /* x is integral */
98  if(huge+x>0.0) { /* raise inexact flag */
99  if(i0>0) {
100  if(j0==20) i0+=1;
101  else {
102  j = i1 + (1<<(52-j0));
103  // NOTICE: Is this a correct cast?
104  if((sword)j<(sword)i1) i0+=1; /* got a carry */
105  i1 = j;
106  }
107  }
108  i1 &= (~i);
109  }
110  }
111  INSERT_WORDS(x,i0,i1);
112  return x;
113 }
#define INSERT_WORDS(d, ix0, ix1)
Set a double from two 32 bit ints.
Definition: prim.h:187
static const double huge
Definition: ceil.c:46
double ceil(double x)
Mathematical ceiling function.
Definition: ceil.c:65
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
Definition: prim.h:153
signed int sword
32 bit signed integer.
Definition: prim.h:107
unsigned int uword
32 bit unsigned integer.
Definition: prim.h:101