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 { |
6764
|
298 ival = ival << x; |
4952
|
299 return *this; |
|
300 } |
|
301 |
|
302 template <class T2> |
|
303 octave_int<T>& operator >>= (const T2& x) |
|
304 { |
6764
|
305 if (ival < 0) |
|
306 ival = - (((-ival) >> x) & std::numeric_limits<T>::max()); |
|
307 else |
|
308 ival = ival >> x; |
4952
|
309 return *this; |
|
310 } |
|
311 |
4906
|
312 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
|
313 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } |
|
314 |
4919
|
315 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909
|
316 |
4949
|
317 static int byte_size (void) { return sizeof(T); } |
|
318 |
5900
|
319 // Unsafe. This function exists to support the MEX interface. |
|
320 // You should not use it anywhere else. |
|
321 void *mex_get_data (void) const { return const_cast<T *> (&ival); } |
|
322 |
4902
|
323 private: |
|
324 |
|
325 T ival; |
|
326 }; |
|
327 |
|
328 template <class T> |
4953
|
329 octave_int<T> |
|
330 pow (const octave_int<T>& a, const octave_int<T>& b) |
4952
|
331 { |
4953
|
332 octave_int<T> retval; |
4952
|
333 |
4953
|
334 octave_int<T> zero = octave_int<T> (0); |
|
335 octave_int<T> one = octave_int<T> (1); |
4952
|
336 |
|
337 if (b == zero) |
|
338 retval = one; |
|
339 else if (b < zero) |
|
340 retval = zero; |
|
341 else |
|
342 { |
4953
|
343 octave_int<T> a_val = a; |
|
344 octave_int<T> b_val = b; |
4952
|
345 |
|
346 retval = a; |
|
347 |
|
348 b_val -= 1; |
|
349 |
5030
|
350 while (b_val != zero) |
4952
|
351 { |
5030
|
352 if ((b_val & one) != zero) |
4952
|
353 retval = retval * a_val; |
|
354 |
|
355 b_val = b_val >> 1; |
|
356 |
|
357 if (b_val > zero) |
|
358 a_val = a_val * a_val; |
|
359 } |
|
360 } |
|
361 |
|
362 return retval; |
|
363 } |
|
364 |
|
365 template <class T> |
4953
|
366 octave_int<T> |
|
367 pow (double a, const octave_int<T>& b) |
|
368 { |
|
369 double tb = static_cast<double> (b.value ()); |
|
370 double r = pow (a, tb); |
4968
|
371 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
372 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
373 } |
|
374 |
|
375 template <class T> |
|
376 octave_int<T> |
|
377 pow (const octave_int<T>& a, double b) |
|
378 { |
|
379 double ta = static_cast<double> (a.value ()); |
|
380 double r = pow (ta, b); |
4968
|
381 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953
|
382 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
|
383 } |
|
384 |
|
385 template <class T> |
4902
|
386 std::ostream& |
|
387 operator << (std::ostream& os, const octave_int<T>& ival) |
|
388 { |
|
389 os << ival.value (); |
|
390 return os; |
|
391 } |
|
392 |
|
393 template <class T> |
|
394 std::istream& |
|
395 operator >> (std::istream& is, octave_int<T>& ival) |
|
396 { |
|
397 T tmp = 0; |
|
398 is >> tmp; |
|
399 ival = tmp; |
|
400 return is; |
|
401 } |
|
402 |
5828
|
403 typedef octave_int<int8_t> octave_int8; |
|
404 typedef octave_int<int16_t> octave_int16; |
|
405 typedef octave_int<int32_t> octave_int32; |
|
406 typedef octave_int<int64_t> octave_int64; |
4902
|
407 |
5828
|
408 typedef octave_int<uint8_t> octave_uint8; |
|
409 typedef octave_int<uint16_t> octave_uint16; |
|
410 typedef octave_int<uint32_t> octave_uint32; |
|
411 typedef octave_int<uint64_t> octave_uint64; |
4902
|
412 |
|
413 #define OCTAVE_INT_BIN_OP(OP) \ |
|
414 template <class T1, class T2> \ |
|
415 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ |
|
416 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
417 { \ |
|
418 double tx = static_cast<double> (x.value ()); \ |
|
419 double ty = static_cast<double> (y.value ()); \ |
|
420 double r = tx OP ty; \ |
|
421 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ |
4906
|
422 } |
4902
|
423 |
|
424 OCTAVE_INT_BIN_OP(+) |
|
425 OCTAVE_INT_BIN_OP(-) |
|
426 OCTAVE_INT_BIN_OP(*) |
4953
|
427 |
|
428 template <class T1, class T2> |
|
429 octave_int<typename octave_int_binop_traits<T1, T2>::TR> |
|
430 operator / (const octave_int<T1>& x, const octave_int<T2>& y) |
|
431 { |
|
432 double tx = static_cast<double> (x.value ()); |
|
433 double ty = static_cast<double> (y.value ()); |
6402
|
434 double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty); |
4953
|
435 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
|
436 } |
|
437 |
|
438 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
|
439 template <class T> \ |
|
440 octave_int<T> \ |
|
441 operator OP (const octave_int<T>& x, double y) \ |
|
442 { \ |
|
443 double tx = static_cast<double> (x.value ()); \ |
4968
|
444 double r = xround (tx OP y); \ |
|
445 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
446 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
447 } |
|
448 |
|
449 OCTAVE_INT_DOUBLE_BIN_OP(+) |
|
450 OCTAVE_INT_DOUBLE_BIN_OP(-) |
|
451 OCTAVE_INT_DOUBLE_BIN_OP(*) |
|
452 OCTAVE_INT_DOUBLE_BIN_OP(/) |
|
453 |
|
454 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ |
|
455 template <class T> \ |
|
456 octave_int<T> \ |
|
457 operator OP (double x, const octave_int<T>& y) \ |
|
458 { \ |
|
459 double ty = static_cast<double> (y.value ()); \ |
|
460 double r = x OP ty; \ |
4968
|
461 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953
|
462 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
|
463 } |
|
464 |
|
465 OCTAVE_DOUBLE_INT_BIN_OP(+) |
|
466 OCTAVE_DOUBLE_INT_BIN_OP(-) |
|
467 OCTAVE_DOUBLE_INT_BIN_OP(*) |
|
468 OCTAVE_DOUBLE_INT_BIN_OP(/) |
4902
|
469 |
5029
|
470 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
|
471 template <class T> \ |
|
472 bool \ |
|
473 operator OP (const octave_int<T>& x, const double& y) \ |
|
474 { \ |
|
475 double tx = static_cast<double> (x.value ()); \ |
5030
|
476 return tx OP y; \ |
5029
|
477 } |
|
478 |
|
479 OCTAVE_INT_DOUBLE_CMP_OP (<) |
|
480 OCTAVE_INT_DOUBLE_CMP_OP (<=) |
|
481 OCTAVE_INT_DOUBLE_CMP_OP (>=) |
|
482 OCTAVE_INT_DOUBLE_CMP_OP (>) |
|
483 OCTAVE_INT_DOUBLE_CMP_OP (==) |
|
484 OCTAVE_INT_DOUBLE_CMP_OP (!=) |
|
485 |
|
486 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ |
|
487 template <class T> \ |
|
488 bool \ |
|
489 operator OP (const double& x, const octave_int<T>& y) \ |
|
490 { \ |
|
491 double ty = static_cast<double> (y.value ()); \ |
5030
|
492 return x OP ty; \ |
5029
|
493 } |
|
494 |
|
495 OCTAVE_DOUBLE_INT_CMP_OP (<) |
|
496 OCTAVE_DOUBLE_INT_CMP_OP (<=) |
|
497 OCTAVE_DOUBLE_INT_CMP_OP (>=) |
|
498 OCTAVE_DOUBLE_INT_CMP_OP (>) |
|
499 OCTAVE_DOUBLE_INT_CMP_OP (==) |
|
500 OCTAVE_DOUBLE_INT_CMP_OP (!=) |
|
501 |
4906
|
502 #define OCTAVE_INT_BITCMP_OP(OP) \ |
|
503 template <class T> \ |
|
504 octave_int<T> \ |
|
505 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ |
|
506 { \ |
|
507 return x.value () OP y.value (); \ |
|
508 } |
|
509 |
|
510 OCTAVE_INT_BITCMP_OP (&) |
|
511 OCTAVE_INT_BITCMP_OP (|) |
|
512 OCTAVE_INT_BITCMP_OP (^) |
|
513 |
4952
|
514 template <class T1, class T2> |
|
515 octave_int<T1> |
|
516 operator << (const octave_int<T1>& x, const T2& y) |
|
517 { |
5030
|
518 octave_int<T1> retval = x; |
4952
|
519 return retval <<= y; |
|
520 } |
4906
|
521 |
4952
|
522 template <class T1, class T2> |
|
523 octave_int<T1> |
|
524 operator >> (const octave_int<T1>& x, const T2& y) |
|
525 { |
5030
|
526 octave_int<T1> retval = x; |
4952
|
527 return retval >>= y; |
|
528 } |
4906
|
529 |
4909
|
530 template <class T> |
|
531 octave_int<T> |
4920
|
532 bitshift (const octave_int<T>& a, int n, |
|
533 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909
|
534 { |
|
535 if (n > 0) |
4952
|
536 return (a << n) & mask; |
4909
|
537 else if (n < 0) |
4952
|
538 return (a >> -n) & mask; |
4909
|
539 else |
|
540 return a; |
|
541 } |
|
542 |
4902
|
543 #define OCTAVE_INT_CMP_OP(OP) \ |
|
544 template <class T1, class T2> \ |
|
545 bool \ |
|
546 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
|
547 { \ |
5072
|
548 return x.value () OP y.value (); \ |
4906
|
549 } |
4902
|
550 |
|
551 OCTAVE_INT_CMP_OP (<) |
|
552 OCTAVE_INT_CMP_OP (<=) |
|
553 OCTAVE_INT_CMP_OP (>=) |
|
554 OCTAVE_INT_CMP_OP (>) |
|
555 OCTAVE_INT_CMP_OP (==) |
|
556 OCTAVE_INT_CMP_OP (!=) |
|
557 |
5072
|
558 // The following apply if the unsigned type is at least as wide as the |
|
559 // signed type (then we can cast postive signed values to the unsigned |
|
560 // type and compare). |
|
561 |
|
562 #define OCTAVE_US_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
6108
|
563 bool OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072
|
564 |
|
565 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
566 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ |
|
567 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ |
|
568 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ |
|
569 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ |
|
570 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ |
|
571 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) |
|
572 |
|
573 #define OCTAVE_SU_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
6108
|
574 bool OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072
|
575 |
|
576 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ |
|
577 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ |
|
578 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ |
|
579 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ |
|
580 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ |
|
581 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ |
|
582 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) |
|
583 |
|
584 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ |
|
585 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ |
|
586 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) |
|
587 |
5828
|
588 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
|
589 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) |
|
590 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) |
5072
|
591 |
5828
|
592 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
|
593 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) |
|
594 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) |
|
595 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) |
5072
|
596 |
|
597 // The following apply if the signed type is wider than the unsigned |
|
598 // type (then we can cast unsigned values to the signed type and |
|
599 // compare if the signed value is positive). |
|
600 |
|
601 #define OCTAVE_US_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ |
6108
|
602 bool OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072
|
603 |
|
604 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
605 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ |
|
606 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ |
|
607 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ |
|
608 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ |
|
609 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
610 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
611 |
|
612 #define OCTAVE_SU_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ |
6108
|
613 bool OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072
|
614 |
|
615 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ |
|
616 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ |
|
617 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ |
|
618 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ |
|
619 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ |
|
620 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ |
|
621 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) |
|
622 |
|
623 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ |
|
624 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ |
|
625 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) |
|
626 |
5828
|
627 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072
|
628 |
4902
|
629 #undef OCTAVE_INT_BINOP_TRAIT |
5072
|
630 #undef OCTAVE_US_S_FTR |
|
631 #undef OCTAVE_US_S_FTR_FCNS |
|
632 #undef OCTAVE_S_US_FTR |
|
633 #undef OCTAVE_S_US_FTR_FCNS |
4902
|
634 #undef OCTAVE_INT_FIT_TO_RANGE |
|
635 #undef OCTAVE_INT_MIN_VAL2 |
|
636 #undef OCTAVE_INT_MAX_VAL2 |
|
637 #undef OCTAVE_INT_FIT_TO_RANGE2 |
|
638 #undef OCTAVE_INT_BIN_OP |
5072
|
639 #undef OCTAVE_INT_DOUBLE_BIN_OP |
|
640 #undef OCTAVE_DOUBLE_INT_BIN_OP |
|
641 #undef OCTAVE_INT_DOUBLE_CMP_OP |
|
642 #undef OCTAVE_DOUBLE_INT_CMP_OP |
|
643 #undef OCTAVE_INT_BITCMP_OP |
4902
|
644 #undef OCTAVE_INT_CMP_OP |
5072
|
645 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
|
646 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS |
|
647 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL |
|
648 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS |
|
649 #undef OCTAVE_TYPE1_CMP_OP_DECLS |
|
650 #undef OCTAVE_US_TYPE2_CMP_OP_DECL |
|
651 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS |
|
652 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL |
|
653 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS |
5074
|
654 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902
|
655 |
|
656 #endif |
|
657 |
|
658 /* |
|
659 ;;; Local Variables: *** |
|
660 ;;; mode: C++ *** |
|
661 ;;; End: *** |
|
662 */ |