Mercurial > hg > octave-nkf
annotate liboctave/oct-inttypes.h @ 7904:1fddd9b8e862
Fix nil_rep reference counting in gl-render.cc::vertex_data internal class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 09 Jul 2008 10:59:29 -0400 |
parents | 82be108cc558 |
children | 935be827eaf8 |
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 | |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
131 template <typename T> |
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
132 inline T |
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
133 octave_int_fit_to_range (const double& x, const T& mn, const T& mx) |
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
134 { |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
135 return (__lo_ieee_isnan (x) ? 0 : (x > mx ? mx : (x < mn ? mn : static_cast<T> (x)))); |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
136 } |
7596
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
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 | |
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
|
209 // 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
|
210 // 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
|
211 // 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
|
212 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
213 // 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
|
214 // 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
|
215 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
216 template <class T> class octave_int; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
217 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
218 template <class T, bool is_signed> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
219 class octave_int_helper |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
220 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
221 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
222 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
|
223 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
224 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
|
225 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
226 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
|
227 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
228 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
229 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
230 class octave_int_helper<T, false> |
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 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
233 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
234 abs (const T& x) { return x; } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
235 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
236 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
237 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
|
238 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
239 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
240 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
241 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
|
242 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
243 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
244 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
245 class octave_int_helper<T, true> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
246 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
247 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
248 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
249 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
|
250 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
251 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
252 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
|
253 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
254 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
255 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
256 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
|
257 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
258 if (ival < 0) |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
259 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
|
260 else |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
261 ival = ival >> x; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
262 } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
263 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
264 |
4902 | 265 template <class T> |
266 class | |
267 octave_int | |
268 { | |
269 public: | |
270 | |
4943 | 271 typedef T val_type; |
272 | |
4902 | 273 octave_int (void) : ival () { } |
274 | |
275 template <class U> | |
276 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { } | |
277 | |
6402 | 278 octave_int (double d) : ival (OCTAVE_INT_FIT_TO_RANGE (xround (d), T)) { } |
279 | |
4902 | 280 octave_int (bool b) : ival (b) { } |
281 | |
282 template <class U> | |
283 octave_int (const octave_int<U>& i) | |
284 : ival (OCTAVE_INT_FIT_TO_RANGE (i.value (), T)) { } | |
285 | |
286 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
287 | |
288 octave_int& operator = (const octave_int<T>& i) | |
289 { | |
290 ival = i.ival; | |
291 return *this; | |
292 } | |
293 | |
294 ~octave_int (void) { } | |
295 | |
296 T value (void) const { return ival; } | |
297 | |
4949 | 298 const unsigned char * iptr (void) const |
299 { return reinterpret_cast<const unsigned char *> (& ival); } | |
300 | |
4902 | 301 bool operator ! (void) const { return ! ival; } |
302 | |
4906 | 303 octave_int<T> operator + (void) const { return *this; } |
4902 | 304 |
4906 | 305 octave_int<T> operator - (void) const |
4902 | 306 { |
4953 | 307 // Can't just return -ival because signed types are not |
308 // symmetric, which causes things like -intmin("int32") to be the | |
309 // same as intmin("int32") instead of intmax("int32") (which is | |
310 // what we should get with saturation semantics). | |
311 | |
312 return std::numeric_limits<T>::is_signed ? | |
313 OCTAVE_INT_FIT_TO_RANGE (- static_cast<double> (ival), T) : 0; | |
4902 | 314 } |
315 | |
7198 | 316 bool bool_value (void) const { return static_cast<bool> (value ()); } |
317 | |
318 char char_value (void) const { return static_cast<char> (value ()); } | |
319 | |
320 double double_value (void) const { return static_cast<double> (value ()); } | |
321 | |
322 float float_value (void) const { return static_cast<float> (value ()); } | |
323 | |
7177 | 324 operator T (void) const { return value (); } |
325 | |
7198 | 326 // char and bool operators intentionally omitted. |
5533 | 327 |
7198 | 328 operator double (void) const { return double_value (); } |
4902 | 329 |
7198 | 330 operator float (void) const { return float_value (); } |
5030 | 331 |
4902 | 332 octave_int<T>& operator += (const octave_int<T>& x) |
333 { | |
334 double t = static_cast<double> (value ()); | |
335 double tx = static_cast<double> (x.value ()); | |
336 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); | |
337 return *this; | |
338 } | |
339 | |
340 octave_int<T>& operator -= (const octave_int<T>& x) | |
341 { | |
342 double t = static_cast<double> (value ()); | |
343 double tx = static_cast<double> (x.value ()); | |
344 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); | |
345 return *this; | |
346 } | |
347 | |
4952 | 348 octave_int<T>& operator *= (const octave_int<T>& x) |
349 { | |
350 double t = static_cast<double> (value ()); | |
351 double tx = static_cast<double> (x.value ()); | |
352 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); | |
353 return *this; | |
354 } | |
355 | |
356 octave_int<T>& operator /= (const octave_int<T>& x) | |
357 { | |
358 double t = static_cast<double> (value ()); | |
359 double tx = static_cast<double> (x.value ()); | |
4968 | 360 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953 | 361 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952 | 362 return *this; |
363 } | |
364 | |
365 template <class T2> | |
366 octave_int<T>& operator <<= (const T2& x) | |
367 { | |
6764 | 368 ival = ival << x; |
4952 | 369 return *this; |
370 } | |
371 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
372 // 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
|
373 // 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
|
374 // 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
|
375 // for unsigned types. |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
376 |
4952 | 377 template <class T2> |
378 octave_int<T>& operator >>= (const T2& x) | |
379 { | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
380 octave_int_helper<T, std::numeric_limits<T>::is_signed>::rshift_eq (ival, x); |
4952 | 381 return *this; |
382 } | |
383 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
384 octave_int<T> abs (void) const |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
385 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
386 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
|
387 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
388 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
389 octave_int<T> signum (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
390 { |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
391 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
|
392 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
393 |
4906 | 394 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
395 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } | |
396 | |
4919 | 397 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909 | 398 |
4949 | 399 static int byte_size (void) { return sizeof(T); } |
400 | |
5900 | 401 // Unsafe. This function exists to support the MEX interface. |
402 // You should not use it anywhere else. | |
403 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
404 | |
4902 | 405 private: |
406 | |
407 T ival; | |
408 }; | |
409 | |
410 template <class T> | |
4953 | 411 octave_int<T> |
412 pow (const octave_int<T>& a, const octave_int<T>& b) | |
4952 | 413 { |
4953 | 414 octave_int<T> retval; |
4952 | 415 |
4953 | 416 octave_int<T> zero = octave_int<T> (0); |
417 octave_int<T> one = octave_int<T> (1); | |
4952 | 418 |
419 if (b == zero) | |
420 retval = one; | |
421 else if (b < zero) | |
422 retval = zero; | |
423 else | |
424 { | |
4953 | 425 octave_int<T> a_val = a; |
426 octave_int<T> b_val = b; | |
4952 | 427 |
428 retval = a; | |
429 | |
430 b_val -= 1; | |
431 | |
5030 | 432 while (b_val != zero) |
4952 | 433 { |
5030 | 434 if ((b_val & one) != zero) |
4952 | 435 retval = retval * a_val; |
436 | |
437 b_val = b_val >> 1; | |
438 | |
439 if (b_val > zero) | |
440 a_val = a_val * a_val; | |
441 } | |
442 } | |
443 | |
444 return retval; | |
445 } | |
446 | |
447 template <class T> | |
4953 | 448 octave_int<T> |
449 pow (double a, const octave_int<T>& b) | |
450 { | |
451 double tb = static_cast<double> (b.value ()); | |
452 double r = pow (a, tb); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
453 r = __lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 454 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
455 } | |
456 | |
457 template <class T> | |
458 octave_int<T> | |
459 pow (const octave_int<T>& a, double b) | |
460 { | |
461 double ta = static_cast<double> (a.value ()); | |
462 double r = pow (ta, b); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
463 r = __lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 464 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
465 } | |
466 | |
467 template <class T> | |
4902 | 468 std::ostream& |
469 operator << (std::ostream& os, const octave_int<T>& ival) | |
470 { | |
471 os << ival.value (); | |
472 return os; | |
473 } | |
474 | |
475 template <class T> | |
476 std::istream& | |
477 operator >> (std::istream& is, octave_int<T>& ival) | |
478 { | |
479 T tmp = 0; | |
480 is >> tmp; | |
481 ival = tmp; | |
482 return is; | |
483 } | |
484 | |
5828 | 485 typedef octave_int<int8_t> octave_int8; |
486 typedef octave_int<int16_t> octave_int16; | |
487 typedef octave_int<int32_t> octave_int32; | |
488 typedef octave_int<int64_t> octave_int64; | |
4902 | 489 |
5828 | 490 typedef octave_int<uint8_t> octave_uint8; |
491 typedef octave_int<uint16_t> octave_uint16; | |
492 typedef octave_int<uint32_t> octave_uint32; | |
493 typedef octave_int<uint64_t> octave_uint64; | |
4902 | 494 |
495 #define OCTAVE_INT_BIN_OP(OP) \ | |
496 template <class T1, class T2> \ | |
497 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ | |
498 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
499 { \ | |
500 double tx = static_cast<double> (x.value ()); \ | |
501 double ty = static_cast<double> (y.value ()); \ | |
502 double r = tx OP ty; \ | |
503 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ | |
4906 | 504 } |
4902 | 505 |
506 OCTAVE_INT_BIN_OP(+) | |
507 OCTAVE_INT_BIN_OP(-) | |
508 OCTAVE_INT_BIN_OP(*) | |
4953 | 509 |
510 template <class T1, class T2> | |
511 octave_int<typename octave_int_binop_traits<T1, T2>::TR> | |
512 operator / (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 ()); | |
6402 | 516 double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty); |
4953 | 517 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
518 } | |
519 | |
520 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ | |
521 template <class T> \ | |
522 octave_int<T> \ | |
523 operator OP (const octave_int<T>& x, double y) \ | |
524 { \ | |
525 double tx = static_cast<double> (x.value ()); \ | |
4968 | 526 double r = xround (tx OP y); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
527 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953 | 528 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
529 } | |
530 | |
531 OCTAVE_INT_DOUBLE_BIN_OP(+) | |
532 OCTAVE_INT_DOUBLE_BIN_OP(-) | |
533 OCTAVE_INT_DOUBLE_BIN_OP(*) | |
534 OCTAVE_INT_DOUBLE_BIN_OP(/) | |
535 | |
536 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ | |
537 template <class T> \ | |
538 octave_int<T> \ | |
539 operator OP (double x, const octave_int<T>& y) \ | |
540 { \ | |
541 double ty = static_cast<double> (y.value ()); \ | |
542 double r = x OP ty; \ | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
543 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953 | 544 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
545 } | |
546 | |
547 OCTAVE_DOUBLE_INT_BIN_OP(+) | |
548 OCTAVE_DOUBLE_INT_BIN_OP(-) | |
549 OCTAVE_DOUBLE_INT_BIN_OP(*) | |
550 OCTAVE_DOUBLE_INT_BIN_OP(/) | |
4902 | 551 |
5029 | 552 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
553 template <class T> \ | |
554 bool \ | |
555 operator OP (const octave_int<T>& x, const double& y) \ | |
556 { \ | |
557 double tx = static_cast<double> (x.value ()); \ | |
5030 | 558 return tx OP y; \ |
5029 | 559 } |
560 | |
561 OCTAVE_INT_DOUBLE_CMP_OP (<) | |
562 OCTAVE_INT_DOUBLE_CMP_OP (<=) | |
563 OCTAVE_INT_DOUBLE_CMP_OP (>=) | |
564 OCTAVE_INT_DOUBLE_CMP_OP (>) | |
565 OCTAVE_INT_DOUBLE_CMP_OP (==) | |
566 OCTAVE_INT_DOUBLE_CMP_OP (!=) | |
567 | |
568 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ | |
569 template <class T> \ | |
570 bool \ | |
571 operator OP (const double& x, const octave_int<T>& y) \ | |
572 { \ | |
573 double ty = static_cast<double> (y.value ()); \ | |
5030 | 574 return x OP ty; \ |
5029 | 575 } |
576 | |
577 OCTAVE_DOUBLE_INT_CMP_OP (<) | |
578 OCTAVE_DOUBLE_INT_CMP_OP (<=) | |
579 OCTAVE_DOUBLE_INT_CMP_OP (>=) | |
580 OCTAVE_DOUBLE_INT_CMP_OP (>) | |
581 OCTAVE_DOUBLE_INT_CMP_OP (==) | |
582 OCTAVE_DOUBLE_INT_CMP_OP (!=) | |
583 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
584 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
585 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
586 octave_int<T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
587 operator OP (const octave_int<T>& x, float y) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
588 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
589 double tx = static_cast<double> (x.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
590 double r = xround (tx OP y); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
591 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
592 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
593 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
594 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
595 OCTAVE_INT_FLOAT_BIN_OP(+) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
596 OCTAVE_INT_FLOAT_BIN_OP(-) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
597 OCTAVE_INT_FLOAT_BIN_OP(*) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
598 OCTAVE_INT_FLOAT_BIN_OP(/) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
599 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
600 #define OCTAVE_FLOAT_INT_BIN_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
601 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
602 octave_int<T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
603 operator OP (float x, const octave_int<T>& y) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
604 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
605 double ty = static_cast<double> (y.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
606 double r = x OP ty; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
607 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
608 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
609 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
610 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
611 OCTAVE_FLOAT_INT_BIN_OP(+) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
612 OCTAVE_FLOAT_INT_BIN_OP(-) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
613 OCTAVE_FLOAT_INT_BIN_OP(*) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
614 OCTAVE_FLOAT_INT_BIN_OP(/) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
615 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
616 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
617 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
618 bool \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
619 operator OP (const octave_int<T>& x, const float& y) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
620 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
621 double tx = static_cast<double> (x.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
622 return tx OP y; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
623 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
624 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
625 OCTAVE_INT_FLOAT_CMP_OP (<) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
626 OCTAVE_INT_FLOAT_CMP_OP (<=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
627 OCTAVE_INT_FLOAT_CMP_OP (>=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
628 OCTAVE_INT_FLOAT_CMP_OP (>) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
629 OCTAVE_INT_FLOAT_CMP_OP (==) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
630 OCTAVE_INT_FLOAT_CMP_OP (!=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
631 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
632 #define OCTAVE_FLOAT_INT_CMP_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
633 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
634 bool \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
635 operator OP (const float& x, const octave_int<T>& y) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
636 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
637 double ty = static_cast<double> (y.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
638 return x OP ty; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
639 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
640 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
641 OCTAVE_FLOAT_INT_CMP_OP (<) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
642 OCTAVE_FLOAT_INT_CMP_OP (<=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
643 OCTAVE_FLOAT_INT_CMP_OP (>=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
644 OCTAVE_FLOAT_INT_CMP_OP (>) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
645 OCTAVE_FLOAT_INT_CMP_OP (==) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
646 OCTAVE_FLOAT_INT_CMP_OP (!=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
647 |
4906 | 648 #define OCTAVE_INT_BITCMP_OP(OP) \ |
649 template <class T> \ | |
650 octave_int<T> \ | |
651 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ | |
652 { \ | |
653 return x.value () OP y.value (); \ | |
654 } | |
655 | |
656 OCTAVE_INT_BITCMP_OP (&) | |
657 OCTAVE_INT_BITCMP_OP (|) | |
658 OCTAVE_INT_BITCMP_OP (^) | |
659 | |
4952 | 660 template <class T1, class T2> |
661 octave_int<T1> | |
662 operator << (const octave_int<T1>& x, const T2& y) | |
663 { | |
5030 | 664 octave_int<T1> retval = x; |
4952 | 665 return retval <<= y; |
666 } | |
4906 | 667 |
4952 | 668 template <class T1, class T2> |
669 octave_int<T1> | |
670 operator >> (const octave_int<T1>& x, const T2& y) | |
671 { | |
5030 | 672 octave_int<T1> retval = x; |
4952 | 673 return retval >>= y; |
674 } | |
4906 | 675 |
4909 | 676 template <class T> |
677 octave_int<T> | |
4920 | 678 bitshift (const octave_int<T>& a, int n, |
679 const octave_int<T>& mask = std::numeric_limits<T>::max ()) | |
4909 | 680 { |
681 if (n > 0) | |
4952 | 682 return (a << n) & mask; |
4909 | 683 else if (n < 0) |
4952 | 684 return (a >> -n) & mask; |
4909 | 685 else |
686 return a; | |
687 } | |
688 | |
4902 | 689 #define OCTAVE_INT_CMP_OP(OP) \ |
690 template <class T1, class T2> \ | |
691 bool \ | |
692 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
693 { \ | |
5072 | 694 return x.value () OP y.value (); \ |
4906 | 695 } |
4902 | 696 |
697 OCTAVE_INT_CMP_OP (<) | |
698 OCTAVE_INT_CMP_OP (<=) | |
699 OCTAVE_INT_CMP_OP (>=) | |
700 OCTAVE_INT_CMP_OP (>) | |
701 OCTAVE_INT_CMP_OP (==) | |
702 OCTAVE_INT_CMP_OP (!=) | |
703 | |
5072 | 704 // The following apply if the unsigned type is at least as wide as the |
705 // signed type (then we can cast postive signed values to the unsigned | |
706 // type and compare). | |
707 | |
708 #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
|
709 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
710 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
711 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 712 |
713 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
714 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ | |
715 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ | |
716 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ | |
717 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ | |
718 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ | |
719 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) | |
720 | |
721 #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
|
722 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
723 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
724 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 725 |
726 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ | |
727 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ | |
728 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ | |
729 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ | |
730 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ | |
731 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ | |
732 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) | |
733 | |
734 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
735 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ | |
736 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) | |
737 | |
5828 | 738 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
739 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) | |
740 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) | |
5072 | 741 |
5828 | 742 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
743 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) | |
744 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) | |
745 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) | |
5072 | 746 |
747 // The following apply if the signed type is wider than the unsigned | |
748 // type (then we can cast unsigned values to the signed type and | |
749 // compare if the signed value is positive). | |
750 | |
751 #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
|
752 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
753 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
754 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 755 |
756 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
757 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ | |
758 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ | |
759 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ | |
760 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ | |
761 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
762 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
763 | |
764 #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
|
765 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
766 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
767 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 768 |
769 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
770 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ | |
771 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ | |
772 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ | |
773 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ | |
774 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
775 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
776 | |
777 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ | |
778 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ | |
779 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) | |
780 | |
5828 | 781 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072 | 782 |
4902 | 783 #undef OCTAVE_INT_BINOP_TRAIT |
5072 | 784 #undef OCTAVE_US_S_FTR |
785 #undef OCTAVE_US_S_FTR_FCNS | |
786 #undef OCTAVE_S_US_FTR | |
787 #undef OCTAVE_S_US_FTR_FCNS | |
4902 | 788 #undef OCTAVE_INT_FIT_TO_RANGE |
789 #undef OCTAVE_INT_MIN_VAL2 | |
790 #undef OCTAVE_INT_MAX_VAL2 | |
791 #undef OCTAVE_INT_FIT_TO_RANGE2 | |
792 #undef OCTAVE_INT_BIN_OP | |
5072 | 793 #undef OCTAVE_INT_DOUBLE_BIN_OP |
794 #undef OCTAVE_DOUBLE_INT_BIN_OP | |
795 #undef OCTAVE_INT_DOUBLE_CMP_OP | |
796 #undef OCTAVE_DOUBLE_INT_CMP_OP | |
797 #undef OCTAVE_INT_BITCMP_OP | |
4902 | 798 #undef OCTAVE_INT_CMP_OP |
5072 | 799 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
800 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS | |
801 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL | |
802 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS | |
803 #undef OCTAVE_TYPE1_CMP_OP_DECLS | |
804 #undef OCTAVE_US_TYPE2_CMP_OP_DECL | |
805 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS | |
806 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL | |
807 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS | |
5074 | 808 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902 | 809 |
810 #endif | |
811 | |
812 /* | |
813 ;;; Local Variables: *** | |
814 ;;; mode: C++ *** | |
815 ;;; End: *** | |
816 */ |