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