GNU Octave  4.4.1
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-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <stdint.h>
28 
29 #include "cmach-info.h"
30 #include "f77-fcn.h"
31 
32 extern double F77_FUNC (d1mach, D1MACH) (const F77_INT*);
33 
34 typedef union
35 {
36  double d;
37  int32_t i[2];
39 
40 typedef struct
41 {
42  int fp_fmt;
43  equiv fp_par[4];
44 } float_params;
45 
46 #define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
47  do \
48  { \
49  fp.fp_fmt = (fmt); \
50  fp.fp_par[0].i[0] = (sm1); fp.fp_par[0].i[1] = (sm2); \
51  fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
52  fp.fp_par[2].i[0] = (rt1); fp.fp_par[2].i[1] = (rt2); \
53  fp.fp_par[3].i[0] = (dv1); fp.fp_par[3].i[1] = (dv2); \
54  } \
55  while (0)
56 
57 static int equiv_compare (const equiv *std, const equiv *v, int len)
58 {
59  int i;
60  for (i = 0; i < len; i++)
61  if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
62  return 0;
63  return 1;
64 }
65 
66 // Return 0 if the floating point format is unknown, 1 if it is IEEE
67 // little endian, or 2 if it is IEEE big endian.
68 //
69 // If the return values change, you must also change the values of the
70 // float format enum in mach-info.h and the correspondence between the
71 // integer and enum values in octave::mach_info::get_float_format.
72 
73 int
75 {
76  int retval = 0;
77 
78  float_params fp[3];
79 
80  INIT_FLT_PAR (fp[0], 1,
81  0, 1048576,
82  -1, 2146435071,
83  0, 1017118720,
84  0, 1018167296);
85 
86  INIT_FLT_PAR (fp[1], 2,
87  1048576, 0,
88  2146435071, -1,
89  1017118720, 0,
90  1018167296, 0);
91 
92  INIT_FLT_PAR (fp[2], 0,
93  0, 0,
94  0, 0,
95  0, 0,
96  0, 0);
97 
98  equiv mach_fp_par[4];
99 
100  F77_INT opt;
101 
102  opt = 1;
103  mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (&opt);
104 
105  opt = 2;
106  mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (&opt);
107 
108  opt = 3;
109  mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (&opt);
110 
111  opt = 4;
112  mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (&opt);
113 
114  int i = 0;
115  do
116  {
117  if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
118  {
119  retval = fp[i].fp_fmt;
120  break;
121  }
122  }
123  while (fp[++i].fp_fmt != 0);
124 
125  return retval;
126 }
127 
128 int
130 {
131  // Are we little or big endian? From Harbison & Steele.
132 
133  union
134  {
135  long l;
136  char c[sizeof (long)];
137  } u;
138 
139  u.l = 1;
140 
141  return (u.c[sizeof (long) - 1] == 1);
142 }
double F77_FUNC(d1mach, D1MACH) const
Definition: cmach-info.c:32
double precision function d1mach(i)
Definition: d1mach.f:20
STL namespace.
static int equiv_compare(const equiv *std, const equiv *v, int len)
Definition: cmach-info.c:57
equiv
Definition: cmach-info.c:38
u
Definition: lu.cc:138
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
octave_value retval
Definition: data.cc:6246
int octave_get_float_format(void)
Definition: cmach-info.c:74
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
int octave_is_big_endian(void)
Definition: cmach-info.c:129
#define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2)
Definition: cmach-info.c:46
for i
Definition: data.cc:5264