* intrinsics/string_intrinsics.c (string_verify): Fix off by one
[official-gcc.git] / libgfortran / intrinsics / c99_functions.c
blob8198e0093eea03aa347fdb1a999a843234746f3c
1 /* Implementation of various C99 functions
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgfortran; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include <sys/types.h>
23 #include <float.h>
24 #include <math.h>
25 #include "libgfortran.h"
28 #ifndef HAVE_ACOSF
29 float
30 acosf(float x)
32 return (float) acos(x);
34 #endif
36 #ifndef HAVE_ASINF
37 float
38 asinf(float x)
40 return (float) asin(x);
42 #endif
44 #ifndef HAVE_ATAN2F
45 float
46 atan2f(float y, float x)
48 return (float) atan2(y, x);
50 #endif
52 #ifndef HAVE_ATANF
53 float
54 atanf(float x)
56 return (float) atan(x);
58 #endif
60 #ifndef HAVE_CEILF
61 float
62 ceilf(float x)
64 return (float) ceil(x);
66 #endif
68 #ifndef HAVE_COPYSIGNF
69 float
70 copysignf(float x, float y)
72 return (float) copysign(x, y);
74 #endif
76 #ifndef HAVE_COSF
77 float
78 cosf(float x)
80 return (float) cos(x);
82 #endif
84 #ifndef HAVE_COSHF
85 float
86 coshf(float x)
88 return (float) cosh(x);
90 #endif
92 #ifndef HAVE_EXPF
93 float
94 expf(float x)
96 return (float) exp(x);
98 #endif
100 #ifndef HAVE_FLOORF
101 float
102 floorf(float x)
104 return (float) floor(x);
106 #endif
108 #ifndef HAVE_FREXPF
109 float
110 frexpf(float x, int *exp)
112 return (float) frexp(x, exp);
114 #endif
116 #ifndef HAVE_HYPOTF
117 float
118 hypotf(float x, float y)
120 return (float) hypot(x, y);
122 #endif
124 #ifndef HAVE_LOGF
125 float
126 logf(float x)
128 return (float) log(x);
130 #endif
132 #ifndef HAVE_LOG10F
133 float
134 log10f(float x)
136 return (float) log10(x);
138 #endif
140 #ifndef HAVE_SCALBNF
141 float
142 scalbnf(float x, int y)
144 return (float) scalbn(x, y);
146 #endif
148 #ifndef HAVE_SINF
149 float
150 sinf(float x)
152 return (float) sin(x);
154 #endif
156 #ifndef HAVE_SINHF
157 float
158 sinhf(float x)
160 return (float) sinh(x);
162 #endif
164 #ifndef HAVE_SQRTF
165 float
166 sqrtf(float x)
168 return (float) sqrt(x);
170 #endif
172 #ifndef HAVE_TANF
173 float
174 tanf(float x)
176 return (float) tan(x);
178 #endif
180 #ifndef HAVE_TANHF
181 float
182 tanhf(float x)
184 return (float) tanh(x);
186 #endif
188 #ifndef HAVE_NEXTAFTERF
189 /* This is a portable implementation of nextafterf that is intended to be
190 independent of the floating point format or its in memory representation.
191 This implementation works correctly with denormalized values. */
192 float
193 nextafterf(float x, float y)
195 /* This variable is marked volatile to avoid excess precision problems
196 on some platforms, including IA-32. */
197 volatile float delta;
198 float absx, denorm_min;
200 if (isnan(x) || isnan(y))
201 return x + y;
202 if (x == y)
203 return x;
205 /* absx = fabsf (x); */
206 absx = (x < 0.0) ? -x : x;
208 /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */
209 if (__FLT_DENORM_MIN__ == 0.0f)
210 denorm_min = __FLT_MIN__;
211 else
212 denorm_min = __FLT_DENORM_MIN__;
214 if (absx < __FLT_MIN__)
215 delta = denorm_min;
216 else
218 float frac;
219 int exp;
221 /* Discard the fraction from x. */
222 frac = frexpf (absx, &exp);
223 delta = scalbnf (0.5f, exp);
225 /* Scale x by the epsilon of the representation. By rights we should
226 have been able to combine this with scalbnf, but some targets don't
227 get that correct with denormals. */
228 delta *= __FLT_EPSILON__;
230 /* If we're going to be reducing the absolute value of X, and doing so
231 would reduce the exponent of X, then the delta to be applied is
232 one exponent smaller. */
233 if (frac == 0.5f && (y < x) == (x > 0))
234 delta *= 0.5f;
236 /* If that underflows to zero, then we're back to the minimum. */
237 if (delta == 0.0f)
238 delta = denorm_min;
241 if (y < x)
242 delta = -delta;
244 return x + delta;
246 #endif
249 #ifndef HAVE_POWF
250 float
251 powf(float x, float y)
253 return (float) pow(x, y);
255 #endif
257 /* Note that if HAVE_FPCLASSIFY is not defined, then NaN is not handled */
259 /* Algorithm by Steven G. Kargl. */
261 #ifndef HAVE_ROUND
262 /* Round to nearest integral value. If the argument is halfway between two
263 integral values then round away from zero. */
265 double
266 round(double x)
268 double t;
269 #ifdef HAVE_FPCLASSIFY
270 int i;
271 i = fpclassify(x);
272 if (i == FP_INFINITE || i == FP_NAN)
273 return (x);
274 #endif
276 if (x >= 0.0)
278 t = ceil(x);
279 if (t - x > 0.5)
280 t -= 1.0;
281 return (t);
283 else
285 t = ceil(-x);
286 if (t + x > 0.5)
287 t -= 1.0;
288 return (-t);
291 #endif
293 #ifndef HAVE_ROUNDF
294 /* Round to nearest integral value. If the argument is halfway between two
295 integral values then round away from zero. */
297 float
298 roundf(float x)
300 float t;
301 #ifdef HAVE_FPCLASSIFY
302 int i;
304 i = fpclassify(x);
305 if (i == FP_INFINITE || i == FP_NAN)
306 return (x);
307 #endif
309 if (x >= 0.0)
311 t = ceilf(x);
312 if (t - x > 0.5)
313 t -= 1.0;
314 return (t);
316 else
318 t = ceilf(-x);
319 if (t + x > 0.5)
320 t -= 1.0;
321 return (-t);
324 #endif