OSDN Git Service

2004-09-28 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / normalize.c
1 /* Nelper routines to convert from integer to real.
2    Copyright 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook.
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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.
16
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.  */
21 #include <math.h>
22 #include "libgfortran.h"
23
24 /* These routines can be sensitive to excess precision, so should really be
25    compiled with -ffloat-store.  */
26
27 /* Return the largest value less than one representable in a REAL*4.  */
28
29 static inline GFC_REAL_4
30 almostone_r4 ()
31 {
32 #ifdef HAVE_NEXTAFTERF
33   return nextafterf (1.0f, 0.0f);
34 #else
35   /* The volatile is a hack to prevent excess precision on x86.  */
36   static volatile GFC_REAL_4 val = 0.0f;
37   GFC_REAL_4 x;
38
39   if (val != 0.0f)
40     return val;
41
42   val = 0.9999f;
43   do
44     {
45       x = val;
46       val = (val + 1.0f) / 2.0f;
47     }
48   while (val > x && val < 1.0f);
49   if (val == 1.0f)
50     val = x;
51   return val;
52 #endif
53 }
54
55
56 /* Return the largest value less than one representable in a REAL*8.  */
57
58 static inline GFC_REAL_8
59 almostone_r8 ()
60 {
61 #ifdef HAVE_NEXTAFTER
62   return nextafter (1.0, 0.0);
63 #else
64   static volatile GFC_REAL_8 val = 0.0;
65   GFC_REAL_8 x;
66
67   if (val != 0.0)
68     return val;
69
70   val = 0.9999;
71   do
72     {
73       x = val;
74       val = (val + 1.0) / 2.0;
75     }
76   while (val > x && val < 1.0);
77   if (val == 1.0)
78     val = x;
79   return val;
80 #endif
81 }
82
83
84 /* Convert an unsigned integer in the range [0..x] into a
85    real the range [0..1).  */
86
87 GFC_REAL_4
88 normalize_r4_i4 (GFC_UINTEGER_4 i, GFC_UINTEGER_4 x)
89 {
90   GFC_REAL_4 r;
91
92   r = (GFC_REAL_4) i / (GFC_REAL_4) x;
93   if (r == 1.0f)
94     r = almostone_r4 ();
95   return r;
96 }
97
98
99 /* Convert an unsigned integer in the range [0..x] into a
100    real the range [0..1).  */
101
102 GFC_REAL_8
103 normalize_r8_i8 (GFC_UINTEGER_8 i, GFC_UINTEGER_8 x)
104 {
105   GFC_REAL_8 r;
106
107   r = (GFC_REAL_8) i / (GFC_REAL_8) x;
108   if (r == 1.0)
109     r = almostone_r8 ();
110   return r;
111 }