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