File: mach-info.cc

package info (click to toggle)
octave 2.0.16-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 26,276 kB
  • ctags: 16,450
  • sloc: cpp: 67,548; fortran: 41,514; ansic: 26,682; sh: 7,361; makefile: 4,077; lex: 2,008; yacc: 1,849; lisp: 1,702; perl: 1,676; exp: 123
file content (243 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*

Copyright (C) 1996 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "f77-fcn.h"
#include "lo-error.h"
#include "mach-info.h"

extern "C"
{
  double F77_FCN (d1mach, D1MACH) (const int&);
}

oct_mach_info *oct_mach_info::instance = 0;

union equiv
{
  double d;
  int i[2];
};

struct
float_params
{
  oct_mach_info::float_format fp_fmt;
  equiv fp_par[4];
};

#define INIT_FLT_PAR(fp, fmt, sm1, sm2, lrg1, lrg2, rt1, rt2, dv1, dv2) \
  do \
    { \
      fp.fp_fmt = (fmt); \
      fp.fp_par[0].i[0] = (sm1);  fp.fp_par[0].i[1] = (sm2); \
      fp.fp_par[1].i[0] = (lrg1); fp.fp_par[1].i[1] = (lrg2); \
      fp.fp_par[2].i[0] = (rt1);  fp.fp_par[2].i[1] = (rt2); \
      fp.fp_par[3].i[0] = (dv1);  fp.fp_par[3].i[1] = (dv2); \
    } \
  while (0)

static int
equiv_compare (const equiv *std, const equiv *v, int len)
{
  int i;
  for (i = 0; i < len; i++)
    if (v[i].i[0] != std[i].i[0] || v[i].i[1] != std[i].i[1])
      return 0;
  return 1;
}

void
oct_mach_info::init_float_format (void)
{
  float_params fp[5];

  INIT_FLT_PAR (fp[0], oct_mach_info::ieee_big_endian,
		   1048576,  0,
		2146435071, -1,
		1017118720,  0,
		1018167296,  0);

  INIT_FLT_PAR (fp[1], oct_mach_info::ieee_little_endian,
		 0,    1048576,
		-1, 2146435071,
		 0, 1017118720,
		 0, 1018167296);

  INIT_FLT_PAR (fp[2], oct_mach_info::vax_d,
		   128,  0,
		-32769, -1,
		  9344,  0,
		  9344,  0);

  INIT_FLT_PAR (fp[3], oct_mach_info::vax_g,
		    16,  0,
		-32769, -1,
		 15552,  0,
		 15552,  0);

  INIT_FLT_PAR (fp[4], oct_mach_info::unknown,
		0, 0,
		0, 0,
		0, 0,
		0, 0);

  equiv mach_fp_par[4];

  mach_fp_par[0].d = F77_FCN (d1mach, D1MACH) (1);
  mach_fp_par[1].d = F77_FCN (d1mach, D1MACH) (2);
  mach_fp_par[2].d = F77_FCN (d1mach, D1MACH) (3);
  mach_fp_par[3].d = F77_FCN (d1mach, D1MACH) (4);

  int i = 0;
  do
    {
      if (equiv_compare (fp[i].fp_par, mach_fp_par, 4))
	{
	  native_float_fmt = fp[i].fp_fmt;
	  break;
	}
    }
  while (fp[++i].fp_fmt != oct_mach_info::unknown);
}

void
oct_mach_info::ten_little_endians (void)
{
  // Are we little or big endian?  From Harbison & Steele.

  union
  {
    long l;
    char c[sizeof (long)];
  } u;

  u.l = 1;

  big_chief = (u.c[sizeof (long) - 1] == 1);
}

oct_mach_info::oct_mach_info (void)
{
  init_float_format ();
  ten_little_endians ();
}

oct_mach_info::float_format
oct_mach_info::native_float_format (void)
{
  if (! instance)
    instance = new oct_mach_info ();

  return instance->native_float_fmt;
}

bool
oct_mach_info::words_big_endian (void)
{
  if (! instance)
    instance = new oct_mach_info ();

  return instance->big_chief;
}

bool
oct_mach_info::words_little_endian (void)
{
  if (! instance)
    instance = new oct_mach_info ();

  return ! instance->big_chief;
}

oct_mach_info::float_format
oct_mach_info::string_to_float_format (const string& s)
{
  oct_mach_info::float_format retval = oct_mach_info::unknown;

  if (s == "native" || s == "n")
    retval = oct_mach_info::native;
  else if (s == "ieee-be" || s == "b")
    retval = oct_mach_info::ieee_big_endian;
  else if (s == "ieee-le" || s == "l")
    retval = oct_mach_info::ieee_little_endian;
  else if (s == "vaxd" || s == "d")
    retval = oct_mach_info::vax_d;
  else if (s == "vaxg" || s == "g")
    retval = oct_mach_info::vax_g;
  else if (s == "cray" || s == "c")
    retval = oct_mach_info::cray;
  else if (s == "unknown")
    retval = oct_mach_info::unknown;
  else
    (*current_liboctave_error_handler)
      ("invalid architecture type specified");

  return retval;
}

string
oct_mach_info::float_format_as_string (float_format flt_fmt)
{
  string retval = "unknown";

  switch (flt_fmt)
    {
    case native:
      retval = "native";
      break;

    case ieee_big_endian:
      retval = "ieee_big_endian";
      break;

    case ieee_little_endian:
      retval = "ieee_little_endian";
      break;

    case vax_d:
      retval = "vax_d_float";
      break;

    case vax_g:
      retval = "vax_g_float";
      break;

    case cray:
      retval = "cray";
      break;

    default:
      break;
    }

  return retval;
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/