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 |
5898
|
250 operator bool (void) const { return static_cast<bool> (value ()); } |
|
251 |
5533
|
252 operator char (void) const { return static_cast<char> (value ()); } |
|
253 |
4902
|
254 operator double (void) const { return static_cast<double> (value ()); } |
|
255 |
5030
|
256 operator float (void) const { return static_cast<float> (value ()); } |
|
257 |
4902
|
258 octave_int<T>& operator += (const octave_int<T>& x) |
|
259 { |
|
260 double t = static_cast<double> (value ()); |
|
261 double tx = static_cast<double> (x.value ()); |
|
262 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); |
|
263 return *this; |
|
264 } |
|
265 |
|
266 octave_int<T>& operator -= (const octave_int<T>& x) |
|
267 { |
|
268 double t = static_cast<double> (value ()); |
|
269 double tx = static_cast<double> (x.value ()); |
|
270 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); |
|
271 return *this; |
|
272 } |
|
273 |
4952
|
274 octave_int<T>& operator *= (const octave_int<T>& x) |
|
275 { |
|
276 double t = static_cast<double> (value ()); |
|
277 double tx = static_cast<double> (x.value ()); |
|
278 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); |
|
279 return *this; |
|
280 } |
|
281 |
|
282 octave_int<T>& operator /= (const octave_int<T>& x) |
|
283 { |
|
284 double t = static_cast<double> (value ()); |
|
285 double tx = static_cast<double> (x.value ()); |
4968
|
286 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953
|
287 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952
|
288 return *this; |
|
289 } |
|
290 |
|
291 template <class T2> |
|
292 octave_int<T>& operator <<= (const T2& x) |
|
293 { |
|
294 ival = ((ival << x) > std::numeric_limits<T>::max ()) ? 0 : (ival << x); |
|
295 return *this; |
|
296 } |
|
297 |
|
298 template <class T2> |
|
299 octave_int<T>& operator >>= (const T2& x) |
|
300 { |
|
301 ival >>= x; |
|
302 return *this; |
|
303 } |
|
304 |
4906
|
305 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
|
306 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } |
|
307 |
4919
|
308 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909
|
309 |
4949
|
310 static int byte_size (void) { return sizeof(T); } |
|
311 |
5900
|
312 // Unsafe. This function exists to support the MEX interface. |
|
313 // You should not use it anywhere else. |
|
314 void *mex_get_data (void) const { return const_cast<T *> (&ival); } |
|
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 |
5030
|
343 while (b_val != zero) |
4952
|
344 { |
5030
|
345 if ((b_val & one) != zero) |
4952
|
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 |
5828
|
396 typedef octave_int<int8_t> octave_int8; |
|
397 typedef octave_int<int16_t> octave_int16; |
|
398 typedef octave_int<int32_t> octave_int32; |
|
399 typedef octave_int<int64_t> octave_int64; |
4902
|
400 |
5828
|
401 typedef octave_int<uint8_t> octave_uint8; |
|
402 typedef octave_int<uint16_t> octave_uint16; |
|
403 typedef octave_int<uint32_t> octave_uint32; |
|
404 typedef octave_int<uint64_t> octave_uint64; |
4902
|
405 |
|
406 #define OCTAVE_INT_BIN_OP(OP) \ |
|
407 template <class T1, class T2> \ |
|
408 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ |
|
409 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
410 { \ |
|
411 double tx = static_cast<double> (x.value ()); \ |
|
412 double ty = static_cast<double> (y.value ()); \ |
|
413 double r = tx OP ty; \ |
|
414 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ |
4906
|
415 } |
4902
|
416 |
|
417 OCTAVE_INT_BIN_OP(+) |
|
418 OCTAVE_INT_BIN_OP(-) |
|
419 OCTAVE_INT_BIN_OP(*) |
4953
|
420 |
|
421 template <class T1, class T2> |
|
422 octave_int<typename octave_int_binop_traits<T1, T2>::TR> |
|
423 operator / (const octave_int<T1>& x, const octave_int<T2>& y) |
|
424 { |
|
425 double tx = static_cast<double> (x.value ()); |
|
426 double ty = static_cast<double> (y.value ()); |
|
427 double r = (tx == 0 && ty == 0) ? 0 : tx / ty; |
|
428 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
|
429 } |
|
430 |
|
431 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
|
432 template <class T> \ |
|
433 octave_int<T> \ |
|
434 operator OP (const octave_int<T>& x, double y) \ |
|
435 { \ |
|
436 double tx = static_cast<double> (x.value ()); \ |
4968
|
437 double r = xround (tx OP y); \ |
|
438 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
439 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
440 } |
|
441 |
|
442 OCTAVE_INT_DOUBLE_BIN_OP(+) |
|
443 OCTAVE_INT_DOUBLE_BIN_OP(-) |
|
444 OCTAVE_INT_DOUBLE_BIN_OP(*) |
|
445 OCTAVE_INT_DOUBLE_BIN_OP(/) |
|
446 |
|
447 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ |
|
448 template <class T> \ |
|
449 octave_int<T> \ |
|
450 operator OP (double x, const octave_int<T>& y) \ |
|
451 { \ |
|
452 double ty = static_cast<double> (y.value ()); \ |
|
453 double r = x OP ty; \ |
4968
|
454 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
455 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
456 } |
|
457 |
|
458 OCTAVE_DOUBLE_INT_BIN_OP(+) |
|
459 OCTAVE_DOUBLE_INT_BIN_OP(-) |
|
460 OCTAVE_DOUBLE_INT_BIN_OP(*) |
|
461 OCTAVE_DOUBLE_INT_BIN_OP(/) |
4902
|
462 |
5029
|
463 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
|
464 template <class T> \ |
|
465 bool \ |
|
466 operator OP (const octave_int<T>& x, const double& y) \ |
|
467 { \ |
|
468 double tx = static_cast<double> (x.value ()); \ |
5030
|
469 return tx OP y; \ |
5029
|
470 } |
|
471 |
|
472 OCTAVE_INT_DOUBLE_CMP_OP (<) |
|
473 OCTAVE_INT_DOUBLE_CMP_OP (<=) |
|
474 OCTAVE_INT_DOUBLE_CMP_OP (>=) |
|
475 OCTAVE_INT_DOUBLE_CMP_OP (>) |
|
476 OCTAVE_INT_DOUBLE_CMP_OP (==) |
|
477 OCTAVE_INT_DOUBLE_CMP_OP (!=) |
|
478 |
|
479 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ |
|
480 template <class T> \ |
|
481 bool \ |
|
482 operator OP (const double& x, const octave_int<T>& y) \ |
|
483 { \ |
|
484 double ty = static_cast<double> (y.value ()); \ |
5030
|
485 return x OP ty; \ |
5029
|
486 } |
|
487 |
|
488 OCTAVE_DOUBLE_INT_CMP_OP (<) |
|
489 OCTAVE_DOUBLE_INT_CMP_OP (<=) |
|
490 OCTAVE_DOUBLE_INT_CMP_OP (>=) |
|
491 OCTAVE_DOUBLE_INT_CMP_OP (>) |
|
492 OCTAVE_DOUBLE_INT_CMP_OP (==) |
|
493 OCTAVE_DOUBLE_INT_CMP_OP (!=) |
|
494 |
4906
|
495 #define OCTAVE_INT_BITCMP_OP(OP) \ |
|
496 template <class T> \ |
|
497 octave_int<T> \ |
|
498 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ |
|
499 { \ |
|
500 return x.value () OP y.value (); \ |
|
501 } |
|
502 |
|
503 OCTAVE_INT_BITCMP_OP (&) |
|
504 OCTAVE_INT_BITCMP_OP (|) |
|
505 OCTAVE_INT_BITCMP_OP (^) |
|
506 |
4952
|
507 template <class T1, class T2> |
|
508 octave_int<T1> |
|
509 operator << (const octave_int<T1>& x, const T2& y) |
|
510 { |
5030
|
511 octave_int<T1> retval = x; |
4952
|
512 return retval <<= y; |
|
513 } |
4906
|
514 |
4952
|
515 template <class T1, class T2> |
|
516 octave_int<T1> |
|
517 operator >> (const octave_int<T1>& x, const T2& y) |
|
518 { |
5030
|
519 octave_int<T1> retval = x; |
4952
|
520 return retval >>= y; |
|
521 } |
4906
|
522 |
4909
|
523 template <class T> |
|
524 octave_int<T> |
4920
|
525 bitshift (const octave_int<T>& a, int n, |
|
526 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909
|
527 { |
|
528 if (n > 0) |
4952
|
529 return (a << n) & mask; |
4909
|
530 else if (n < 0) |
4952
|
531 return (a >> -n) & mask; |
4909
|
532 else |
|
533 return a; |
|
534 } |
|
535 |
4902
|
536 #define OCTAVE_INT_CMP_OP(OP) \ |
|
537 template <class T1, class T2> \ |
|
538 bool \ |
|
539 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
540 { \ |
5072
|
541 return x.value () OP y.value (); \ |
4906
|
542 } |
4902
|
543 |
|
544 OCTAVE_INT_CMP_OP (<) |
|
545 OCTAVE_INT_CMP_OP (<=) |
|
546 OCTAVE_INT_CMP_OP (>=) |
|
547 OCTAVE_INT_CMP_OP (>) |
|
548 OCTAVE_INT_CMP_OP (==) |
|
549 OCTAVE_INT_CMP_OP (!=) |
|
550 |
5072
|
551 // The following apply if the unsigned type is at least as wide as the |
|
552 // signed type (then we can cast postive signed values to the unsigned |
|
553 // type and compare). |
|
554 |
|
555 #define OCTAVE_US_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
6108
|
556 bool OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072
|
557 |
|
558 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
559 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ |
|
560 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ |
|
561 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ |
|
562 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ |
|
563 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ |
|
564 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) |
|
565 |
|
566 #define OCTAVE_SU_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
6108
|
567 bool OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072
|
568 |
|
569 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ |
|
570 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ |
|
571 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ |
|
572 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ |
|
573 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ |
|
574 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ |
|
575 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) |
|
576 |
|
577 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
578 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ |
|
579 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) |
|
580 |
5828
|
581 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
|
582 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) |
|
583 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) |
5072
|
584 |
5828
|
585 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
|
586 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) |
|
587 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) |
|
588 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) |
5072
|
589 |
|
590 // The following apply if the signed type is wider than the unsigned |
|
591 // type (then we can cast unsigned values to the signed type and |
|
592 // compare if the signed value is positive). |
|
593 |
|
594 #define OCTAVE_US_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
6108
|
595 bool OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072
|
596 |
|
597 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
598 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ |
|
599 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ |
|
600 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ |
|
601 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ |
|
602 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
603 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
604 |
|
605 #define OCTAVE_SU_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
6108
|
606 bool OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072
|
607 |
|
608 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
609 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ |
|
610 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ |
|
611 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ |
|
612 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ |
|
613 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
614 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
615 |
|
616 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ |
|
617 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ |
|
618 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) |
|
619 |
5828
|
620 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072
|
621 |
4902
|
622 #undef OCTAVE_INT_BINOP_TRAIT |
5072
|
623 #undef OCTAVE_US_S_FTR |
|
624 #undef OCTAVE_US_S_FTR_FCNS |
|
625 #undef OCTAVE_S_US_FTR |
|
626 #undef OCTAVE_S_US_FTR_FCNS |
4902
|
627 #undef OCTAVE_INT_FIT_TO_RANGE |
|
628 #undef OCTAVE_INT_MIN_VAL2 |
|
629 #undef OCTAVE_INT_MAX_VAL2 |
|
630 #undef OCTAVE_INT_FIT_TO_RANGE2 |
|
631 #undef OCTAVE_INT_BIN_OP |
5072
|
632 #undef OCTAVE_INT_DOUBLE_BIN_OP |
|
633 #undef OCTAVE_DOUBLE_INT_BIN_OP |
|
634 #undef OCTAVE_INT_DOUBLE_CMP_OP |
|
635 #undef OCTAVE_DOUBLE_INT_CMP_OP |
|
636 #undef OCTAVE_INT_BITCMP_OP |
4902
|
637 #undef OCTAVE_INT_CMP_OP |
5072
|
638 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
|
639 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS |
|
640 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL |
|
641 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS |
|
642 #undef OCTAVE_TYPE1_CMP_OP_DECLS |
|
643 #undef OCTAVE_US_TYPE2_CMP_OP_DECL |
|
644 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS |
|
645 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL |
|
646 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS |
5074
|
647 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902
|
648 |
|
649 #endif |
|
650 |
|
651 /* |
|
652 ;;; Local Variables: *** |
|
653 ;;; mode: C++ *** |
|
654 ;;; End: *** |
|
655 */ |