GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-ieee.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cmath>
31 #include <cstdlib>
32 
33 #include <limits>
34 
35 #include "lo-error.h"
36 #include "lo-ieee.h"
37 #include "mach-info.h"
38 
39 static double lo_inf;
40 static double lo_nan;
41 static double lo_na;
42 
43 static float lo_float_inf;
44 static float lo_float_nan;
45 static float lo_float_na;
46 
47 static int lo_ieee_hw;
48 static int lo_ieee_lw;
49 
50 #if defined (HAVE_MIPS_NAN)
51  #define LO_IEEE_NA_HW 0x7FF040F4
52 #else
53  #define LO_IEEE_NA_HW 0x7FF840F4
54 #endif
55 #define LO_IEEE_NA_LW 0x40000000
56 #define LO_IEEE_NA_FLOAT 0x7FC207A2
57 
58 int
60 {
62  t.value = x;
63  return (__lo_ieee_isnan (x) && t.word[lo_ieee_hw] == LO_IEEE_NA_HW
64  && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0;
65 }
66 
67 double
69 {
71 
72  return lo_inf;
73 }
74 
75 double
77 {
79 
80  return lo_na;
81 }
82 
83 double
85 {
87 
88  return lo_nan;
89 }
90 
91 int
93 {
94  lo_ieee_float t;
95  t.value = x;
96  return (__lo_ieee_float_isnan (x) && (t.word == LO_IEEE_NA_FLOAT)) ? 1 : 0;
97 }
98 
99 float
101 {
102  octave_ieee_init ();
103 
104  return lo_float_inf;
105 }
106 
107 float
109 {
110  octave_ieee_init ();
111 
112  return lo_float_na;
113 }
114 
115 float
117 {
118  octave_ieee_init ();
119 
120  return lo_float_nan;
121 }
122 
123 void
125 {
126  static bool initialized = false;
127 
128  if (! initialized)
129  {
131 
132  switch (ff)
133  {
136  {
137  lo_nan = std::numeric_limits<double>::quiet_NaN ();
138  lo_inf = std::numeric_limits<double>::infinity ();
139 
140  lo_float_nan = std::numeric_limits<float>::quiet_NaN ();
141  lo_float_inf = std::numeric_limits<float>::infinity ();
142 
143  // The following is patterned after code in R.
144 
146  {
147  lo_ieee_hw = 0;
148  lo_ieee_lw = 1;
149  }
150  else
151  {
152  lo_ieee_hw = 1;
153  lo_ieee_lw = 0;
154  }
155 
156  lo_ieee_double t;
159 
160  lo_na = t.value;
161 
162  lo_ieee_float tf;
163  tf.word = LO_IEEE_NA_FLOAT;
164 
165  lo_float_na = tf.value;
166  }
167  break;
168 
169  default:
170  // If the format is unknown, then you will probably not have a
171  // useful system, so we will abort here. Anyone wishing to
172  // experiment with building Octave on a system without IEEE
173  // floating point should be capable of removing this check and
174  // the configure test.
175  //
176  // If the error handler returns, then we'll abort.
177 
178  (*current_liboctave_error_handler)
179  ("lo_ieee_init: floating point format is not IEEE! Maybe DLAMCH is miscompiled, or you are using some strange system without IEEE floating point math?");
180 
181  abort ();
182  }
183 
184  initialized = true;
185  }
186 }
double lo_ieee_inf_value(void)
Definition: lo-ieee.cc:68
int __lo_ieee_float_is_NA(float x)
Definition: lo-ieee.cc:92
double lo_ieee_na_value(void)
Definition: lo-ieee.cc:76
float lo_ieee_float_inf_value(void)
Definition: lo-ieee.cc:100
static int lo_ieee_hw
Definition: lo-ieee.cc:47
void octave_ieee_init(void)
Definition: lo-ieee.cc:124
static float lo_float_nan
Definition: lo-ieee.cc:44
static float lo_float_inf
Definition: lo-ieee.cc:43
static double lo_na
Definition: lo-ieee.cc:41
static float lo_float_na
Definition: lo-ieee.cc:45
static int lo_ieee_lw
Definition: lo-ieee.cc:48
int __lo_ieee_is_NA(double x)
Definition: lo-ieee.cc:59
double lo_ieee_nan_value(void)
Definition: lo-ieee.cc:84
static double lo_inf
Definition: lo-ieee.cc:39
float lo_ieee_float_na_value(void)
Definition: lo-ieee.cc:108
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:116
#define LO_IEEE_NA_HW
Definition: lo-ieee.cc:53
#define LO_IEEE_NA_LW
Definition: lo-ieee.cc:55
#define LO_IEEE_NA_FLOAT
Definition: lo-ieee.cc:56
static double lo_nan
Definition: lo-ieee.cc:40
int __lo_ieee_float_isnan(float x)
Definition: lo-ieee.h:84
int __lo_ieee_isnan(double x)
Definition: lo-ieee.h:72
F77_RET_T const F77_DBLE * x
float_format native_float_format(void)
Definition: mach-info.cc:65
float_format
Definition: mach-info.h:38
@ flt_fmt_ieee_big_endian
Definition: mach-info.h:44
@ flt_fmt_ieee_little_endian
Definition: mach-info.h:43
double value
Definition: lo-ieee.h:60
unsigned int word[2]
Definition: lo-ieee.h:61
float value
Definition: lo-ieee.h:66
unsigned int word
Definition: lo-ieee.h:67