1 /* Nelper routines to convert from integer to real.
2 Copyright 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook.
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Ligbfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "libgfortran.h"
24 /* These routines can be sensitive to excess precision, so should really be
25 compiled with -ffloat-store. */
27 /* Return the largest value less than one representable in a REAL*4. */
29 static inline GFC_REAL_4
32 #ifdef HAVE_NEXTAFTERF
33 return nextafterf (1.0f, 0.0f);
35 /* The volatile is a hack to prevent excess precision on x86. */
36 static volatile GFC_REAL_4 val = 0.0f;
46 val = (val + 1.0f) / 2.0f;
48 while (val > x && val < 1.0f);
56 /* Return the largest value less than one representable in a REAL*8. */
58 static inline GFC_REAL_8
62 return nextafter (1.0, 0.0);
64 static volatile GFC_REAL_8 val = 0.0;
74 val = (val + 1.0) / 2.0;
76 while (val > x && val < 1.0);
84 /* Convert an unsigned integer in the range [0..x] into a
85 real the range [0..1). */
88 normalize_r4_i4 (GFC_UINTEGER_4 i, GFC_UINTEGER_4 x)
92 r = (GFC_REAL_4) i / (GFC_REAL_4) x;
99 /* Convert an unsigned integer in the range [0..x] into a
100 real the range [0..1). */
103 normalize_r8_i8 (GFC_UINTEGER_8 i, GFC_UINTEGER_8 x)
107 r = (GFC_REAL_8) i / (GFC_REAL_8) x;