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