| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: n_floor.c,v 1.8 2014/03/16 09:51:39 martin Exp $ */ | | 1 | /* $NetBSD: n_floor.c,v 1.9 2024/05/08 02:08:11 riastradh Exp $ */ |
2 | /* | | 2 | /* |
3 | * Copyright (c) 1985, 1993 | | 3 | * Copyright (c) 1985, 1993 |
4 | * The Regents of the University of California. All rights reserved. | | 4 | * The Regents of the University of California. All rights reserved. |
5 | * | | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | | 6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions | | 7 | * modification, are permitted provided that the following conditions |
8 | * are met: | | 8 | * are met: |
9 | * 1. Redistributions of source code must retain the above copyright | | 9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. | | 10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright | | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the | | 12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. | | 13 | * documentation and/or other materials provided with the distribution. |
14 | * 3. Neither the name of the University nor the names of its contributors | | 14 | * 3. Neither the name of the University nor the names of its contributors |
| @@ -35,31 +35,30 @@ static char sccsid[] = "@(#)floor.c 8.1 | | | @@ -35,31 +35,30 @@ static char sccsid[] = "@(#)floor.c 8.1 |
35 | #endif /* not lint */ | | 35 | #endif /* not lint */ |
36 | | | 36 | |
37 | #define _LIBM_STATIC | | 37 | #define _LIBM_STATIC |
38 | #include "mathimpl.h" | | 38 | #include "mathimpl.h" |
39 | | | 39 | |
40 | vc(L, 4503599627370496.0E0 ,0000,5c00,0000,0000, 55, 1.0) /* 2**55 */ | | 40 | vc(L, 4503599627370496.0E0 ,0000,5c00,0000,0000, 55, 1.0) /* 2**55 */ |
41 | | | 41 | |
42 | ic(L, 4503599627370496.0E0, 52, 1.0) /* 2**52 */ | | 42 | ic(L, 4503599627370496.0E0, 52, 1.0) /* 2**52 */ |
43 | | | 43 | |
44 | #ifdef vccast | | 44 | #ifdef vccast |
45 | #define L vccast(L) | | 45 | #define L vccast(L) |
46 | #endif | | 46 | #endif |
47 | | | 47 | |
48 | #ifdef __weak_alias | | 48 | __weak_alias(ceill, ceil) |
49 | __weak_alias(ceill, ceil); | | 49 | __weak_alias(floorl, floor) |
50 | __weak_alias(floorl, floor); | | 50 | __weak_alias(truncl, trunc) |
51 | __weak_alias(truncl, trunc); | | 51 | __weak_alias(rintl, rint) |
52 | #endif | | | |
53 | | | 52 | |
54 | /* | | 53 | /* |
55 | * floor(x) := the largest integer no larger than x; | | 54 | * floor(x) := the largest integer no larger than x; |
56 | * ceil(x) := -floor(-x), for all real x. | | 55 | * ceil(x) := -floor(-x), for all real x. |
57 | * | | 56 | * |
58 | * Note: Inexact will be signaled if x is not an integer, as is | | 57 | * Note: Inexact will be signaled if x is not an integer, as is |
59 | * customary for IEEE 754. No other signal can be emitted. | | 58 | * customary for IEEE 754. No other signal can be emitted. |
60 | */ | | 59 | */ |
61 | double | | 60 | double |
62 | floor(double x) | | 61 | floor(double x) |
63 | { | | 62 | { |
64 | volatile double y; | | 63 | volatile double y; |
65 | | | 64 | |
| @@ -100,27 +99,26 @@ ceil(double x) | | | @@ -100,27 +99,26 @@ ceil(double x) |
100 | else { /* now 0 <= x < L */ | | 99 | else { /* now 0 <= x < L */ |
101 | y = L+x; /* destructive store must be forced */ | | 100 | y = L+x; /* destructive store must be forced */ |
102 | y -= L; /* an integer, and |x-y| < 1 */ | | 101 | y -= L; /* an integer, and |x-y| < 1 */ |
103 | return x > y ? y+(double)1 : y; | | 102 | return x > y ? y+(double)1 : y; |
104 | } | | 103 | } |
105 | } | | 104 | } |
106 | | | 105 | |
107 | float | | 106 | float |
108 | ceilf(float x) | | 107 | ceilf(float x) |
109 | { | | 108 | { |
110 | return ceil((double)x); | | 109 | return ceil((double)x); |
111 | } | | 110 | } |
112 | | | 111 | |
113 | #ifndef ns32000 /* rint() is in ./NATIONAL/support.s */ | | | |
114 | /* | | 112 | /* |
115 | * algorithm for rint(x) in pseudo-pascal form ... | | 113 | * algorithm for rint(x) in pseudo-pascal form ... |
116 | * | | 114 | * |
117 | * real rint(x): real x; | | 115 | * real rint(x): real x; |
118 | * ... delivers integer nearest x in direction of prevailing rounding | | 116 | * ... delivers integer nearest x in direction of prevailing rounding |
119 | * ... mode | | 117 | * ... mode |
120 | * const L = (last consecutive integer)/2 | | 118 | * const L = (last consecutive integer)/2 |
121 | * = 2**55; for VAX D | | 119 | * = 2**55; for VAX D |
122 | * = 2**52; for IEEE 754 Double | | 120 | * = 2**52; for IEEE 754 Double |
123 | * real s,t; | | 121 | * real s,t; |
124 | * begin | | 122 | * begin |
125 | * if x != x then return x; ... NaN | | 123 | * if x != x then return x; ... NaN |
126 | * if |x| >= L then return x; ... already an integer | | 124 | * if |x| >= L then return x; ... already an integer |
| @@ -139,27 +137,32 @@ rint(double x) | | | @@ -139,27 +137,32 @@ rint(double x) |
139 | volatile double t; | | 137 | volatile double t; |
140 | const double one = 1.0; | | 138 | const double one = 1.0; |
141 | | | 139 | |
142 | #if !defined(__vax__)&&!defined(tahoe) | | 140 | #if !defined(__vax__)&&!defined(tahoe) |
143 | if (x != x) /* NaN */ | | 141 | if (x != x) /* NaN */ |
144 | return (x); | | 142 | return (x); |
145 | #endif /* !defined(__vax__)&&!defined(tahoe) */ | | 143 | #endif /* !defined(__vax__)&&!defined(tahoe) */ |
146 | if (copysign(x,one) >= L) /* already an integer */ | | 144 | if (copysign(x,one) >= L) /* already an integer */ |
147 | return (x); | | 145 | return (x); |
148 | s = copysign(L,x); | | 146 | s = copysign(L,x); |
149 | t = x + s; /* x+s rounded to integer */ | | 147 | t = x + s; /* x+s rounded to integer */ |
150 | return (t - s); | | 148 | return (t - s); |
151 | } | | 149 | } |
152 | #endif /* not national */ | | 150 | |
| | | 151 | float |
| | | 152 | rintf(float x) |
| | | 153 | { |
| | | 154 | return rint((double)x); |
| | | 155 | } |
153 | | | 156 | |
154 | long | | 157 | long |
155 | lrint(double x) | | 158 | lrint(double x) |
156 | { | | 159 | { |
157 | double s; | | 160 | double s; |
158 | volatile double t; | | 161 | volatile double t; |
159 | const double one = 1.0; | | 162 | const double one = 1.0; |
160 | | | 163 | |
161 | #if !defined(__vax__)&&!defined(tahoe) | | 164 | #if !defined(__vax__)&&!defined(tahoe) |
162 | if (x != x) /* NaN */ | | 165 | if (x != x) /* NaN */ |
163 | return (x); | | 166 | return (x); |
164 | #endif /* !defined(__vax__)&&!defined(tahoe) */ | | 167 | #endif /* !defined(__vax__)&&!defined(tahoe) */ |
165 | if (copysign(x,one) >= L) /* already an integer */ | | 168 | if (copysign(x,one) >= L) /* already an integer */ |