amath  1.8.5
Simple command line calculator
floor.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_floor.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 floor.c
43  * @brief Floor function
44  */
45 
46 #include "prim.h"
47 
48 static const double huge = 1.0e300;
49 
50 /**
51  * @brief Floor function
52  * @return x rounded toward -inf to integral value
53  * @details
54  * <pre>
55  * Method:
56  * Bit twiddling
57  *
58  * Exception:
59  * Inexact flag raised if x not equal to floor(x)
60  * </pre>
61  */
62 double floor(double x)
63 {
64  int32_t i0, i1, j0;
65  uint32_t i, j;
66  EXTRACT_WORDS(i0, i1, x);
67  j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
68  if (j0 < 20)
69  {
70  if (j0 < 0)
71  { /* raise inexact if x != 0 */
72  if (huge + x > 0.0)
73  { /* return 0*sign(x) if |x|<1 */
74  if (i0 >= 0)
75  {
76  i0 = i1 = 0;
77  }
78  else if (((i0 & 0x7fffffff) | i1) != 0)
79  {
80  i0 = 0xbff00000;
81  i1 = 0;
82  }
83  }
84  }
85  else
86  {
87  i = (0x000fffff) >> j0;
88  if (((i0 & i) | i1) == 0)
89  return x; /* x is integral */
90  if (huge + x > 0.0)
91  { /* raise inexact flag */
92  if (i0 < 0)
93  i0 += (0x00100000) >> j0;
94  i0 &= (~i);
95  i1 = 0;
96  }
97  }
98  }
99  else if (j0 > 51)
100  {
101  if (j0 == 0x400)
102  return x + x; /* inf or NaN */
103  else
104  return x; /* x is integral */
105  }
106  else
107  {
108  i = ((uint32_t)(0xffffffff)) >> (j0 - 20);
109  if ((i1 & i) == 0)
110  return x; /* x is integral */
111  if (huge + x > 0.0)
112  { /* raise inexact flag */
113  if (i0 < 0)
114  {
115  if (j0 == 20)
116  i0 += 1;
117  else
118  {
119  j = i1 + (1 << (52 - j0));
120  if (j < (uint32_t)i1)
121  i0 += 1; /* got a carry */
122  i1 = j;
123  }
124  }
125  i1 &= (~i);
126  }
127  }
128  INSERT_WORDS(x, i0, i1);
129  return x;
130 }
#define INSERT_WORDS(d, ix0, ix1)
Set a double from two 32 bit ints.
Definition: prim.h:187
static const double huge
Definition: floor.c:48
double floor(double x)
Floor function.
Definition: floor.c:62
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
Definition: prim.h:156