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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4902
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_inttypes_h) |
|
25 #define octave_inttypes_h 1 |
|
26 |
|
27 #include <limits> |
|
28 #include <iostream> |
|
29 |
5828
|
30 #include "oct-types.h" |
4953
|
31 #include "lo-ieee.h" |
4969
|
32 #include "lo-mappers.h" |
4902
|
33 |
|
34 template <class T1, class T2> |
|
35 class |
|
36 octave_int_binop_traits |
|
37 { |
|
38 public: |
|
39 // The return type for a T1 by T2 binary operation. |
|
40 typedef T1 TR; |
|
41 }; |
|
42 |
|
43 #define OCTAVE_INT_BINOP_TRAIT(T1, T2, T3) \ |
|
44 template<> \ |
|
45 class octave_int_binop_traits <T1, T2> \ |
|
46 { \ |
|
47 public: \ |
|
48 typedef T3 TR; \ |
|
49 } |
|
50 |
5828
|
51 OCTAVE_INT_BINOP_TRAIT (int8_t, int8_t, int8_t); |
|
52 OCTAVE_INT_BINOP_TRAIT (int8_t, int16_t, int8_t); |
|
53 OCTAVE_INT_BINOP_TRAIT (int8_t, int32_t, int8_t); |
|
54 OCTAVE_INT_BINOP_TRAIT (int8_t, int64_t, int8_t); |
|
55 OCTAVE_INT_BINOP_TRAIT (int8_t, uint8_t, int8_t); |
|
56 OCTAVE_INT_BINOP_TRAIT (int8_t, uint16_t, int8_t); |
|
57 OCTAVE_INT_BINOP_TRAIT (int8_t, uint32_t, int8_t); |
|
58 OCTAVE_INT_BINOP_TRAIT (int8_t, uint64_t, int8_t); |
4902
|
59 |
5828
|
60 OCTAVE_INT_BINOP_TRAIT (int16_t, int8_t, int16_t); |
|
61 OCTAVE_INT_BINOP_TRAIT (int16_t, int16_t, int16_t); |
|
62 OCTAVE_INT_BINOP_TRAIT (int16_t, int32_t, int16_t); |
|
63 OCTAVE_INT_BINOP_TRAIT (int16_t, int64_t, int16_t); |
|
64 OCTAVE_INT_BINOP_TRAIT (int16_t, uint8_t, int16_t); |
|
65 OCTAVE_INT_BINOP_TRAIT (int16_t, uint16_t, int16_t); |
|
66 OCTAVE_INT_BINOP_TRAIT (int16_t, uint32_t, int16_t); |
|
67 OCTAVE_INT_BINOP_TRAIT (int16_t, uint64_t, int16_t); |
4902
|
68 |
5828
|
69 OCTAVE_INT_BINOP_TRAIT (int32_t, int8_t, int32_t); |
|
70 OCTAVE_INT_BINOP_TRAIT (int32_t, int16_t, int32_t); |
|
71 OCTAVE_INT_BINOP_TRAIT (int32_t, int32_t, int32_t); |
|
72 OCTAVE_INT_BINOP_TRAIT (int32_t, int64_t, int32_t); |
|
73 OCTAVE_INT_BINOP_TRAIT (int32_t, uint8_t, int32_t); |
|
74 OCTAVE_INT_BINOP_TRAIT (int32_t, uint16_t, int32_t); |
|
75 OCTAVE_INT_BINOP_TRAIT (int32_t, uint32_t, int32_t); |
|
76 OCTAVE_INT_BINOP_TRAIT (int32_t, uint64_t, int32_t); |
4902
|
77 |
5828
|
78 OCTAVE_INT_BINOP_TRAIT (int64_t, int8_t, int64_t); |
|
79 OCTAVE_INT_BINOP_TRAIT (int64_t, int16_t, int64_t); |
|
80 OCTAVE_INT_BINOP_TRAIT (int64_t, int32_t, int64_t); |
|
81 OCTAVE_INT_BINOP_TRAIT (int64_t, int64_t, int64_t); |
|
82 OCTAVE_INT_BINOP_TRAIT (int64_t, uint8_t, int64_t); |
|
83 OCTAVE_INT_BINOP_TRAIT (int64_t, uint16_t, int64_t); |
|
84 OCTAVE_INT_BINOP_TRAIT (int64_t, uint32_t, int64_t); |
|
85 OCTAVE_INT_BINOP_TRAIT (int64_t, uint64_t, int64_t); |
4902
|
86 |
5828
|
87 OCTAVE_INT_BINOP_TRAIT (uint8_t, int8_t, int8_t); |
|
88 OCTAVE_INT_BINOP_TRAIT (uint8_t, int16_t, int8_t); |
|
89 OCTAVE_INT_BINOP_TRAIT (uint8_t, int32_t, int8_t); |
|
90 OCTAVE_INT_BINOP_TRAIT (uint8_t, int64_t, int8_t); |
|
91 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint8_t, uint8_t); |
|
92 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint16_t, uint8_t); |
|
93 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint32_t, uint8_t); |
|
94 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint64_t, uint8_t); |
4902
|
95 |
5828
|
96 OCTAVE_INT_BINOP_TRAIT (uint16_t, int8_t, int16_t); |
|
97 OCTAVE_INT_BINOP_TRAIT (uint16_t, int16_t, int16_t); |
|
98 OCTAVE_INT_BINOP_TRAIT (uint16_t, int32_t, int16_t); |
|
99 OCTAVE_INT_BINOP_TRAIT (uint16_t, int64_t, int16_t); |
|
100 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint8_t, uint16_t); |
|
101 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint16_t, uint16_t); |
|
102 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint32_t, uint16_t); |
|
103 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint64_t, uint16_t); |
4902
|
104 |
5828
|
105 OCTAVE_INT_BINOP_TRAIT (uint32_t, int8_t, int32_t); |
|
106 OCTAVE_INT_BINOP_TRAIT (uint32_t, int16_t, int32_t); |
|
107 OCTAVE_INT_BINOP_TRAIT (uint32_t, int32_t, int32_t); |
|
108 OCTAVE_INT_BINOP_TRAIT (uint32_t, int64_t, int32_t); |
|
109 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint8_t, uint32_t); |
|
110 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint16_t, uint32_t); |
|
111 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint32_t, uint32_t); |
|
112 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint64_t, uint32_t); |
4902
|
113 |
5828
|
114 OCTAVE_INT_BINOP_TRAIT (uint64_t, int8_t, int64_t); |
|
115 OCTAVE_INT_BINOP_TRAIT (uint64_t, int16_t, int64_t); |
|
116 OCTAVE_INT_BINOP_TRAIT (uint64_t, int32_t, int64_t); |
|
117 OCTAVE_INT_BINOP_TRAIT (uint64_t, int64_t, int64_t); |
|
118 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint8_t, uint64_t); |
|
119 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint16_t, uint64_t); |
|
120 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint32_t, uint64_t); |
|
121 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint64_t, uint64_t); |
4902
|
122 |
|
123 template <class T1, class T2> |
|
124 inline T2 |
|
125 octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx) |
|
126 { |
4963
|
127 return (x > mx ? mx : (x < mn ? mn : T2 (x))); |
4902
|
128 } |
|
129 |
4943
|
130 // If X is unsigned and the new type is signed, then we only have to |
|
131 // check the upper limit, but we should cast the maximum value of the |
|
132 // new type to an unsigned type before performing the comparison. |
|
133 // This should always be OK because the maximum value should always be |
|
134 // positive. |
|
135 |
5072
|
136 #define OCTAVE_US_S_FTR(T1, T2, TC) \ |
4943
|
137 template <> \ |
|
138 inline T2 \ |
|
139 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2& mx) \ |
|
140 { \ |
|
141 return x > static_cast<TC> (mx) ? mx : x; \ |
|
142 } |
|
143 |
5072
|
144 #define OCTAVE_US_S_FTR_FCNS(T) \ |
|
145 OCTAVE_US_S_FTR (T, char, unsigned char) \ |
|
146 OCTAVE_US_S_FTR (T, signed char, unsigned char) \ |
|
147 OCTAVE_US_S_FTR (T, short, unsigned short) \ |
|
148 OCTAVE_US_S_FTR (T, int, unsigned int) \ |
|
149 OCTAVE_US_S_FTR (T, long, unsigned long) \ |
|
150 OCTAVE_US_S_FTR (T, long long, unsigned long long) |
4943
|
151 |
5072
|
152 OCTAVE_US_S_FTR_FCNS (unsigned char) |
|
153 OCTAVE_US_S_FTR_FCNS (unsigned short) |
|
154 OCTAVE_US_S_FTR_FCNS (unsigned int) |
|
155 OCTAVE_US_S_FTR_FCNS (unsigned long) |
|
156 OCTAVE_US_S_FTR_FCNS (unsigned long long) |
4943
|
157 |
|
158 // If X is signed and the new type is unsigned, then we only have to |
|
159 // check the lower limit (which will always be 0 for an unsigned |
|
160 // type). The upper limit will be enforced correctly by converting to |
|
161 // the new type, even if the type of X is wider than the new type. |
|
162 |
5072
|
163 #define OCTAVE_S_US_FTR(T1, T2) \ |
4943
|
164 template <> \ |
|
165 inline T2 \ |
|
166 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2&) \ |
|
167 { \ |
5420
|
168 return x <= 0 ? 0 : x; \ |
4943
|
169 } |
|
170 |
5072
|
171 #define OCTAVE_S_US_FTR_FCNS(T) \ |
|
172 OCTAVE_S_US_FTR (T, unsigned char) \ |
|
173 OCTAVE_S_US_FTR (T, unsigned short) \ |
|
174 OCTAVE_S_US_FTR (T, unsigned int) \ |
|
175 OCTAVE_S_US_FTR (T, unsigned long) \ |
|
176 OCTAVE_S_US_FTR (T, unsigned long long) |
4943
|
177 |
5072
|
178 OCTAVE_S_US_FTR_FCNS (char) |
|
179 OCTAVE_S_US_FTR_FCNS (signed char) |
|
180 OCTAVE_S_US_FTR_FCNS (short) |
|
181 OCTAVE_S_US_FTR_FCNS (int) |
|
182 OCTAVE_S_US_FTR_FCNS (long) |
|
183 OCTAVE_S_US_FTR_FCNS (long long) |
4943
|
184 |
4902
|
185 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \ |
4943
|
186 octave_int_fit_to_range (r, \ |
|
187 std::numeric_limits<T>::min (), \ |
|
188 std::numeric_limits<T>::max ()) |
4902
|
189 |
|
190 #define OCTAVE_INT_MIN_VAL2(T1, T2) \ |
|
191 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min () |
|
192 |
|
193 #define OCTAVE_INT_MAX_VAL2(T1, T2) \ |
|
194 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::max () |
|
195 |
|
196 #define OCTAVE_INT_FIT_TO_RANGE2(r, T1, T2) \ |
|
197 octave_int_fit_to_range (r, \ |
|
198 OCTAVE_INT_MIN_VAL2 (T1, T2), \ |
|
199 OCTAVE_INT_MAX_VAL2 (T1, T2)) |
|
200 |
|
201 template <class T> |
|
202 class |
|
203 octave_int |
|
204 { |
|
205 public: |
|
206 |
4943
|
207 typedef T val_type; |
|
208 |
4902
|
209 octave_int (void) : ival () { } |
|
210 |
|
211 template <class U> |
|
212 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { } |
|
213 |
|
214 octave_int (bool b) : ival (b) { } |
|
215 |
|
216 template <class U> |
|
217 octave_int (const octave_int<U>& i) |
|
218 : ival (OCTAVE_INT_FIT_TO_RANGE (i.value (), T)) { } |
|
219 |
|
220 octave_int (const octave_int<T>& i) : ival (i.ival) { } |
|
221 |
|
222 octave_int& operator = (const octave_int<T>& i) |
|
223 { |
|
224 ival = i.ival; |
|
225 return *this; |
|
226 } |
|
227 |
|
228 ~octave_int (void) { } |
|
229 |
|
230 T value (void) const { return ival; } |
|
231 |
4949
|
232 const unsigned char * iptr (void) const |
|
233 { return reinterpret_cast<const unsigned char *> (& ival); } |
|
234 |
4902
|
235 bool operator ! (void) const { return ! ival; } |
|
236 |
4906
|
237 octave_int<T> operator + (void) const { return *this; } |
4902
|
238 |
4906
|
239 octave_int<T> operator - (void) const |
4902
|
240 { |
4953
|
241 // Can't just return -ival because signed types are not |
|
242 // symmetric, which causes things like -intmin("int32") to be the |
|
243 // same as intmin("int32") instead of intmax("int32") (which is |
|
244 // what we should get with saturation semantics). |
|
245 |
|
246 return std::numeric_limits<T>::is_signed ? |
|
247 OCTAVE_INT_FIT_TO_RANGE (- static_cast<double> (ival), T) : 0; |
4902
|
248 } |
|
249 |
5533
|
250 operator char (void) const { return static_cast<char> (value ()); } |
|
251 |
4902
|
252 operator double (void) const { return static_cast<double> (value ()); } |
|
253 |
5030
|
254 operator float (void) const { return static_cast<float> (value ()); } |
|
255 |
4902
|
256 octave_int<T>& operator += (const octave_int<T>& x) |
|
257 { |
|
258 double t = static_cast<double> (value ()); |
|
259 double tx = static_cast<double> (x.value ()); |
|
260 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); |
|
261 return *this; |
|
262 } |
|
263 |
|
264 octave_int<T>& operator -= (const octave_int<T>& x) |
|
265 { |
|
266 double t = static_cast<double> (value ()); |
|
267 double tx = static_cast<double> (x.value ()); |
|
268 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); |
|
269 return *this; |
|
270 } |
|
271 |
4952
|
272 octave_int<T>& operator *= (const octave_int<T>& x) |
|
273 { |
|
274 double t = static_cast<double> (value ()); |
|
275 double tx = static_cast<double> (x.value ()); |
|
276 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); |
|
277 return *this; |
|
278 } |
|
279 |
|
280 octave_int<T>& operator /= (const octave_int<T>& x) |
|
281 { |
|
282 double t = static_cast<double> (value ()); |
|
283 double tx = static_cast<double> (x.value ()); |
4968
|
284 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953
|
285 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952
|
286 return *this; |
|
287 } |
|
288 |
|
289 template <class T2> |
|
290 octave_int<T>& operator <<= (const T2& x) |
|
291 { |
|
292 ival = ((ival << x) > std::numeric_limits<T>::max ()) ? 0 : (ival << x); |
|
293 return *this; |
|
294 } |
|
295 |
|
296 template <class T2> |
|
297 octave_int<T>& operator >>= (const T2& x) |
|
298 { |
|
299 ival >>= x; |
|
300 return *this; |
|
301 } |
|
302 |
4906
|
303 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
|
304 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } |
|
305 |
4919
|
306 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909
|
307 |
4949
|
308 static int byte_size (void) { return sizeof(T); } |
|
309 |
4902
|
310 private: |
|
311 |
|
312 T ival; |
|
313 }; |
|
314 |
|
315 template <class T> |
4953
|
316 octave_int<T> |
|
317 pow (const octave_int<T>& a, const octave_int<T>& b) |
4952
|
318 { |
4953
|
319 octave_int<T> retval; |
4952
|
320 |
4953
|
321 octave_int<T> zero = octave_int<T> (0); |
|
322 octave_int<T> one = octave_int<T> (1); |
4952
|
323 |
|
324 if (b == zero) |
|
325 retval = one; |
|
326 else if (b < zero) |
|
327 retval = zero; |
|
328 else |
|
329 { |
4953
|
330 octave_int<T> a_val = a; |
|
331 octave_int<T> b_val = b; |
4952
|
332 |
|
333 retval = a; |
|
334 |
|
335 b_val -= 1; |
|
336 |
5030
|
337 while (b_val != zero) |
4952
|
338 { |
5030
|
339 if ((b_val & one) != zero) |
4952
|
340 retval = retval * a_val; |
|
341 |
|
342 b_val = b_val >> 1; |
|
343 |
|
344 if (b_val > zero) |
|
345 a_val = a_val * a_val; |
|
346 } |
|
347 } |
|
348 |
|
349 return retval; |
|
350 } |
|
351 |
|
352 template <class T> |
4953
|
353 octave_int<T> |
|
354 pow (double a, const octave_int<T>& b) |
|
355 { |
|
356 double tb = static_cast<double> (b.value ()); |
|
357 double r = pow (a, tb); |
4968
|
358 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
359 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
360 } |
|
361 |
|
362 template <class T> |
|
363 octave_int<T> |
|
364 pow (const octave_int<T>& a, double b) |
|
365 { |
|
366 double ta = static_cast<double> (a.value ()); |
|
367 double r = pow (ta, b); |
4968
|
368 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
369 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
370 } |
|
371 |
|
372 template <class T> |
4902
|
373 std::ostream& |
|
374 operator << (std::ostream& os, const octave_int<T>& ival) |
|
375 { |
|
376 os << ival.value (); |
|
377 return os; |
|
378 } |
|
379 |
|
380 template <class T> |
|
381 std::istream& |
|
382 operator >> (std::istream& is, octave_int<T>& ival) |
|
383 { |
|
384 T tmp = 0; |
|
385 is >> tmp; |
|
386 ival = tmp; |
|
387 return is; |
|
388 } |
|
389 |
5828
|
390 typedef octave_int<int8_t> octave_int8; |
|
391 typedef octave_int<int16_t> octave_int16; |
|
392 typedef octave_int<int32_t> octave_int32; |
|
393 typedef octave_int<int64_t> octave_int64; |
4902
|
394 |
5828
|
395 typedef octave_int<uint8_t> octave_uint8; |
|
396 typedef octave_int<uint16_t> octave_uint16; |
|
397 typedef octave_int<uint32_t> octave_uint32; |
|
398 typedef octave_int<uint64_t> octave_uint64; |
4902
|
399 |
|
400 #define OCTAVE_INT_BIN_OP(OP) \ |
|
401 template <class T1, class T2> \ |
|
402 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ |
|
403 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
404 { \ |
|
405 double tx = static_cast<double> (x.value ()); \ |
|
406 double ty = static_cast<double> (y.value ()); \ |
|
407 double r = tx OP ty; \ |
|
408 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ |
4906
|
409 } |
4902
|
410 |
|
411 OCTAVE_INT_BIN_OP(+) |
|
412 OCTAVE_INT_BIN_OP(-) |
|
413 OCTAVE_INT_BIN_OP(*) |
4953
|
414 |
|
415 template <class T1, class T2> |
|
416 octave_int<typename octave_int_binop_traits<T1, T2>::TR> |
|
417 operator / (const octave_int<T1>& x, const octave_int<T2>& y) |
|
418 { |
|
419 double tx = static_cast<double> (x.value ()); |
|
420 double ty = static_cast<double> (y.value ()); |
|
421 double r = (tx == 0 && ty == 0) ? 0 : tx / ty; |
|
422 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
|
423 } |
|
424 |
|
425 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
|
426 template <class T> \ |
|
427 octave_int<T> \ |
|
428 operator OP (const octave_int<T>& x, double y) \ |
|
429 { \ |
|
430 double tx = static_cast<double> (x.value ()); \ |
4968
|
431 double r = xround (tx OP y); \ |
|
432 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
433 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
434 } |
|
435 |
|
436 OCTAVE_INT_DOUBLE_BIN_OP(+) |
|
437 OCTAVE_INT_DOUBLE_BIN_OP(-) |
|
438 OCTAVE_INT_DOUBLE_BIN_OP(*) |
|
439 OCTAVE_INT_DOUBLE_BIN_OP(/) |
|
440 |
|
441 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ |
|
442 template <class T> \ |
|
443 octave_int<T> \ |
|
444 operator OP (double x, const octave_int<T>& y) \ |
|
445 { \ |
|
446 double ty = static_cast<double> (y.value ()); \ |
|
447 double r = x OP ty; \ |
4968
|
448 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
449 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
450 } |
|
451 |
|
452 OCTAVE_DOUBLE_INT_BIN_OP(+) |
|
453 OCTAVE_DOUBLE_INT_BIN_OP(-) |
|
454 OCTAVE_DOUBLE_INT_BIN_OP(*) |
|
455 OCTAVE_DOUBLE_INT_BIN_OP(/) |
4902
|
456 |
5029
|
457 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
|
458 template <class T> \ |
|
459 bool \ |
|
460 operator OP (const octave_int<T>& x, const double& y) \ |
|
461 { \ |
|
462 double tx = static_cast<double> (x.value ()); \ |
5030
|
463 return tx OP y; \ |
5029
|
464 } |
|
465 |
|
466 OCTAVE_INT_DOUBLE_CMP_OP (<) |
|
467 OCTAVE_INT_DOUBLE_CMP_OP (<=) |
|
468 OCTAVE_INT_DOUBLE_CMP_OP (>=) |
|
469 OCTAVE_INT_DOUBLE_CMP_OP (>) |
|
470 OCTAVE_INT_DOUBLE_CMP_OP (==) |
|
471 OCTAVE_INT_DOUBLE_CMP_OP (!=) |
|
472 |
|
473 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ |
|
474 template <class T> \ |
|
475 bool \ |
|
476 operator OP (const double& x, const octave_int<T>& y) \ |
|
477 { \ |
|
478 double ty = static_cast<double> (y.value ()); \ |
5030
|
479 return x OP ty; \ |
5029
|
480 } |
|
481 |
|
482 OCTAVE_DOUBLE_INT_CMP_OP (<) |
|
483 OCTAVE_DOUBLE_INT_CMP_OP (<=) |
|
484 OCTAVE_DOUBLE_INT_CMP_OP (>=) |
|
485 OCTAVE_DOUBLE_INT_CMP_OP (>) |
|
486 OCTAVE_DOUBLE_INT_CMP_OP (==) |
|
487 OCTAVE_DOUBLE_INT_CMP_OP (!=) |
|
488 |
4906
|
489 #define OCTAVE_INT_BITCMP_OP(OP) \ |
|
490 template <class T> \ |
|
491 octave_int<T> \ |
|
492 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ |
|
493 { \ |
|
494 return x.value () OP y.value (); \ |
|
495 } |
|
496 |
|
497 OCTAVE_INT_BITCMP_OP (&) |
|
498 OCTAVE_INT_BITCMP_OP (|) |
|
499 OCTAVE_INT_BITCMP_OP (^) |
|
500 |
4952
|
501 template <class T1, class T2> |
|
502 octave_int<T1> |
|
503 operator << (const octave_int<T1>& x, const T2& y) |
|
504 { |
5030
|
505 octave_int<T1> retval = x; |
4952
|
506 return retval <<= y; |
|
507 } |
4906
|
508 |
4952
|
509 template <class T1, class T2> |
|
510 octave_int<T1> |
|
511 operator >> (const octave_int<T1>& x, const T2& y) |
|
512 { |
5030
|
513 octave_int<T1> retval = x; |
4952
|
514 return retval >>= y; |
|
515 } |
4906
|
516 |
4909
|
517 template <class T> |
|
518 octave_int<T> |
4920
|
519 bitshift (const octave_int<T>& a, int n, |
|
520 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909
|
521 { |
|
522 if (n > 0) |
4952
|
523 return (a << n) & mask; |
4909
|
524 else if (n < 0) |
4952
|
525 return (a >> -n) & mask; |
4909
|
526 else |
|
527 return a; |
|
528 } |
|
529 |
4902
|
530 #define OCTAVE_INT_CMP_OP(OP) \ |
|
531 template <class T1, class T2> \ |
|
532 bool \ |
|
533 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
534 { \ |
5072
|
535 return x.value () OP y.value (); \ |
4906
|
536 } |
4902
|
537 |
|
538 OCTAVE_INT_CMP_OP (<) |
|
539 OCTAVE_INT_CMP_OP (<=) |
|
540 OCTAVE_INT_CMP_OP (>=) |
|
541 OCTAVE_INT_CMP_OP (>) |
|
542 OCTAVE_INT_CMP_OP (==) |
|
543 OCTAVE_INT_CMP_OP (!=) |
|
544 |
5072
|
545 // The following apply if the unsigned type is at least as wide as the |
|
546 // signed type (then we can cast postive signed values to the unsigned |
|
547 // type and compare). |
|
548 |
|
549 #define OCTAVE_US_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
|
550 bool operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
|
551 |
|
552 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
553 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ |
|
554 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ |
|
555 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ |
|
556 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ |
|
557 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ |
|
558 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) |
|
559 |
|
560 #define OCTAVE_SU_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
|
561 bool operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
|
562 |
|
563 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ |
|
564 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ |
|
565 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ |
|
566 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ |
|
567 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ |
|
568 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ |
|
569 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) |
|
570 |
|
571 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
572 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ |
|
573 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) |
|
574 |
5828
|
575 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
|
576 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) |
|
577 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) |
5072
|
578 |
5828
|
579 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
|
580 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) |
|
581 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) |
|
582 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) |
5072
|
583 |
|
584 // The following apply if the signed type is wider than the unsigned |
|
585 // type (then we can cast unsigned values to the signed type and |
|
586 // compare if the signed value is positive). |
|
587 |
|
588 #define OCTAVE_US_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
|
589 bool operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
|
590 |
|
591 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
592 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ |
|
593 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ |
|
594 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ |
|
595 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ |
|
596 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
597 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
598 |
|
599 #define OCTAVE_SU_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
|
600 bool operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
|
601 |
|
602 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
603 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ |
|
604 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ |
|
605 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ |
|
606 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ |
|
607 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
608 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
609 |
|
610 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ |
|
611 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ |
|
612 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) |
|
613 |
5828
|
614 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072
|
615 |
4902
|
616 #undef OCTAVE_INT_BINOP_TRAIT |
5072
|
617 #undef OCTAVE_US_S_FTR |
|
618 #undef OCTAVE_US_S_FTR_FCNS |
|
619 #undef OCTAVE_S_US_FTR |
|
620 #undef OCTAVE_S_US_FTR_FCNS |
4902
|
621 #undef OCTAVE_INT_FIT_TO_RANGE |
|
622 #undef OCTAVE_INT_MIN_VAL2 |
|
623 #undef OCTAVE_INT_MAX_VAL2 |
|
624 #undef OCTAVE_INT_FIT_TO_RANGE2 |
|
625 #undef OCTAVE_INT_BIN_OP |
5072
|
626 #undef OCTAVE_INT_DOUBLE_BIN_OP |
|
627 #undef OCTAVE_DOUBLE_INT_BIN_OP |
|
628 #undef OCTAVE_INT_DOUBLE_CMP_OP |
|
629 #undef OCTAVE_DOUBLE_INT_CMP_OP |
|
630 #undef OCTAVE_INT_BITCMP_OP |
4902
|
631 #undef OCTAVE_INT_CMP_OP |
5072
|
632 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
|
633 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS |
|
634 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL |
|
635 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS |
|
636 #undef OCTAVE_TYPE1_CMP_OP_DECLS |
|
637 #undef OCTAVE_US_TYPE2_CMP_OP_DECL |
|
638 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS |
|
639 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL |
|
640 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS |
5074
|
641 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902
|
642 |
|
643 #endif |
|
644 |
|
645 /* |
|
646 ;;; Local Variables: *** |
|
647 ;;; mode: C++ *** |
|
648 ;;; End: *** |
|
649 */ |