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