GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cmach-info.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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 <stdint.h>
31 
32 #include "cmach-info.h"
33 #include "f77-fcn.h"
34 
35 extern double F77_FUNC (d1mach, D1MACH) (const F77_INT*);
36 
37 typedef union
38 {
39  double d;
40  int32_t i[2];
42 
43 typedef struct
44 {
45  int fp_fmt;
46  equiv fp_par[4];
47 } float_params;
48 
49 #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
50  do \
51  { \
52  fp.fp_fmt = (fmt); \
53  fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \
54  fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
55  fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \
56  fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \
57  } \
58  while (0)
59 
60 static int equiv_compare (const equiv *std, const equiv *v, int len)
61 {
62  int i;
63  for (i = 0; i < len; i++)
64  if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
65  return 0;
66  return 1;
67 }
68 
69 // Return 0 if the floating point format is unknown, 1 if it is IEEE
70 // little endian, or 2 if it is IEEE big endian.
71 //
72 // If the return values change, you must also change the values of the
73 // float format enum in mach-info.h and the correspondence between the
74 // integer and enum values in octave::mach_info::get_float_format.
75 
76 int
78 {
79  int retval = 0;
80 
81  float_params fp[3];
82 
83  INIT_FLT_PAR (fp[0], 1,
84  0, 1048576,
85  -1, 2146435071,
86  0, 1017118720,
87  0, 1018167296);
88 
89  INIT_FLT_PAR (fp[1], 2,
90  1048576, 0,
91  2146435071, -1,
92  1017118720, 0,
93  1018167296, 0);
94 
95  INIT_FLT_PAR (fp[2], 0,
96  0, 0,
97  0, 0,
98  0, 0,
99  0, 0);
100 
101  equiv mach_fp_par[4];
102 
103  F77_INT opt;
104 
105  opt = 1;
106  mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (&opt);
107 
108  opt = 2;
109  mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (&opt);
110 
111  opt = 3;
112  mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (&opt);
113 
114  opt = 4;
115  mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (&opt);
116 
117  int i = 0;
118  do
119  {
120  if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
121  {
122  retval = fp[i].fp_fmt;
123  break;
124  }
125  }
126  while (fp[++i].fp_fmt != 0);
127 
128  return retval;
129 }
130 
131 int
133 {
134  // Are we little or big endian? From Harbison & Steele.
135 
136  union
137  {
138  long l;
139  char c[sizeof (long)];
140  } u;
141 
142  u.l = 1;
143 
144  return (u.c[sizeof (long) - 1] == 1);
145 }
equiv
Definition: cmach-info.c:41
int octave_is_big_endian(void)
Definition: cmach-info.c:132
#define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2)
Definition: cmach-info.c:49
double F77_FUNC(d1mach, D1MACH) const
Definition: cmach-info.c:35
int octave_get_float_format(void)
Definition: cmach-info.c:77
double precision function d1mach(i)
Definition: d1mach.f:23
octave_f77_int_type F77_INT
Definition: f77-fcn.h:306
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T len
Definition: xerbla.cc:61