1960
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1960
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1960
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
1960
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "f77-fcn.h" |
2317
|
28 #include "lo-error.h" |
|
29 #include "mach-info.h" |
5275
|
30 #include "oct-types.h" |
1960
|
31 |
|
32 extern "C" |
|
33 { |
5275
|
34 double F77_FUNC (d1mach, D1MACH) (const octave_idx_type&); |
1960
|
35 } |
|
36 |
2317
|
37 oct_mach_info *oct_mach_info::instance = 0; |
1960
|
38 |
|
39 union equiv |
|
40 { |
|
41 double d; |
|
42 int i[2]; |
|
43 }; |
|
44 |
|
45 struct |
|
46 float_params |
|
47 { |
2317
|
48 oct_mach_info::float_format fp_fmt; |
1960
|
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 |
2317
|
73 void |
2926
|
74 oct_mach_info::init_float_format (void) const |
1960
|
75 { |
4605
|
76 #if defined (CRAY) |
|
77 |
5775
|
78 // FIXME -- this should be determined automatically. |
4605
|
79 |
|
80 native_float_fmt = oct_mach_info::flt_fmt_cray; |
|
81 |
|
82 #else |
|
83 |
1960
|
84 float_params fp[5]; |
|
85 |
4574
|
86 INIT_FLT_PAR (fp[0], oct_mach_info::flt_fmt_ieee_big_endian, |
1960
|
87 1048576, 0, |
|
88 2146435071, -1, |
|
89 1017118720, 0, |
|
90 1018167296, 0); |
|
91 |
4574
|
92 INIT_FLT_PAR (fp[1], oct_mach_info::flt_fmt_ieee_little_endian, |
1960
|
93 0, 1048576, |
|
94 -1, 2146435071, |
|
95 0, 1017118720, |
|
96 0, 1018167296); |
|
97 |
4574
|
98 INIT_FLT_PAR (fp[2], oct_mach_info::flt_fmt_vax_d, |
1960
|
99 128, 0, |
|
100 -32769, -1, |
|
101 9344, 0, |
|
102 9344, 0); |
|
103 |
4574
|
104 INIT_FLT_PAR (fp[3], oct_mach_info::flt_fmt_vax_g, |
1960
|
105 16, 0, |
|
106 -32769, -1, |
|
107 15552, 0, |
|
108 15552, 0); |
|
109 |
4574
|
110 INIT_FLT_PAR (fp[4], oct_mach_info::flt_fmt_unknown, |
1960
|
111 0, 0, |
|
112 0, 0, |
|
113 0, 0, |
|
114 0, 0); |
|
115 |
|
116 equiv mach_fp_par[4]; |
|
117 |
3887
|
118 mach_fp_par[0].d = F77_FUNC (d1mach, D1MACH) (1); |
|
119 mach_fp_par[1].d = F77_FUNC (d1mach, D1MACH) (2); |
|
120 mach_fp_par[2].d = F77_FUNC (d1mach, D1MACH) (3); |
|
121 mach_fp_par[3].d = F77_FUNC (d1mach, D1MACH) (4); |
1960
|
122 |
|
123 int i = 0; |
|
124 do |
|
125 { |
|
126 if (equiv_compare (fp[i].fp_par, mach_fp_par, 4)) |
|
127 { |
2317
|
128 native_float_fmt = fp[i].fp_fmt; |
1960
|
129 break; |
|
130 } |
|
131 } |
4574
|
132 while (fp[++i].fp_fmt != oct_mach_info::flt_fmt_unknown); |
4605
|
133 |
|
134 #endif |
2317
|
135 } |
|
136 |
|
137 void |
2926
|
138 oct_mach_info::ten_little_endians (void) const |
2317
|
139 { |
|
140 // Are we little or big endian? From Harbison & Steele. |
|
141 |
|
142 union |
|
143 { |
|
144 long l; |
|
145 char c[sizeof (long)]; |
|
146 } u; |
|
147 |
|
148 u.l = 1; |
|
149 |
|
150 big_chief = (u.c[sizeof (long) - 1] == 1); |
|
151 } |
|
152 |
|
153 oct_mach_info::oct_mach_info (void) |
|
154 { |
|
155 init_float_format (); |
|
156 ten_little_endians (); |
|
157 } |
|
158 |
2926
|
159 bool |
|
160 oct_mach_info::instance_ok (void) |
|
161 { |
|
162 bool retval = true; |
|
163 |
|
164 if (! instance) |
|
165 instance = new oct_mach_info (); |
|
166 |
|
167 if (! instance) |
|
168 { |
|
169 (*current_liboctave_error_handler) |
|
170 ("unable to create command history object!"); |
|
171 |
|
172 retval = false; |
|
173 } |
|
174 |
|
175 return retval; |
|
176 } |
|
177 |
2317
|
178 oct_mach_info::float_format |
|
179 oct_mach_info::native_float_format (void) |
|
180 { |
2926
|
181 return (instance_ok ()) |
4574
|
182 ? instance->native_float_fmt : oct_mach_info::flt_fmt_unknown; |
2317
|
183 } |
|
184 |
|
185 bool |
|
186 oct_mach_info::words_big_endian (void) |
|
187 { |
2926
|
188 return (instance_ok ()) |
|
189 ? instance->big_chief : false; |
2317
|
190 } |
|
191 |
|
192 bool |
|
193 oct_mach_info::words_little_endian (void) |
|
194 { |
2926
|
195 return (instance_ok ()) |
|
196 ? (! instance->big_chief) : false; |
2317
|
197 } |
|
198 |
|
199 oct_mach_info::float_format |
3504
|
200 oct_mach_info::string_to_float_format (const std::string& s) |
2317
|
201 { |
4574
|
202 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown; |
1960
|
203 |
2317
|
204 if (s == "native" || s == "n") |
5015
|
205 retval = oct_mach_info::native_float_format (); |
2317
|
206 else if (s == "ieee-be" || s == "b") |
4574
|
207 retval = oct_mach_info::flt_fmt_ieee_big_endian; |
2317
|
208 else if (s == "ieee-le" || s == "l") |
4574
|
209 retval = oct_mach_info::flt_fmt_ieee_little_endian; |
2317
|
210 else if (s == "vaxd" || s == "d") |
4574
|
211 retval = oct_mach_info::flt_fmt_vax_d; |
2759
|
212 else if (s == "vaxg" || s == "g") |
4574
|
213 retval = oct_mach_info::flt_fmt_vax_g; |
2317
|
214 else if (s == "cray" || s == "c") |
4574
|
215 retval = oct_mach_info::flt_fmt_cray; |
2317
|
216 else if (s == "unknown") |
4574
|
217 retval = oct_mach_info::flt_fmt_unknown; |
2317
|
218 else |
|
219 (*current_liboctave_error_handler) |
|
220 ("invalid architecture type specified"); |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
3504
|
225 std::string |
2317
|
226 oct_mach_info::float_format_as_string (float_format flt_fmt) |
|
227 { |
3504
|
228 std::string retval = "unknown"; |
2317
|
229 |
|
230 switch (flt_fmt) |
|
231 { |
4574
|
232 case flt_fmt_ieee_big_endian: |
2317
|
233 retval = "ieee_big_endian"; |
|
234 break; |
|
235 |
4574
|
236 case flt_fmt_ieee_little_endian: |
2317
|
237 retval = "ieee_little_endian"; |
|
238 break; |
|
239 |
4574
|
240 case flt_fmt_vax_d: |
2317
|
241 retval = "vax_d_float"; |
|
242 break; |
|
243 |
4574
|
244 case flt_fmt_vax_g: |
2317
|
245 retval = "vax_g_float"; |
|
246 break; |
|
247 |
4574
|
248 case flt_fmt_cray: |
2317
|
249 retval = "cray"; |
|
250 break; |
|
251 |
|
252 default: |
|
253 break; |
|
254 } |
|
255 |
|
256 return retval; |
1960
|
257 } |
|
258 |
|
259 /* |
|
260 ;;; Local Variables: *** |
|
261 ;;; mode: C++ *** |
|
262 ;;; End: *** |
|
263 */ |