Mercurial > octave-nkf
annotate liboctave/oct-inttypes.h @ 7596:6929e40fc597
compatible handling of NaN -> int conversions
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 18 Mar 2008 14:46:15 -0400 |
parents | ef755c763b62 |
children | a89b3fa632ee |
rev | line source |
---|---|
4902 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 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 | |
7596
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
131 #define OCTAVE_DBL_FTR(T) \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
132 template <> \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
133 inline T \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
134 octave_int_fit_to_range<double, T> (const double& x, const T& mn, \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
135 const T& mx) \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
136 { \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
137 return (lo_ieee_isnan (x) ? 0 : (x > mx ? mx : (x < mn ? mn : static_cast<T> (x)))); \ |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
138 } |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
139 |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
140 OCTAVE_DBL_FTR (char) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
141 OCTAVE_DBL_FTR (short) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
142 OCTAVE_DBL_FTR (int) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
143 OCTAVE_DBL_FTR (long) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
144 OCTAVE_DBL_FTR (long long) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
145 |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
146 OCTAVE_DBL_FTR (unsigned char) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
147 OCTAVE_DBL_FTR (unsigned short) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
148 OCTAVE_DBL_FTR (unsigned int) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
149 OCTAVE_DBL_FTR (unsigned long) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
150 OCTAVE_DBL_FTR (unsigned long long) |
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
151 |
4943 | 152 // If X is unsigned and the new type is signed, then we only have to |
153 // check the upper limit, but we should cast the maximum value of the | |
154 // new type to an unsigned type before performing the comparison. | |
155 // This should always be OK because the maximum value should always be | |
156 // positive. | |
157 | |
5072 | 158 #define OCTAVE_US_S_FTR(T1, T2, TC) \ |
4943 | 159 template <> \ |
160 inline T2 \ | |
161 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2& mx) \ | |
162 { \ | |
163 return x > static_cast<TC> (mx) ? mx : x; \ | |
164 } | |
165 | |
5072 | 166 #define OCTAVE_US_S_FTR_FCNS(T) \ |
167 OCTAVE_US_S_FTR (T, char, unsigned char) \ | |
168 OCTAVE_US_S_FTR (T, signed char, unsigned char) \ | |
169 OCTAVE_US_S_FTR (T, short, unsigned short) \ | |
170 OCTAVE_US_S_FTR (T, int, unsigned int) \ | |
171 OCTAVE_US_S_FTR (T, long, unsigned long) \ | |
172 OCTAVE_US_S_FTR (T, long long, unsigned long long) | |
4943 | 173 |
5072 | 174 OCTAVE_US_S_FTR_FCNS (unsigned char) |
175 OCTAVE_US_S_FTR_FCNS (unsigned short) | |
176 OCTAVE_US_S_FTR_FCNS (unsigned int) | |
177 OCTAVE_US_S_FTR_FCNS (unsigned long) | |
178 OCTAVE_US_S_FTR_FCNS (unsigned long long) | |
4943 | 179 |
180 // If X is signed and the new type is unsigned, then we only have to | |
181 // check the lower limit (which will always be 0 for an unsigned | |
182 // type). The upper limit will be enforced correctly by converting to | |
183 // the new type, even if the type of X is wider than the new type. | |
184 | |
5072 | 185 #define OCTAVE_S_US_FTR(T1, T2) \ |
4943 | 186 template <> \ |
187 inline T2 \ | |
188 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2&) \ | |
189 { \ | |
5420 | 190 return x <= 0 ? 0 : x; \ |
4943 | 191 } |
192 | |
5072 | 193 #define OCTAVE_S_US_FTR_FCNS(T) \ |
194 OCTAVE_S_US_FTR (T, unsigned char) \ | |
195 OCTAVE_S_US_FTR (T, unsigned short) \ | |
196 OCTAVE_S_US_FTR (T, unsigned int) \ | |
197 OCTAVE_S_US_FTR (T, unsigned long) \ | |
198 OCTAVE_S_US_FTR (T, unsigned long long) | |
4943 | 199 |
5072 | 200 OCTAVE_S_US_FTR_FCNS (char) |
201 OCTAVE_S_US_FTR_FCNS (signed char) | |
202 OCTAVE_S_US_FTR_FCNS (short) | |
203 OCTAVE_S_US_FTR_FCNS (int) | |
204 OCTAVE_S_US_FTR_FCNS (long) | |
205 OCTAVE_S_US_FTR_FCNS (long long) | |
4943 | 206 |
4902 | 207 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \ |
4943 | 208 octave_int_fit_to_range (r, \ |
209 std::numeric_limits<T>::min (), \ | |
210 std::numeric_limits<T>::max ()) | |
4902 | 211 |
212 #define OCTAVE_INT_MIN_VAL2(T1, T2) \ | |
213 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min () | |
214 | |
215 #define OCTAVE_INT_MAX_VAL2(T1, T2) \ | |
216 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::max () | |
217 | |
218 #define OCTAVE_INT_FIT_TO_RANGE2(r, T1, T2) \ | |
219 octave_int_fit_to_range (r, \ | |
220 OCTAVE_INT_MIN_VAL2 (T1, T2), \ | |
221 OCTAVE_INT_MAX_VAL2 (T1, T2)) | |
222 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7521
diff
changeset
|
223 // We have all the machinery below (octave_int_helper) to avoid a few |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7521
diff
changeset
|
224 // warnings from GCC about comparisons always false due to limited |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7521
diff
changeset
|
225 // range of data types. Ugh. The cure may be worse than the disease. |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
226 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
227 // FIXME -- it would be nice to nest the helper class inside the |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
228 // octave_int class, but I don't see the magic for that at the moment. |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
229 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
230 template <class T> class octave_int; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
231 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
232 template <class T, bool is_signed> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
233 class octave_int_helper |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
234 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
235 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
236 static octave_int<T> abs (const T& x); |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
237 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
238 static octave_int<T> signum (const T& x); |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
239 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
240 template <class T2> static void rshift_eq (T& ival, const T2& x); |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
241 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
242 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
243 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
244 class octave_int_helper<T, false> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
245 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
246 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
247 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
248 abs (const T& x) { return x; } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
249 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
250 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
251 signum (const T& x) { return x > 0 ? 1 : 0; } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
252 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
253 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
254 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
255 rshift_eq (T& ival, const T2& x) { ival = ival >> x; } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
256 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
257 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
258 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
259 class octave_int_helper<T, true> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
260 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
261 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
262 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
263 abs (const T& x) { return x < 0 ? -x : x; } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
264 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
265 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
266 signum (const T& x) { return x < 0 ? -1 : (x > 0 ? 1 : 0); } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
267 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
268 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
269 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
270 rshift_eq (T& ival, const T2& x) |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
271 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
272 if (ival < 0) |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
273 ival = - (((-ival) >> x) & std::numeric_limits<T>::max()); |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
274 else |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
275 ival = ival >> x; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
276 } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
277 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
278 |
4902 | 279 template <class T> |
280 class | |
281 octave_int | |
282 { | |
283 public: | |
284 | |
4943 | 285 typedef T val_type; |
286 | |
4902 | 287 octave_int (void) : ival () { } |
288 | |
289 template <class U> | |
290 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { } | |
291 | |
6402 | 292 octave_int (double d) : ival (OCTAVE_INT_FIT_TO_RANGE (xround (d), T)) { } |
293 | |
4902 | 294 octave_int (bool b) : ival (b) { } |
295 | |
296 template <class U> | |
297 octave_int (const octave_int<U>& i) | |
298 : ival (OCTAVE_INT_FIT_TO_RANGE (i.value (), T)) { } | |
299 | |
300 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
301 | |
302 octave_int& operator = (const octave_int<T>& i) | |
303 { | |
304 ival = i.ival; | |
305 return *this; | |
306 } | |
307 | |
308 ~octave_int (void) { } | |
309 | |
310 T value (void) const { return ival; } | |
311 | |
4949 | 312 const unsigned char * iptr (void) const |
313 { return reinterpret_cast<const unsigned char *> (& ival); } | |
314 | |
4902 | 315 bool operator ! (void) const { return ! ival; } |
316 | |
4906 | 317 octave_int<T> operator + (void) const { return *this; } |
4902 | 318 |
4906 | 319 octave_int<T> operator - (void) const |
4902 | 320 { |
4953 | 321 // Can't just return -ival because signed types are not |
322 // symmetric, which causes things like -intmin("int32") to be the | |
323 // same as intmin("int32") instead of intmax("int32") (which is | |
324 // what we should get with saturation semantics). | |
325 | |
326 return std::numeric_limits<T>::is_signed ? | |
327 OCTAVE_INT_FIT_TO_RANGE (- static_cast<double> (ival), T) : 0; | |
4902 | 328 } |
329 | |
7198 | 330 bool bool_value (void) const { return static_cast<bool> (value ()); } |
331 | |
332 char char_value (void) const { return static_cast<char> (value ()); } | |
333 | |
334 double double_value (void) const { return static_cast<double> (value ()); } | |
335 | |
336 float float_value (void) const { return static_cast<float> (value ()); } | |
337 | |
7177 | 338 operator T (void) const { return value (); } |
339 | |
7198 | 340 // char and bool operators intentionally omitted. |
5533 | 341 |
7198 | 342 operator double (void) const { return double_value (); } |
4902 | 343 |
7198 | 344 operator float (void) const { return float_value (); } |
5030 | 345 |
4902 | 346 octave_int<T>& operator += (const octave_int<T>& x) |
347 { | |
348 double t = static_cast<double> (value ()); | |
349 double tx = static_cast<double> (x.value ()); | |
350 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); | |
351 return *this; | |
352 } | |
353 | |
354 octave_int<T>& operator -= (const octave_int<T>& x) | |
355 { | |
356 double t = static_cast<double> (value ()); | |
357 double tx = static_cast<double> (x.value ()); | |
358 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); | |
359 return *this; | |
360 } | |
361 | |
4952 | 362 octave_int<T>& operator *= (const octave_int<T>& x) |
363 { | |
364 double t = static_cast<double> (value ()); | |
365 double tx = static_cast<double> (x.value ()); | |
366 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); | |
367 return *this; | |
368 } | |
369 | |
370 octave_int<T>& operator /= (const octave_int<T>& x) | |
371 { | |
372 double t = static_cast<double> (value ()); | |
373 double tx = static_cast<double> (x.value ()); | |
4968 | 374 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953 | 375 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952 | 376 return *this; |
377 } | |
378 | |
379 template <class T2> | |
380 octave_int<T>& operator <<= (const T2& x) | |
381 { | |
6764 | 382 ival = ival << x; |
4952 | 383 return *this; |
384 } | |
385 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
386 // Use helper functions in the operator >>=, abs, and signum |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
387 // functions to avoid "comparison of unsigned expression < 0 is |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
388 // always false" warnings from GCC when instantiating these funtions |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
389 // for unsigned types. |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
390 |
4952 | 391 template <class T2> |
392 octave_int<T>& operator >>= (const T2& x) | |
393 { | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
394 octave_int_helper<T, std::numeric_limits<T>::is_signed>::rshift_eq (ival, x); |
4952 | 395 return *this; |
396 } | |
397 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
398 octave_int<T> abs (void) const |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
399 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
400 return octave_int_helper<T, std::numeric_limits<T>::is_signed>::abs (value ()); |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
401 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
402 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
403 octave_int<T> signum (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
404 { |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
405 return octave_int_helper<T, std::numeric_limits<T>::is_signed>::signum (value ()); |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
406 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
407 |
4906 | 408 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
409 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } | |
410 | |
4919 | 411 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909 | 412 |
4949 | 413 static int byte_size (void) { return sizeof(T); } |
414 | |
5900 | 415 // Unsafe. This function exists to support the MEX interface. |
416 // You should not use it anywhere else. | |
417 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
418 | |
4902 | 419 private: |
420 | |
421 T ival; | |
422 }; | |
423 | |
424 template <class T> | |
4953 | 425 octave_int<T> |
426 pow (const octave_int<T>& a, const octave_int<T>& b) | |
4952 | 427 { |
4953 | 428 octave_int<T> retval; |
4952 | 429 |
4953 | 430 octave_int<T> zero = octave_int<T> (0); |
431 octave_int<T> one = octave_int<T> (1); | |
4952 | 432 |
433 if (b == zero) | |
434 retval = one; | |
435 else if (b < zero) | |
436 retval = zero; | |
437 else | |
438 { | |
4953 | 439 octave_int<T> a_val = a; |
440 octave_int<T> b_val = b; | |
4952 | 441 |
442 retval = a; | |
443 | |
444 b_val -= 1; | |
445 | |
5030 | 446 while (b_val != zero) |
4952 | 447 { |
5030 | 448 if ((b_val & one) != zero) |
4952 | 449 retval = retval * a_val; |
450 | |
451 b_val = b_val >> 1; | |
452 | |
453 if (b_val > zero) | |
454 a_val = a_val * a_val; | |
455 } | |
456 } | |
457 | |
458 return retval; | |
459 } | |
460 | |
461 template <class T> | |
4953 | 462 octave_int<T> |
463 pow (double a, const octave_int<T>& b) | |
464 { | |
465 double tb = static_cast<double> (b.value ()); | |
466 double r = pow (a, tb); | |
4968 | 467 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 468 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
469 } | |
470 | |
471 template <class T> | |
472 octave_int<T> | |
473 pow (const octave_int<T>& a, double b) | |
474 { | |
475 double ta = static_cast<double> (a.value ()); | |
476 double r = pow (ta, b); | |
4968 | 477 r = lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 478 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
479 } | |
480 | |
481 template <class T> | |
4902 | 482 std::ostream& |
483 operator << (std::ostream& os, const octave_int<T>& ival) | |
484 { | |
485 os << ival.value (); | |
486 return os; | |
487 } | |
488 | |
489 template <class T> | |
490 std::istream& | |
491 operator >> (std::istream& is, octave_int<T>& ival) | |
492 { | |
493 T tmp = 0; | |
494 is >> tmp; | |
495 ival = tmp; | |
496 return is; | |
497 } | |
498 | |
5828 | 499 typedef octave_int<int8_t> octave_int8; |
500 typedef octave_int<int16_t> octave_int16; | |
501 typedef octave_int<int32_t> octave_int32; | |
502 typedef octave_int<int64_t> octave_int64; | |
4902 | 503 |
5828 | 504 typedef octave_int<uint8_t> octave_uint8; |
505 typedef octave_int<uint16_t> octave_uint16; | |
506 typedef octave_int<uint32_t> octave_uint32; | |
507 typedef octave_int<uint64_t> octave_uint64; | |
4902 | 508 |
509 #define OCTAVE_INT_BIN_OP(OP) \ | |
510 template <class T1, class T2> \ | |
511 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ | |
512 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
513 { \ | |
514 double tx = static_cast<double> (x.value ()); \ | |
515 double ty = static_cast<double> (y.value ()); \ | |
516 double r = tx OP ty; \ | |
517 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ | |
4906 | 518 } |
4902 | 519 |
520 OCTAVE_INT_BIN_OP(+) | |
521 OCTAVE_INT_BIN_OP(-) | |
522 OCTAVE_INT_BIN_OP(*) | |
4953 | 523 |
524 template <class T1, class T2> | |
525 octave_int<typename octave_int_binop_traits<T1, T2>::TR> | |
526 operator / (const octave_int<T1>& x, const octave_int<T2>& y) | |
527 { | |
528 double tx = static_cast<double> (x.value ()); | |
529 double ty = static_cast<double> (y.value ()); | |
6402 | 530 double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty); |
4953 | 531 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
532 } | |
533 | |
534 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ | |
535 template <class T> \ | |
536 octave_int<T> \ | |
537 operator OP (const octave_int<T>& x, double y) \ | |
538 { \ | |
539 double tx = static_cast<double> (x.value ()); \ | |
4968 | 540 double r = xround (tx OP y); \ |
541 r = lo_ieee_isnan (r) ? 0 : xround (r); \ | |
4953 | 542 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
543 } | |
544 | |
545 OCTAVE_INT_DOUBLE_BIN_OP(+) | |
546 OCTAVE_INT_DOUBLE_BIN_OP(-) | |
547 OCTAVE_INT_DOUBLE_BIN_OP(*) | |
548 OCTAVE_INT_DOUBLE_BIN_OP(/) | |
549 | |
550 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ | |
551 template <class T> \ | |
552 octave_int<T> \ | |
553 operator OP (double x, const octave_int<T>& y) \ | |
554 { \ | |
555 double ty = static_cast<double> (y.value ()); \ | |
556 double r = x OP ty; \ | |
4968 | 557 r = lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953 | 558 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
559 } | |
560 | |
561 OCTAVE_DOUBLE_INT_BIN_OP(+) | |
562 OCTAVE_DOUBLE_INT_BIN_OP(-) | |
563 OCTAVE_DOUBLE_INT_BIN_OP(*) | |
564 OCTAVE_DOUBLE_INT_BIN_OP(/) | |
4902 | 565 |
5029 | 566 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
567 template <class T> \ | |
568 bool \ | |
569 operator OP (const octave_int<T>& x, const double& y) \ | |
570 { \ | |
571 double tx = static_cast<double> (x.value ()); \ | |
5030 | 572 return tx OP y; \ |
5029 | 573 } |
574 | |
575 OCTAVE_INT_DOUBLE_CMP_OP (<) | |
576 OCTAVE_INT_DOUBLE_CMP_OP (<=) | |
577 OCTAVE_INT_DOUBLE_CMP_OP (>=) | |
578 OCTAVE_INT_DOUBLE_CMP_OP (>) | |
579 OCTAVE_INT_DOUBLE_CMP_OP (==) | |
580 OCTAVE_INT_DOUBLE_CMP_OP (!=) | |
581 | |
582 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ | |
583 template <class T> \ | |
584 bool \ | |
585 operator OP (const double& x, const octave_int<T>& y) \ | |
586 { \ | |
587 double ty = static_cast<double> (y.value ()); \ | |
5030 | 588 return x OP ty; \ |
5029 | 589 } |
590 | |
591 OCTAVE_DOUBLE_INT_CMP_OP (<) | |
592 OCTAVE_DOUBLE_INT_CMP_OP (<=) | |
593 OCTAVE_DOUBLE_INT_CMP_OP (>=) | |
594 OCTAVE_DOUBLE_INT_CMP_OP (>) | |
595 OCTAVE_DOUBLE_INT_CMP_OP (==) | |
596 OCTAVE_DOUBLE_INT_CMP_OP (!=) | |
597 | |
4906 | 598 #define OCTAVE_INT_BITCMP_OP(OP) \ |
599 template <class T> \ | |
600 octave_int<T> \ | |
601 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ | |
602 { \ | |
603 return x.value () OP y.value (); \ | |
604 } | |
605 | |
606 OCTAVE_INT_BITCMP_OP (&) | |
607 OCTAVE_INT_BITCMP_OP (|) | |
608 OCTAVE_INT_BITCMP_OP (^) | |
609 | |
4952 | 610 template <class T1, class T2> |
611 octave_int<T1> | |
612 operator << (const octave_int<T1>& x, const T2& y) | |
613 { | |
5030 | 614 octave_int<T1> retval = x; |
4952 | 615 return retval <<= y; |
616 } | |
4906 | 617 |
4952 | 618 template <class T1, class T2> |
619 octave_int<T1> | |
620 operator >> (const octave_int<T1>& x, const T2& y) | |
621 { | |
5030 | 622 octave_int<T1> retval = x; |
4952 | 623 return retval >>= y; |
624 } | |
4906 | 625 |
4909 | 626 template <class T> |
627 octave_int<T> | |
4920 | 628 bitshift (const octave_int<T>& a, int n, |
629 const octave_int<T>& mask = std::numeric_limits<T>::max ()) | |
4909 | 630 { |
631 if (n > 0) | |
4952 | 632 return (a << n) & mask; |
4909 | 633 else if (n < 0) |
4952 | 634 return (a >> -n) & mask; |
4909 | 635 else |
636 return a; | |
637 } | |
638 | |
4902 | 639 #define OCTAVE_INT_CMP_OP(OP) \ |
640 template <class T1, class T2> \ | |
641 bool \ | |
642 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
643 { \ | |
5072 | 644 return x.value () OP y.value (); \ |
4906 | 645 } |
4902 | 646 |
647 OCTAVE_INT_CMP_OP (<) | |
648 OCTAVE_INT_CMP_OP (<=) | |
649 OCTAVE_INT_CMP_OP (>=) | |
650 OCTAVE_INT_CMP_OP (>) | |
651 OCTAVE_INT_CMP_OP (==) | |
652 OCTAVE_INT_CMP_OP (!=) | |
653 | |
5072 | 654 // The following apply if the unsigned type is at least as wide as the |
655 // signed type (then we can cast postive signed values to the unsigned | |
656 // type and compare). | |
657 | |
658 #define OCTAVE_US_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
659 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
660 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
661 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 662 |
663 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
664 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ | |
665 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ | |
666 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ | |
667 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ | |
668 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ | |
669 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) | |
670 | |
671 #define OCTAVE_SU_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
672 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
673 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
674 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 675 |
676 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ | |
677 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ | |
678 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ | |
679 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ | |
680 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ | |
681 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ | |
682 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) | |
683 | |
684 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
685 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ | |
686 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) | |
687 | |
5828 | 688 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
689 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) | |
690 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) | |
5072 | 691 |
5828 | 692 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
693 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) | |
694 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) | |
695 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) | |
5072 | 696 |
697 // The following apply if the signed type is wider than the unsigned | |
698 // type (then we can cast unsigned values to the signed type and | |
699 // compare if the signed value is positive). | |
700 | |
701 #define OCTAVE_US_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
702 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
703 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
704 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 705 |
706 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
707 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ | |
708 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ | |
709 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ | |
710 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ | |
711 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
712 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
713 | |
714 #define OCTAVE_SU_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
715 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
716 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
717 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 718 |
719 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
720 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ | |
721 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ | |
722 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ | |
723 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ | |
724 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
725 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
726 | |
727 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ | |
728 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ | |
729 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) | |
730 | |
5828 | 731 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072 | 732 |
4902 | 733 #undef OCTAVE_INT_BINOP_TRAIT |
5072 | 734 #undef OCTAVE_US_S_FTR |
735 #undef OCTAVE_US_S_FTR_FCNS | |
736 #undef OCTAVE_S_US_FTR | |
737 #undef OCTAVE_S_US_FTR_FCNS | |
4902 | 738 #undef OCTAVE_INT_FIT_TO_RANGE |
739 #undef OCTAVE_INT_MIN_VAL2 | |
740 #undef OCTAVE_INT_MAX_VAL2 | |
741 #undef OCTAVE_INT_FIT_TO_RANGE2 | |
742 #undef OCTAVE_INT_BIN_OP | |
5072 | 743 #undef OCTAVE_INT_DOUBLE_BIN_OP |
744 #undef OCTAVE_DOUBLE_INT_BIN_OP | |
745 #undef OCTAVE_INT_DOUBLE_CMP_OP | |
746 #undef OCTAVE_DOUBLE_INT_CMP_OP | |
747 #undef OCTAVE_INT_BITCMP_OP | |
4902 | 748 #undef OCTAVE_INT_CMP_OP |
5072 | 749 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
750 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS | |
751 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL | |
752 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS | |
753 #undef OCTAVE_TYPE1_CMP_OP_DECLS | |
754 #undef OCTAVE_US_TYPE2_CMP_OP_DECL | |
755 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS | |
756 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL | |
757 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS | |
5074 | 758 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902 | 759 |
760 #endif | |
761 | |
762 /* | |
763 ;;; Local Variables: *** | |
764 ;;; mode: C++ *** | |
765 ;;; End: *** | |
766 */ |