GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mach-info.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "f77-fcn.h"
28 #include "lo-error.h"
29 #include "mach-info.h"
30 #include "singleton-cleanup.h"
31 
32 extern "C"
33 {
34  double F77_FUNC (d1mach, D1MACH) (const octave_idx_type&);
35 }
36 
38 
39 union equiv
40 {
41  double d;
42  int i[2];
43 };
44 
45 struct
47 {
49  equiv fp_par[4];
50 };
51 
52 #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
53  do \
54  { \
55  fp.fp_fmt = (fmt); \
56  fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \
57  fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
58  fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \
59  fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \
60  } \
61  while (0)
62 
63 static int
64 equiv_compare (const equiv *std, const equiv *v, int len)
65 {
66  int i;
67  for (i = 0; i < len; i++)
68  if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
69  return 0;
70  return 1;
71 }
72 
75 {
77 
78  float_params fp[5];
79 
81  1048576, 0,
82  2146435071, -1,
83  1017118720, 0,
84  1018167296, 0);
85 
87  0, 1048576,
88  -1, 2146435071,
89  0, 1017118720,
90  0, 1018167296);
91 
93  0, 0,
94  0, 0,
95  0, 0,
96  0, 0);
97 
98  equiv mach_fp_par[4];
99 
100  mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (1);
101  mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (2);
102  mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (3);
103  mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (4);
104 
105  int i = 0;
106  do
107  {
108  if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
109  {
110  retval = fp[i].fp_fmt;
111  break;
112  }
113  }
114  while (fp[++i].fp_fmt != oct_mach_info::flt_fmt_unknown);
115 
116  return retval;
117 }
118 
119 static bool
121 {
122  // Are we little or big endian? From Harbison & Steele.
123 
124  union
125  {
126  long l;
127  char c[sizeof (long)];
128  } u;
129 
130  u.l = 1;
131 
132  return (u.c[sizeof (long) - 1] == 1);
133 }
134 
136  : native_float_fmt (get_float_format ()),
137  big_chief (ten_little_endians ()) { }
138 
139 bool
141 {
142  bool retval = true;
143 
144  if (! instance)
145  {
146  instance = new oct_mach_info ();
147 
148  if (instance)
150  }
151 
152  if (! instance)
153  {
154  (*current_liboctave_error_handler)
155  ("unable to create command history object!");
156 
157  retval = false;
158  }
159 
160  return retval;
161 }
162 
165 {
166  return (instance_ok ())
168 }
169 
170 bool
172 {
173  return (instance_ok ())
174  ? instance->big_chief : false;
175 }
176 
177 bool
179 {
180  return (instance_ok ())
181  ? (! instance->big_chief) : false;
182 }
183 
186 {
188 
189  if (s == "native" || s == "n")
191  else if (s == "ieee-be" || s == "b")
193  else if (s == "ieee-le" || s == "l")
195  else if (s == "unknown")
197  else
199  ("invalid architecture type specified");
200 
201  return retval;
202 }
203 
204 std::string
206 {
207  std::string retval = "unknown";
208 
209  switch (flt_fmt)
210  {
212  retval = "ieee-be";
213  break;
214 
216  retval = "ieee-le";
217  break;
218 
219  default:
220  break;
221  }
222 
223  return retval;
224 }
double F77_FUNC(d1mach, D1MACH)(const octave_idx_type &)
int i[2]
Definition: mach-info.cc:42
static std::string float_format_as_string(float_format)
Definition: mach-info.cc:205
static oct_mach_info * instance
Definition: mach-info.h:59
float_format native_float_fmt
Definition: mach-info.h:64
double precision function d1mach(i)
Definition: d1mach.f:1
STL namespace.
static float_format string_to_float_format(const std::string &)
Definition: mach-info.cc:185
liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
static void add(fptr f)
double d
Definition: mach-info.cc:41
static bool words_little_endian(void)
Definition: mach-info.cc:178
oct_mach_info(void)
Definition: mach-info.cc:135
static bool words_big_endian(void)
Definition: mach-info.cc:171
static bool instance_ok(void)
Definition: mach-info.cc:140
static float_format native_float_format(void)
Definition: mach-info.cc:164
static oct_mach_info::float_format get_float_format(void)
Definition: mach-info.cc:74
bool big_chief
Definition: mach-info.h:67
#define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2)
Definition: mach-info.cc:52
static void cleanup_instance(void)
Definition: mach-info.h:61
oct_mach_info::float_format fp_fmt
Definition: mach-info.cc:48
static bool ten_little_endians(void)
Definition: mach-info.cc:120
static int equiv_compare(const equiv *std, const equiv *v, int len)
Definition: mach-info.cc:64