4902
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 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 the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_inttypes_h) |
|
24 #define octave_inttypes_h 1 |
|
25 |
4968
|
26 #include <cmath> |
|
27 |
4902
|
28 #include <limits> |
|
29 #include <iostream> |
|
30 |
|
31 #include "data-conv.h" |
4953
|
32 #include "lo-ieee.h" |
4902
|
33 |
|
34 typedef signed char octave_int8_t; |
|
35 typedef TWO_BYTE_INT octave_int16_t; |
|
36 typedef FOUR_BYTE_INT octave_int32_t; |
|
37 typedef EIGHT_BYTE_INT octave_int64_t; |
|
38 |
|
39 typedef unsigned char octave_uint8_t; |
|
40 typedef unsigned TWO_BYTE_INT octave_uint16_t; |
|
41 typedef unsigned FOUR_BYTE_INT octave_uint32_t; |
|
42 typedef unsigned EIGHT_BYTE_INT octave_uint64_t; |
|
43 |
|
44 template <class T1, class T2> |
|
45 class |
|
46 octave_int_binop_traits |
|
47 { |
|
48 public: |
|
49 // The return type for a T1 by T2 binary operation. |
|
50 typedef T1 TR; |
|
51 }; |
|
52 |
|
53 #define OCTAVE_INT_BINOP_TRAIT(T1, T2, T3) \ |
|
54 template<> \ |
|
55 class octave_int_binop_traits <T1, T2> \ |
|
56 { \ |
|
57 public: \ |
|
58 typedef T3 TR; \ |
|
59 } |
|
60 |
4909
|
61 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_int8_t, octave_int8_t); |
|
62 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_int16_t, octave_int8_t); |
|
63 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_int32_t, octave_int8_t); |
|
64 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_int64_t, octave_int8_t); |
|
65 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_uint8_t, octave_int8_t); |
|
66 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_uint16_t, octave_int8_t); |
|
67 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_uint32_t, octave_int8_t); |
|
68 OCTAVE_INT_BINOP_TRAIT (octave_int8_t, octave_uint64_t, octave_int8_t); |
4902
|
69 |
4909
|
70 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_int8_t, octave_int16_t); |
|
71 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_int16_t, octave_int16_t); |
|
72 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_int32_t, octave_int16_t); |
|
73 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_int64_t, octave_int16_t); |
|
74 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_uint8_t, octave_int16_t); |
|
75 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_uint16_t, octave_int16_t); |
|
76 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_uint32_t, octave_int16_t); |
|
77 OCTAVE_INT_BINOP_TRAIT (octave_int16_t, octave_uint64_t, octave_int16_t); |
4902
|
78 |
4909
|
79 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_int8_t, octave_int32_t); |
|
80 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_int16_t, octave_int32_t); |
|
81 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_int32_t, octave_int32_t); |
|
82 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_int64_t, octave_int32_t); |
|
83 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_uint8_t, octave_int32_t); |
|
84 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_uint16_t, octave_int32_t); |
|
85 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_uint32_t, octave_int32_t); |
|
86 OCTAVE_INT_BINOP_TRAIT (octave_int32_t, octave_uint64_t, octave_int32_t); |
4902
|
87 |
4909
|
88 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_int8_t, octave_int64_t); |
|
89 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_int16_t, octave_int64_t); |
|
90 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_int32_t, octave_int64_t); |
|
91 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_int64_t, octave_int64_t); |
|
92 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_uint8_t, octave_int64_t); |
|
93 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_uint16_t, octave_int64_t); |
|
94 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_uint32_t, octave_int64_t); |
|
95 OCTAVE_INT_BINOP_TRAIT (octave_int64_t, octave_uint64_t, octave_int64_t); |
4902
|
96 |
4909
|
97 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_int8_t, octave_int8_t); |
|
98 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_int16_t, octave_int8_t); |
|
99 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_int32_t, octave_int8_t); |
|
100 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_int64_t, octave_int8_t); |
|
101 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_uint8_t, octave_uint8_t); |
|
102 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_uint16_t, octave_uint8_t); |
|
103 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_uint32_t, octave_uint8_t); |
|
104 OCTAVE_INT_BINOP_TRAIT (octave_uint8_t, octave_uint64_t, octave_uint8_t); |
4902
|
105 |
4909
|
106 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_int8_t, octave_int16_t); |
|
107 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_int16_t, octave_int16_t); |
|
108 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_int32_t, octave_int16_t); |
|
109 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_int64_t, octave_int16_t); |
|
110 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_uint8_t, octave_uint16_t); |
|
111 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_uint16_t, octave_uint16_t); |
|
112 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_uint32_t, octave_uint16_t); |
|
113 OCTAVE_INT_BINOP_TRAIT (octave_uint16_t, octave_uint64_t, octave_uint16_t); |
4902
|
114 |
4909
|
115 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_int8_t, octave_int32_t); |
|
116 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_int16_t, octave_int32_t); |
|
117 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_int32_t, octave_int32_t); |
|
118 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_int64_t, octave_int32_t); |
|
119 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_uint8_t, octave_uint32_t); |
|
120 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_uint16_t, octave_uint32_t); |
|
121 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_uint32_t, octave_uint32_t); |
|
122 OCTAVE_INT_BINOP_TRAIT (octave_uint32_t, octave_uint64_t, octave_uint32_t); |
4902
|
123 |
4909
|
124 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_int8_t, octave_int64_t); |
|
125 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_int16_t, octave_int64_t); |
|
126 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_int32_t, octave_int64_t); |
|
127 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_int64_t, octave_int64_t); |
|
128 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_uint8_t, octave_uint64_t); |
|
129 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_uint16_t, octave_uint64_t); |
|
130 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_uint32_t, octave_uint64_t); |
|
131 OCTAVE_INT_BINOP_TRAIT (octave_uint64_t, octave_uint64_t, octave_uint64_t); |
4902
|
132 |
|
133 template <class T1, class T2> |
|
134 inline T2 |
|
135 octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx) |
|
136 { |
4963
|
137 return (x > mx ? mx : (x < mn ? mn : T2 (x))); |
4902
|
138 } |
|
139 |
4943
|
140 // If X is unsigned and the new type is signed, then we only have to |
|
141 // check the upper limit, but we should cast the maximum value of the |
|
142 // new type to an unsigned type before performing the comparison. |
|
143 // This should always be OK because the maximum value should always be |
|
144 // positive. |
|
145 |
|
146 #define US_S_FTR(T1, T2, TC) \ |
|
147 template <> \ |
|
148 inline T2 \ |
|
149 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2& mx) \ |
|
150 { \ |
|
151 return x > static_cast<TC> (mx) ? mx : x; \ |
|
152 } |
|
153 |
|
154 #define US_S_FTR_FCNS(T) \ |
|
155 US_S_FTR(T, char, unsigned char) \ |
|
156 US_S_FTR(T, signed char, unsigned char) \ |
|
157 US_S_FTR(T, short, unsigned short) \ |
|
158 US_S_FTR(T, int, unsigned int) \ |
|
159 US_S_FTR(T, long, unsigned long) \ |
|
160 US_S_FTR(T, long long, unsigned long long) |
|
161 |
|
162 US_S_FTR_FCNS (unsigned char) |
|
163 US_S_FTR_FCNS (unsigned short) |
|
164 US_S_FTR_FCNS (unsigned int) |
|
165 US_S_FTR_FCNS (unsigned long) |
|
166 US_S_FTR_FCNS (unsigned long long) |
|
167 |
|
168 // If X is signed and the new type is unsigned, then we only have to |
|
169 // check the lower limit (which will always be 0 for an unsigned |
|
170 // type). The upper limit will be enforced correctly by converting to |
|
171 // the new type, even if the type of X is wider than the new type. |
|
172 |
|
173 #define S_US_FTR(T1, T2) \ |
|
174 template <> \ |
|
175 inline T2 \ |
|
176 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2&) \ |
|
177 { \ |
|
178 return x < 0 ? 0 : x; \ |
|
179 } |
|
180 |
|
181 #define S_US_FTR_FCNS(T) \ |
|
182 S_US_FTR(T, unsigned char) \ |
|
183 S_US_FTR(T, unsigned short) \ |
|
184 S_US_FTR(T, unsigned int) \ |
|
185 S_US_FTR(T, unsigned long) \ |
|
186 S_US_FTR(T, unsigned long long) |
|
187 |
|
188 S_US_FTR_FCNS (char) |
|
189 S_US_FTR_FCNS (signed char) |
|
190 S_US_FTR_FCNS (short) |
|
191 S_US_FTR_FCNS (int) |
|
192 S_US_FTR_FCNS (long) |
|
193 S_US_FTR_FCNS (long long) |
|
194 |
4902
|
195 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \ |
4943
|
196 octave_int_fit_to_range (r, \ |
|
197 std::numeric_limits<T>::min (), \ |
|
198 std::numeric_limits<T>::max ()) |
4902
|
199 |
|
200 #define OCTAVE_INT_MIN_VAL2(T1, T2) \ |
|
201 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min () |
|
202 |
|
203 #define OCTAVE_INT_MAX_VAL2(T1, T2) \ |
|
204 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::max () |
|
205 |
|
206 #define OCTAVE_INT_FIT_TO_RANGE2(r, T1, T2) \ |
|
207 octave_int_fit_to_range (r, \ |
|
208 OCTAVE_INT_MIN_VAL2 (T1, T2), \ |
|
209 OCTAVE_INT_MAX_VAL2 (T1, T2)) |
|
210 |
|
211 template <class T> |
|
212 class |
|
213 octave_int |
|
214 { |
|
215 public: |
|
216 |
4943
|
217 typedef T val_type; |
|
218 |
4902
|
219 octave_int (void) : ival () { } |
|
220 |
|
221 template <class U> |
|
222 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { } |
|
223 |
|
224 octave_int (bool b) : ival (b) { } |
|
225 |
|
226 template <class U> |
|
227 octave_int (const octave_int<U>& i) |
|
228 : ival (OCTAVE_INT_FIT_TO_RANGE (i.value (), T)) { } |
|
229 |
|
230 octave_int (const octave_int<T>& i) : ival (i.ival) { } |
|
231 |
|
232 octave_int& operator = (const octave_int<T>& i) |
|
233 { |
|
234 ival = i.ival; |
|
235 return *this; |
|
236 } |
|
237 |
|
238 ~octave_int (void) { } |
|
239 |
|
240 T value (void) const { return ival; } |
|
241 |
4949
|
242 const unsigned char * iptr (void) const |
|
243 { return reinterpret_cast<const unsigned char *> (& ival); } |
|
244 |
4902
|
245 bool operator ! (void) const { return ! ival; } |
|
246 |
4906
|
247 octave_int<T> operator + (void) const { return *this; } |
4902
|
248 |
4906
|
249 octave_int<T> operator - (void) const |
4902
|
250 { |
4953
|
251 // Can't just return -ival because signed types are not |
|
252 // symmetric, which causes things like -intmin("int32") to be the |
|
253 // same as intmin("int32") instead of intmax("int32") (which is |
|
254 // what we should get with saturation semantics). |
|
255 |
|
256 return std::numeric_limits<T>::is_signed ? |
|
257 OCTAVE_INT_FIT_TO_RANGE (- static_cast<double> (ival), T) : 0; |
4902
|
258 } |
|
259 |
|
260 operator double (void) const { return static_cast<double> (value ()); } |
|
261 |
|
262 octave_int<T>& operator += (const octave_int<T>& x) |
|
263 { |
|
264 double t = static_cast<double> (value ()); |
|
265 double tx = static_cast<double> (x.value ()); |
|
266 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); |
|
267 return *this; |
|
268 } |
|
269 |
|
270 octave_int<T>& operator -= (const octave_int<T>& x) |
|
271 { |
|
272 double t = static_cast<double> (value ()); |
|
273 double tx = static_cast<double> (x.value ()); |
|
274 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); |
|
275 return *this; |
|
276 } |
|
277 |
4952
|
278 octave_int<T>& operator *= (const octave_int<T>& x) |
|
279 { |
|
280 double t = static_cast<double> (value ()); |
|
281 double tx = static_cast<double> (x.value ()); |
|
282 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); |
|
283 return *this; |
|
284 } |
|
285 |
|
286 octave_int<T>& operator /= (const octave_int<T>& x) |
|
287 { |
|
288 double t = static_cast<double> (value ()); |
|
289 double tx = static_cast<double> (x.value ()); |
4968
|
290 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953
|
291 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952
|
292 return *this; |
|
293 } |
|
294 |
|
295 template <class T2> |
|
296 octave_int<T>& operator <<= (const T2& x) |
|
297 { |
|
298 ival = ((ival << x) > std::numeric_limits<T>::max ()) ? 0 : (ival << x); |
|
299 return *this; |
|
300 } |
|
301 |
|
302 template <class T2> |
|
303 octave_int<T>& operator >>= (const T2& x) |
|
304 { |
|
305 ival >>= x; |
|
306 return *this; |
|
307 } |
|
308 |
4906
|
309 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
|
310 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } |
|
311 |
4919
|
312 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909
|
313 |
4949
|
314 static int byte_size (void) { return sizeof(T); } |
|
315 |
4902
|
316 private: |
|
317 |
|
318 T ival; |
|
319 }; |
|
320 |
|
321 template <class T> |
4953
|
322 octave_int<T> |
|
323 pow (const octave_int<T>& a, const octave_int<T>& b) |
4952
|
324 { |
4953
|
325 octave_int<T> retval; |
4952
|
326 |
4953
|
327 octave_int<T> zero = octave_int<T> (0); |
|
328 octave_int<T> one = octave_int<T> (1); |
4952
|
329 |
|
330 if (b == zero) |
|
331 retval = one; |
|
332 else if (b < zero) |
|
333 retval = zero; |
|
334 else |
|
335 { |
4953
|
336 octave_int<T> a_val = a; |
|
337 octave_int<T> b_val = b; |
4952
|
338 |
|
339 retval = a; |
|
340 |
|
341 b_val -= 1; |
|
342 |
|
343 while (b_val) |
|
344 { |
|
345 if (b_val & one) |
|
346 retval = retval * a_val; |
|
347 |
|
348 b_val = b_val >> 1; |
|
349 |
|
350 if (b_val > zero) |
|
351 a_val = a_val * a_val; |
|
352 } |
|
353 } |
|
354 |
|
355 return retval; |
|
356 } |
|
357 |
|
358 template <class T> |
4953
|
359 octave_int<T> |
|
360 pow (double a, const octave_int<T>& b) |
|
361 { |
|
362 double tb = static_cast<double> (b.value ()); |
|
363 double r = pow (a, tb); |
4968
|
364 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
365 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
366 } |
|
367 |
|
368 template <class T> |
|
369 octave_int<T> |
|
370 pow (const octave_int<T>& a, double b) |
|
371 { |
|
372 double ta = static_cast<double> (a.value ()); |
|
373 double r = pow (ta, b); |
4968
|
374 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
375 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
376 } |
|
377 |
|
378 template <class T> |
4902
|
379 std::ostream& |
|
380 operator << (std::ostream& os, const octave_int<T>& ival) |
|
381 { |
|
382 os << ival.value (); |
|
383 return os; |
|
384 } |
|
385 |
|
386 template <class T> |
|
387 std::istream& |
|
388 operator >> (std::istream& is, octave_int<T>& ival) |
|
389 { |
|
390 T tmp = 0; |
|
391 is >> tmp; |
|
392 ival = tmp; |
|
393 return is; |
|
394 } |
|
395 |
|
396 typedef octave_int<octave_int8_t> octave_int8; |
|
397 typedef octave_int<octave_int16_t> octave_int16; |
|
398 typedef octave_int<octave_int32_t> octave_int32; |
|
399 typedef octave_int<octave_int64_t> octave_int64; |
|
400 |
|
401 typedef octave_int<octave_uint8_t> octave_uint8; |
|
402 typedef octave_int<octave_uint16_t> octave_uint16; |
|
403 typedef octave_int<octave_uint32_t> octave_uint32; |
|
404 typedef octave_int<octave_uint64_t> octave_uint64; |
|
405 |
|
406 #define OCTAVE_INT_BIN_OP(OP) \ |
|
407 \ |
|
408 template <class T1, class T2> \ |
|
409 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ |
|
410 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
411 { \ |
|
412 double tx = static_cast<double> (x.value ()); \ |
|
413 double ty = static_cast<double> (y.value ()); \ |
|
414 double r = tx OP ty; \ |
|
415 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ |
4906
|
416 } |
4902
|
417 |
|
418 OCTAVE_INT_BIN_OP(+) |
|
419 OCTAVE_INT_BIN_OP(-) |
|
420 OCTAVE_INT_BIN_OP(*) |
4953
|
421 |
|
422 template <class T1, class T2> |
|
423 octave_int<typename octave_int_binop_traits<T1, T2>::TR> |
|
424 operator / (const octave_int<T1>& x, const octave_int<T2>& y) |
|
425 { |
|
426 double tx = static_cast<double> (x.value ()); |
|
427 double ty = static_cast<double> (y.value ()); |
|
428 double r = (tx == 0 && ty == 0) ? 0 : tx / ty; |
|
429 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
|
430 } |
|
431 |
|
432 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
|
433 \ |
|
434 template <class T> \ |
|
435 octave_int<T> \ |
|
436 operator OP (const octave_int<T>& x, double y) \ |
|
437 { \ |
|
438 double tx = static_cast<double> (x.value ()); \ |
4968
|
439 double r = xround (tx OP y); \ |
|
440 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
441 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
442 } |
|
443 |
|
444 OCTAVE_INT_DOUBLE_BIN_OP(+) |
|
445 OCTAVE_INT_DOUBLE_BIN_OP(-) |
|
446 OCTAVE_INT_DOUBLE_BIN_OP(*) |
|
447 OCTAVE_INT_DOUBLE_BIN_OP(/) |
|
448 |
|
449 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ |
|
450 \ |
|
451 template <class T> \ |
|
452 octave_int<T> \ |
|
453 operator OP (double x, const octave_int<T>& y) \ |
|
454 { \ |
|
455 double ty = static_cast<double> (y.value ()); \ |
|
456 double r = x OP ty; \ |
4968
|
457 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
458 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
459 } |
|
460 |
|
461 OCTAVE_DOUBLE_INT_BIN_OP(+) |
|
462 OCTAVE_DOUBLE_INT_BIN_OP(-) |
|
463 OCTAVE_DOUBLE_INT_BIN_OP(*) |
|
464 OCTAVE_DOUBLE_INT_BIN_OP(/) |
4902
|
465 |
4906
|
466 #define OCTAVE_INT_BITCMP_OP(OP) \ |
|
467 \ |
|
468 template <class T> \ |
|
469 octave_int<T> \ |
|
470 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ |
|
471 { \ |
|
472 return x.value () OP y.value (); \ |
|
473 } |
|
474 |
|
475 OCTAVE_INT_BITCMP_OP (&) |
|
476 OCTAVE_INT_BITCMP_OP (|) |
|
477 OCTAVE_INT_BITCMP_OP (^) |
|
478 |
4952
|
479 template <class T1, class T2> |
|
480 octave_int<T1> |
|
481 operator << (const octave_int<T1>& x, const T2& y) |
|
482 { |
|
483 T1 retval = x; |
|
484 return retval <<= y; |
|
485 } |
4906
|
486 |
4952
|
487 template <class T1, class T2> |
|
488 octave_int<T1> |
|
489 operator >> (const octave_int<T1>& x, const T2& y) |
|
490 { |
|
491 T1 retval = x; |
|
492 return retval >>= y; |
|
493 } |
4906
|
494 |
4909
|
495 template <class T> |
|
496 octave_int<T> |
4920
|
497 bitshift (const octave_int<T>& a, int n, |
|
498 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909
|
499 { |
|
500 if (n > 0) |
4952
|
501 return (a << n) & mask; |
4909
|
502 else if (n < 0) |
4952
|
503 return (a >> -n) & mask; |
4909
|
504 else |
|
505 return a; |
|
506 } |
|
507 |
4964
|
508 // XXX FIXME XXX -- need partial specializations for int64 and uint64 |
|
509 // types. |
|
510 |
4902
|
511 #define OCTAVE_INT_CMP_OP(OP) \ |
|
512 \ |
|
513 template <class T1, class T2> \ |
|
514 bool \ |
|
515 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
516 { \ |
4964
|
517 double tx = static_cast<double> (x.value ()); \ |
|
518 double ty = static_cast<double> (y.value ()); \ |
|
519 return tx OP ty; \ |
4906
|
520 } |
4902
|
521 |
|
522 OCTAVE_INT_CMP_OP (<) |
|
523 OCTAVE_INT_CMP_OP (<=) |
|
524 OCTAVE_INT_CMP_OP (>=) |
|
525 OCTAVE_INT_CMP_OP (>) |
|
526 OCTAVE_INT_CMP_OP (==) |
|
527 OCTAVE_INT_CMP_OP (!=) |
|
528 |
4915
|
529 #define OCTAVE_INT_CONCAT_FN(TYPE) \ |
|
530 intNDArray< TYPE > \ |
|
531 concat (const intNDArray< TYPE >& ra, const intNDArray< TYPE >& rb, \ |
|
532 const Array<int>& ra_idx) \ |
|
533 { \ |
|
534 intNDArray< TYPE > retval (ra); \ |
|
535 retval.insert (rb, ra_idx); \ |
|
536 return retval; \ |
|
537 } |
|
538 |
|
539 #define OCTAVE_INT_CONCAT_DECL(TYPE) \ |
|
540 intNDArray< TYPE > \ |
|
541 concat (const intNDArray< TYPE >& ra, const intNDArray< TYPE >& rb, \ |
|
542 const Array<int>& ra_idx); |
|
543 |
4902
|
544 #undef OCTAVE_INT_TRAIT |
|
545 #undef OCTAVE_INT_BINOP_TRAIT |
|
546 #undef OCTAVE_INT_MIN_VAL |
|
547 #undef OCTAVE_INT_MAX_VAL |
|
548 #undef OCTAVE_INT_FIT_TO_RANGE |
|
549 #undef OCTAVE_INT_MIN_VAL2 |
|
550 #undef OCTAVE_INT_MAX_VAL2 |
|
551 #undef OCTAVE_INT_FIT_TO_RANGE2 |
|
552 #undef OCTAVE_INT_BIN_OP |
|
553 #undef OCTAVE_INT_CMP_OP |
|
554 |
|
555 #endif |
|
556 |
|
557 /* |
|
558 ;;; Local Variables: *** |
|
559 ;;; mode: C++ *** |
|
560 ;;; End: *** |
|
561 */ |