Mercurial > hg > octave-nkf
annotate liboctave/oct-inttypes.h @ 8104:fc45357bf50c
fix integer exponentiation with negative exponent
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 12 Sep 2008 15:34:13 -0400 |
parents | cd90e2842080 |
children | 66bc6f9b4f72 |
rev | line source |
---|---|
4902 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 John W. Eaton |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
4 Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com> |
4902 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4902 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4902 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_inttypes_h) | |
25 #define octave_inttypes_h 1 | |
26 | |
6482 | 27 #include <climits> |
28 | |
4902 | 29 #include <limits> |
30 #include <iostream> | |
31 | |
5828 | 32 #include "oct-types.h" |
4953 | 33 #include "lo-ieee.h" |
4969 | 34 #include "lo-mappers.h" |
4902 | 35 |
36 template <class T1, class T2> | |
37 class | |
38 octave_int_binop_traits | |
39 { | |
40 public: | |
41 // The return type for a T1 by T2 binary operation. | |
42 typedef T1 TR; | |
43 }; | |
44 | |
45 #define OCTAVE_INT_BINOP_TRAIT(T1, T2, T3) \ | |
7999 | 46 template <> \ |
4902 | 47 class octave_int_binop_traits <T1, T2> \ |
48 { \ | |
49 public: \ | |
50 typedef T3 TR; \ | |
51 } | |
52 | |
5828 | 53 OCTAVE_INT_BINOP_TRAIT (int8_t, int8_t, int8_t); |
54 OCTAVE_INT_BINOP_TRAIT (int8_t, int16_t, int8_t); | |
55 OCTAVE_INT_BINOP_TRAIT (int8_t, int32_t, int8_t); | |
56 OCTAVE_INT_BINOP_TRAIT (int8_t, int64_t, int8_t); | |
57 OCTAVE_INT_BINOP_TRAIT (int8_t, uint8_t, int8_t); | |
58 OCTAVE_INT_BINOP_TRAIT (int8_t, uint16_t, int8_t); | |
59 OCTAVE_INT_BINOP_TRAIT (int8_t, uint32_t, int8_t); | |
60 OCTAVE_INT_BINOP_TRAIT (int8_t, uint64_t, int8_t); | |
4902 | 61 |
5828 | 62 OCTAVE_INT_BINOP_TRAIT (int16_t, int8_t, int16_t); |
63 OCTAVE_INT_BINOP_TRAIT (int16_t, int16_t, int16_t); | |
64 OCTAVE_INT_BINOP_TRAIT (int16_t, int32_t, int16_t); | |
65 OCTAVE_INT_BINOP_TRAIT (int16_t, int64_t, int16_t); | |
66 OCTAVE_INT_BINOP_TRAIT (int16_t, uint8_t, int16_t); | |
67 OCTAVE_INT_BINOP_TRAIT (int16_t, uint16_t, int16_t); | |
68 OCTAVE_INT_BINOP_TRAIT (int16_t, uint32_t, int16_t); | |
69 OCTAVE_INT_BINOP_TRAIT (int16_t, uint64_t, int16_t); | |
4902 | 70 |
5828 | 71 OCTAVE_INT_BINOP_TRAIT (int32_t, int8_t, int32_t); |
72 OCTAVE_INT_BINOP_TRAIT (int32_t, int16_t, int32_t); | |
73 OCTAVE_INT_BINOP_TRAIT (int32_t, int32_t, int32_t); | |
74 OCTAVE_INT_BINOP_TRAIT (int32_t, int64_t, int32_t); | |
75 OCTAVE_INT_BINOP_TRAIT (int32_t, uint8_t, int32_t); | |
76 OCTAVE_INT_BINOP_TRAIT (int32_t, uint16_t, int32_t); | |
77 OCTAVE_INT_BINOP_TRAIT (int32_t, uint32_t, int32_t); | |
78 OCTAVE_INT_BINOP_TRAIT (int32_t, uint64_t, int32_t); | |
4902 | 79 |
5828 | 80 OCTAVE_INT_BINOP_TRAIT (int64_t, int8_t, int64_t); |
81 OCTAVE_INT_BINOP_TRAIT (int64_t, int16_t, int64_t); | |
82 OCTAVE_INT_BINOP_TRAIT (int64_t, int32_t, int64_t); | |
83 OCTAVE_INT_BINOP_TRAIT (int64_t, int64_t, int64_t); | |
84 OCTAVE_INT_BINOP_TRAIT (int64_t, uint8_t, int64_t); | |
85 OCTAVE_INT_BINOP_TRAIT (int64_t, uint16_t, int64_t); | |
86 OCTAVE_INT_BINOP_TRAIT (int64_t, uint32_t, int64_t); | |
87 OCTAVE_INT_BINOP_TRAIT (int64_t, uint64_t, int64_t); | |
4902 | 88 |
5828 | 89 OCTAVE_INT_BINOP_TRAIT (uint8_t, int8_t, int8_t); |
90 OCTAVE_INT_BINOP_TRAIT (uint8_t, int16_t, int8_t); | |
91 OCTAVE_INT_BINOP_TRAIT (uint8_t, int32_t, int8_t); | |
92 OCTAVE_INT_BINOP_TRAIT (uint8_t, int64_t, int8_t); | |
93 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint8_t, uint8_t); | |
94 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint16_t, uint8_t); | |
95 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint32_t, uint8_t); | |
96 OCTAVE_INT_BINOP_TRAIT (uint8_t, uint64_t, uint8_t); | |
4902 | 97 |
5828 | 98 OCTAVE_INT_BINOP_TRAIT (uint16_t, int8_t, int16_t); |
99 OCTAVE_INT_BINOP_TRAIT (uint16_t, int16_t, int16_t); | |
100 OCTAVE_INT_BINOP_TRAIT (uint16_t, int32_t, int16_t); | |
101 OCTAVE_INT_BINOP_TRAIT (uint16_t, int64_t, int16_t); | |
102 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint8_t, uint16_t); | |
103 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint16_t, uint16_t); | |
104 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint32_t, uint16_t); | |
105 OCTAVE_INT_BINOP_TRAIT (uint16_t, uint64_t, uint16_t); | |
4902 | 106 |
5828 | 107 OCTAVE_INT_BINOP_TRAIT (uint32_t, int8_t, int32_t); |
108 OCTAVE_INT_BINOP_TRAIT (uint32_t, int16_t, int32_t); | |
109 OCTAVE_INT_BINOP_TRAIT (uint32_t, int32_t, int32_t); | |
110 OCTAVE_INT_BINOP_TRAIT (uint32_t, int64_t, int32_t); | |
111 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint8_t, uint32_t); | |
112 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint16_t, uint32_t); | |
113 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint32_t, uint32_t); | |
114 OCTAVE_INT_BINOP_TRAIT (uint32_t, uint64_t, uint32_t); | |
4902 | 115 |
5828 | 116 OCTAVE_INT_BINOP_TRAIT (uint64_t, int8_t, int64_t); |
117 OCTAVE_INT_BINOP_TRAIT (uint64_t, int16_t, int64_t); | |
118 OCTAVE_INT_BINOP_TRAIT (uint64_t, int32_t, int64_t); | |
119 OCTAVE_INT_BINOP_TRAIT (uint64_t, int64_t, int64_t); | |
120 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint8_t, uint64_t); | |
121 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint16_t, uint64_t); | |
122 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint32_t, uint64_t); | |
123 OCTAVE_INT_BINOP_TRAIT (uint64_t, uint64_t, uint64_t); | |
4902 | 124 |
125 template <class T1, class T2> | |
126 inline T2 | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
127 octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx, |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
128 int& conv_flag, int math_truncate) |
4902 | 129 { |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
130 bool out_of_range_max = x > mx; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
131 bool out_of_range_min = x < mn; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
132 conv_flag |= (out_of_range_max || out_of_range_min ? math_truncate : 0); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
133 return (out_of_range_max ? mx : (out_of_range_min ? mn : T2 (x))); |
4902 | 134 } |
135 | |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
136 template <typename T> |
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
137 inline T |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
138 octave_int_fit_to_range (const double& x, const T& mn, const T& mx, |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
139 int& conv_flag, int math_truncate) |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
140 { |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
141 bool out_of_range_max = x > mx; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
142 bool out_of_range_min = x < mn; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
143 conv_flag |= (out_of_range_max || out_of_range_min ? math_truncate : 0); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
144 return (__lo_ieee_isnan (x) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
145 ? 0 : (out_of_range_max |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
146 ? mx : (out_of_range_min ? 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
|
147 } |
7596
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
148 |
4943 | 149 // If X is unsigned and the new type is signed, then we only have to |
150 // check the upper limit, but we should cast the maximum value of the | |
151 // new type to an unsigned type before performing the comparison. | |
152 // This should always be OK because the maximum value should always be | |
153 // positive. | |
154 | |
5072 | 155 #define OCTAVE_US_S_FTR(T1, T2, TC) \ |
4943 | 156 template <> \ |
157 inline T2 \ | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
158 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2& mx, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
159 int& conv_flag, int math_truncate) \ |
4943 | 160 { \ |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
161 bool out_of_range = x > static_cast<TC> (mx); \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
162 conv_flag |= (out_of_range ? math_truncate : 0); \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
163 return out_of_range ? mx : x; \ |
4943 | 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 \ | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
188 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2&, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
189 int& conv_flag, int math_truncate) \ |
4943 | 190 { \ |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
191 bool out_of_range = x < 0; \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
192 conv_flag |= (out_of_range ? math_truncate : 0); \ |
5420 | 193 return x <= 0 ? 0 : x; \ |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
194 } |
4943 | 195 |
5072 | 196 #define OCTAVE_S_US_FTR_FCNS(T) \ |
197 OCTAVE_S_US_FTR (T, unsigned char) \ | |
198 OCTAVE_S_US_FTR (T, unsigned short) \ | |
199 OCTAVE_S_US_FTR (T, unsigned int) \ | |
200 OCTAVE_S_US_FTR (T, unsigned long) \ | |
201 OCTAVE_S_US_FTR (T, unsigned long long) | |
4943 | 202 |
5072 | 203 OCTAVE_S_US_FTR_FCNS (char) |
204 OCTAVE_S_US_FTR_FCNS (signed char) | |
205 OCTAVE_S_US_FTR_FCNS (short) | |
206 OCTAVE_S_US_FTR_FCNS (int) | |
207 OCTAVE_S_US_FTR_FCNS (long) | |
208 OCTAVE_S_US_FTR_FCNS (long long) | |
4943 | 209 |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
210 #define OCTAVE_INT_CONV_FIT_TO_RANGE(r, T) \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
211 octave_int_fit_to_range (r, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
212 std::numeric_limits<T>::min (), \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
213 std::numeric_limits<T>::max (), \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
214 octave_int<T>::conv_flag, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
215 octave_int<T>::int_truncate) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
216 |
4902 | 217 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \ |
4943 | 218 octave_int_fit_to_range (r, \ |
219 std::numeric_limits<T>::min (), \ | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
220 std::numeric_limits<T>::max (), \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
221 octave_int<T>::conv_flag, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
222 octave_int<T>::math_truncate) |
4902 | 223 |
224 #define OCTAVE_INT_MIN_VAL2(T1, T2) \ | |
225 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min () | |
226 | |
227 #define OCTAVE_INT_MAX_VAL2(T1, T2) \ | |
228 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::max () | |
229 | |
230 #define OCTAVE_INT_FIT_TO_RANGE2(r, T1, T2) \ | |
231 octave_int_fit_to_range (r, \ | |
232 OCTAVE_INT_MIN_VAL2 (T1, T2), \ | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
233 OCTAVE_INT_MAX_VAL2 (T1, T2), \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
234 octave_int<typename octave_int_binop_traits<T1, T2>::TR>::conv_flag, \ |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
235 octave_int<typename octave_int_binop_traits<T1, T2>::TR>::math_truncate) |
4902 | 236 |
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
|
237 // 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
|
238 // 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
|
239 // 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
|
240 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
241 // 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
|
242 // 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
|
243 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
244 template <class T> class octave_int; |
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 template <class T, bool is_signed> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
247 class octave_int_helper |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
248 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
249 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
250 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
|
251 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
252 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
|
253 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
254 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
|
255 }; |
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 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
258 class octave_int_helper<T, false> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
259 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
260 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
261 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
262 abs (const T& x) { return x; } |
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 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
265 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
|
266 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
267 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
268 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
269 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
|
270 }; |
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 template <class T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
273 class octave_int_helper<T, true> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
274 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
275 public: |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
276 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
277 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
|
278 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
279 static octave_int<T> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
280 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
|
281 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
282 template <class T2> |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
283 static void |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
284 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
|
285 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
286 if (ival < 0) |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
287 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
|
288 else |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
289 ival = ival >> x; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
290 } |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
291 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
292 |
4902 | 293 template <class T> |
294 class | |
295 octave_int | |
296 { | |
297 public: | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
298 enum conv_error_type |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
299 { |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
300 int_truncate = 1, |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
301 conv_nan = 2, |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
302 conv_non_int = 4, |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
303 math_truncate = 8 |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
304 }; |
4902 | 305 |
4943 | 306 typedef T val_type; |
307 | |
4902 | 308 octave_int (void) : ival () { } |
309 | |
310 template <class U> | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
311 octave_int (U i) : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (i, T)) { } |
4902 | 312 |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
313 octave_int (double d) : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (xround (d), T)) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
314 { |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
315 if (xisnan (d)) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
316 conv_flag |= conv_nan; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
317 else |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
318 conv_flag |= (d != xround (d) ? conv_non_int : 0); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
319 } |
6402 | 320 |
4902 | 321 octave_int (bool b) : ival (b) { } |
322 | |
323 template <class U> | |
324 octave_int (const octave_int<U>& i) | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
325 : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (i.value (), T)) { } |
4902 | 326 |
327 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
328 | |
329 octave_int& operator = (const octave_int<T>& i) | |
330 { | |
331 ival = i.ival; | |
332 return *this; | |
333 } | |
334 | |
335 ~octave_int (void) { } | |
336 | |
337 T value (void) const { return ival; } | |
338 | |
4949 | 339 const unsigned char * iptr (void) const |
340 { return reinterpret_cast<const unsigned char *> (& ival); } | |
341 | |
4902 | 342 bool operator ! (void) const { return ! ival; } |
343 | |
4906 | 344 octave_int<T> operator + (void) const { return *this; } |
4902 | 345 |
4906 | 346 octave_int<T> operator - (void) const |
4902 | 347 { |
4953 | 348 // Can't just return -ival because signed types are not |
349 // symmetric, which causes things like -intmin("int32") to be the | |
350 // same as intmin("int32") instead of intmax("int32") (which is | |
351 // what we should get with saturation semantics). | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
352 if (std::numeric_limits<T>::is_signed) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
353 return OCTAVE_INT_FIT_TO_RANGE (- static_cast<double> (ival), T); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
354 else |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
355 { |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
356 conv_flag |= math_truncate; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
357 return 0; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
358 } |
4902 | 359 } |
360 | |
7198 | 361 bool bool_value (void) const { return static_cast<bool> (value ()); } |
362 | |
363 char char_value (void) const { return static_cast<char> (value ()); } | |
364 | |
365 double double_value (void) const { return static_cast<double> (value ()); } | |
366 | |
367 float float_value (void) const { return static_cast<float> (value ()); } | |
368 | |
7177 | 369 operator T (void) const { return value (); } |
370 | |
7198 | 371 // char and bool operators intentionally omitted. |
5533 | 372 |
7198 | 373 operator double (void) const { return double_value (); } |
4902 | 374 |
7198 | 375 operator float (void) const { return float_value (); } |
5030 | 376 |
4902 | 377 octave_int<T>& operator += (const octave_int<T>& x) |
378 { | |
379 double t = static_cast<double> (value ()); | |
380 double tx = static_cast<double> (x.value ()); | |
381 ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); | |
382 return *this; | |
383 } | |
384 | |
385 octave_int<T>& operator -= (const octave_int<T>& x) | |
386 { | |
387 double t = static_cast<double> (value ()); | |
388 double tx = static_cast<double> (x.value ()); | |
389 ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); | |
390 return *this; | |
391 } | |
392 | |
4952 | 393 octave_int<T>& operator *= (const octave_int<T>& x) |
394 { | |
395 double t = static_cast<double> (value ()); | |
396 double tx = static_cast<double> (x.value ()); | |
397 ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); | |
398 return *this; | |
399 } | |
400 | |
401 octave_int<T>& operator /= (const octave_int<T>& x) | |
402 { | |
403 double t = static_cast<double> (value ()); | |
404 double tx = static_cast<double> (x.value ()); | |
4968 | 405 double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); |
4953 | 406 ival = OCTAVE_INT_FIT_TO_RANGE (r, T); |
4952 | 407 return *this; |
408 } | |
409 | |
410 template <class T2> | |
411 octave_int<T>& operator <<= (const T2& x) | |
412 { | |
6764 | 413 ival = ival << x; |
4952 | 414 return *this; |
415 } | |
416 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
417 // 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
|
418 // 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
|
419 // 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
|
420 // for unsigned types. |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
421 |
4952 | 422 template <class T2> |
423 octave_int<T>& operator >>= (const T2& x) | |
424 { | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
425 octave_int_helper<T, std::numeric_limits<T>::is_signed>::rshift_eq (ival, x); |
4952 | 426 return *this; |
427 } | |
428 | |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
429 octave_int<T> abs (void) const |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
430 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
431 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
|
432 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
433 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
434 octave_int<T> signum (void) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
435 { |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
436 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
|
437 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
438 |
4906 | 439 octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } |
440 octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } | |
441 | |
4919 | 442 static int nbits (void) { return sizeof (T) * CHAR_BIT; } |
4909 | 443 |
4949 | 444 static int byte_size (void) { return sizeof(T); } |
445 | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
446 static int get_conv_flag () { return conv_flag; } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
447 static bool get_trunc_flag () { return (conv_flag & int_truncate); } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
448 static bool get_nan_flag () { return (conv_flag & conv_nan); } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
449 static bool get_non_int_flag () { return (conv_flag & conv_non_int); } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
450 static bool get_math_trunc_flag () { return (conv_flag & math_truncate); } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
451 static void clear_conv_flag () { conv_flag = 0; } |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
452 |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
453 static const char *type_name () { return "unknown type"; } |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
454 |
5900 | 455 // Unsafe. This function exists to support the MEX interface. |
456 // You should not use it anywhere else. | |
457 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
458 | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
459 static int conv_flag; |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
460 |
4902 | 461 private: |
462 | |
463 T ival; | |
464 }; | |
465 | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
466 template<class T> int octave_int<T>::conv_flag = 0; |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
467 |
4902 | 468 template <class T> |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
469 bool |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
470 xisnan (const octave_int<T>&) |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
471 { |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
472 return false; |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
473 } |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
474 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
475 template <class T> |
4953 | 476 octave_int<T> |
477 pow (const octave_int<T>& a, const octave_int<T>& b) | |
4952 | 478 { |
4953 | 479 octave_int<T> retval; |
4952 | 480 |
4953 | 481 octave_int<T> zero = octave_int<T> (0); |
482 octave_int<T> one = octave_int<T> (1); | |
4952 | 483 |
8104
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
484 if (b == zero || a == one) |
4952 | 485 retval = one; |
486 else if (b < zero) | |
8104
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
487 { |
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
488 if (std::numeric_limits<T>::is_signed && a.value () == -1) |
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
489 retval = (b.value () % 2) ? a : one; |
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
490 else |
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
491 retval = zero; |
fc45357bf50c
fix integer exponentiation with negative exponent
Jaroslav Hajek <highegg@gmail.com>
parents:
8039
diff
changeset
|
492 } |
4952 | 493 else |
494 { | |
4953 | 495 octave_int<T> a_val = a; |
496 octave_int<T> b_val = b; | |
4952 | 497 |
498 retval = a; | |
499 | |
500 b_val -= 1; | |
501 | |
5030 | 502 while (b_val != zero) |
4952 | 503 { |
5030 | 504 if ((b_val & one) != zero) |
4952 | 505 retval = retval * a_val; |
506 | |
507 b_val = b_val >> 1; | |
508 | |
509 if (b_val > zero) | |
510 a_val = a_val * a_val; | |
511 } | |
512 } | |
513 | |
514 return retval; | |
515 } | |
516 | |
517 template <class T> | |
4953 | 518 octave_int<T> |
519 pow (double a, const octave_int<T>& b) | |
520 { | |
521 double tb = static_cast<double> (b.value ()); | |
522 double r = pow (a, tb); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
523 r = __lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 524 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
525 } | |
526 | |
527 template <class T> | |
528 octave_int<T> | |
529 pow (const octave_int<T>& a, double b) | |
530 { | |
531 double ta = static_cast<double> (a.value ()); | |
532 double r = pow (ta, b); | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
533 r = __lo_ieee_isnan (r) ? 0 : xround (r); |
4953 | 534 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
535 } | |
536 | |
537 template <class T> | |
8039
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
538 octave_int<T> |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
539 powf (float a, const octave_int<T>& b) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
540 { |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
541 float tb = static_cast<float> (b.value ()); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
542 float r = powf (a, tb); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
543 r = __lo_ieee_float_isnan (r) ? 0 : xround (r); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
544 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
545 } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
546 |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
547 template <class T> |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
548 octave_int<T> |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
549 powf (const octave_int<T>& a, float b) |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
550 { |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
551 float ta = static_cast<float> (a.value ()); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
552 float r = pow (ta, b); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
553 r = __lo_ieee_float_isnan (r) ? 0 : xround (r); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
554 return OCTAVE_INT_FIT_TO_RANGE (r, T); |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
555 } |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
556 |
cd90e2842080
Add additional integer math and conversion warnings, set their default state to be off and add the intwarning function
David Bateman <dbateman@free.fr>
parents:
7999
diff
changeset
|
557 template <class T> |
4902 | 558 std::ostream& |
559 operator << (std::ostream& os, const octave_int<T>& ival) | |
560 { | |
561 os << ival.value (); | |
562 return os; | |
563 } | |
564 | |
565 template <class T> | |
566 std::istream& | |
567 operator >> (std::istream& is, octave_int<T>& ival) | |
568 { | |
569 T tmp = 0; | |
570 is >> tmp; | |
571 ival = tmp; | |
572 return is; | |
573 } | |
574 | |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
575 // specialize the widening conversions to make them faster |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
576 // gosh. the syntax is tricky! |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
577 |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
578 #define SPECIALIZE_WIDENING_CONVERSION(T1, T2) \ |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
579 template <> template <> \ |
7999 | 580 inline octave_int<T2>::octave_int (T1 i) \ |
581 : ival (i) { } \ | |
582 \ | |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
583 template <> template <> \ |
7999 | 584 inline octave_int<T2>::octave_int (const octave_int<T1>& i) \ |
585 : ival (i.value ()) { } | |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
586 |
7999 | 587 SPECIALIZE_WIDENING_CONVERSION (int8_t, int16_t) |
588 SPECIALIZE_WIDENING_CONVERSION (int8_t, int32_t) | |
589 SPECIALIZE_WIDENING_CONVERSION (int8_t, int64_t) | |
590 SPECIALIZE_WIDENING_CONVERSION (int16_t, int32_t) | |
591 SPECIALIZE_WIDENING_CONVERSION (int16_t, int64_t) | |
592 SPECIALIZE_WIDENING_CONVERSION (int32_t, int64_t) | |
593 SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint16_t) | |
594 SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint32_t) | |
595 SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint64_t) | |
596 SPECIALIZE_WIDENING_CONVERSION (uint16_t, uint32_t) | |
597 SPECIALIZE_WIDENING_CONVERSION (uint16_t, uint64_t) | |
598 SPECIALIZE_WIDENING_CONVERSION (uint32_t, uint64_t) | |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
599 |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
600 // declare type names |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
601 #define DECLARE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ |
7999 | 602 template <> \ |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
603 inline const char * \ |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
604 octave_int<TYPE>::type_name () { return TYPENAME; } |
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
605 |
7999 | 606 DECLARE_OCTAVE_INT_TYPENAME (int8_t, "int8") |
607 DECLARE_OCTAVE_INT_TYPENAME (int16_t, "int16") | |
608 DECLARE_OCTAVE_INT_TYPENAME (int32_t, "int32") | |
609 DECLARE_OCTAVE_INT_TYPENAME (int64_t, "int64") | |
610 DECLARE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") | |
611 DECLARE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") | |
612 DECLARE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") | |
613 DECLARE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") | |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
614 |
5828 | 615 typedef octave_int<int8_t> octave_int8; |
616 typedef octave_int<int16_t> octave_int16; | |
617 typedef octave_int<int32_t> octave_int32; | |
618 typedef octave_int<int64_t> octave_int64; | |
4902 | 619 |
5828 | 620 typedef octave_int<uint8_t> octave_uint8; |
621 typedef octave_int<uint16_t> octave_uint16; | |
622 typedef octave_int<uint32_t> octave_uint32; | |
623 typedef octave_int<uint64_t> octave_uint64; | |
4902 | 624 |
625 #define OCTAVE_INT_BIN_OP(OP) \ | |
626 template <class T1, class T2> \ | |
627 octave_int<typename octave_int_binop_traits<T1, T2>::TR> \ | |
628 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
629 { \ | |
630 double tx = static_cast<double> (x.value ()); \ | |
631 double ty = static_cast<double> (y.value ()); \ | |
632 double r = tx OP ty; \ | |
633 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ | |
4906 | 634 } |
4902 | 635 |
7999 | 636 OCTAVE_INT_BIN_OP (+) |
637 OCTAVE_INT_BIN_OP (-) | |
638 OCTAVE_INT_BIN_OP (*) | |
4953 | 639 |
640 template <class T1, class T2> | |
641 octave_int<typename octave_int_binop_traits<T1, T2>::TR> | |
642 operator / (const octave_int<T1>& x, const octave_int<T2>& y) | |
643 { | |
644 double tx = static_cast<double> (x.value ()); | |
645 double ty = static_cast<double> (y.value ()); | |
6402 | 646 double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty); |
4953 | 647 return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); |
648 } | |
649 | |
650 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ | |
651 template <class T> \ | |
652 octave_int<T> \ | |
653 operator OP (const octave_int<T>& x, double y) \ | |
654 { \ | |
655 double tx = static_cast<double> (x.value ()); \ | |
4968 | 656 double r = xround (tx OP y); \ |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
657 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953 | 658 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
659 } | |
660 | |
7999 | 661 OCTAVE_INT_DOUBLE_BIN_OP (+) |
662 OCTAVE_INT_DOUBLE_BIN_OP (-) | |
663 OCTAVE_INT_DOUBLE_BIN_OP (*) | |
664 OCTAVE_INT_DOUBLE_BIN_OP (/) | |
4953 | 665 |
666 #define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ | |
667 template <class T> \ | |
668 octave_int<T> \ | |
669 operator OP (double x, const octave_int<T>& y) \ | |
670 { \ | |
671 double ty = static_cast<double> (y.value ()); \ | |
672 double r = x OP ty; \ | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
673 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
4953 | 674 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
675 } | |
676 | |
7999 | 677 OCTAVE_DOUBLE_INT_BIN_OP (+) |
678 OCTAVE_DOUBLE_INT_BIN_OP (-) | |
679 OCTAVE_DOUBLE_INT_BIN_OP (*) | |
680 OCTAVE_DOUBLE_INT_BIN_OP (/) | |
4902 | 681 |
5029 | 682 #define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ |
683 template <class T> \ | |
684 bool \ | |
685 operator OP (const octave_int<T>& x, const double& y) \ | |
686 { \ | |
687 double tx = static_cast<double> (x.value ()); \ | |
5030 | 688 return tx OP y; \ |
5029 | 689 } |
690 | |
691 OCTAVE_INT_DOUBLE_CMP_OP (<) | |
692 OCTAVE_INT_DOUBLE_CMP_OP (<=) | |
693 OCTAVE_INT_DOUBLE_CMP_OP (>=) | |
694 OCTAVE_INT_DOUBLE_CMP_OP (>) | |
695 OCTAVE_INT_DOUBLE_CMP_OP (==) | |
696 OCTAVE_INT_DOUBLE_CMP_OP (!=) | |
697 | |
698 #define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ | |
699 template <class T> \ | |
700 bool \ | |
701 operator OP (const double& x, const octave_int<T>& y) \ | |
702 { \ | |
703 double ty = static_cast<double> (y.value ()); \ | |
5030 | 704 return x OP ty; \ |
5029 | 705 } |
706 | |
707 OCTAVE_DOUBLE_INT_CMP_OP (<) | |
708 OCTAVE_DOUBLE_INT_CMP_OP (<=) | |
709 OCTAVE_DOUBLE_INT_CMP_OP (>=) | |
710 OCTAVE_DOUBLE_INT_CMP_OP (>) | |
711 OCTAVE_DOUBLE_INT_CMP_OP (==) | |
712 OCTAVE_DOUBLE_INT_CMP_OP (!=) | |
713 | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
714 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
715 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
716 octave_int<T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
717 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
|
718 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
719 double tx = static_cast<double> (x.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
720 double r = xround (tx OP y); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
721 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
722 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
723 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
724 |
7999 | 725 OCTAVE_INT_FLOAT_BIN_OP (+) |
726 OCTAVE_INT_FLOAT_BIN_OP (-) | |
727 OCTAVE_INT_FLOAT_BIN_OP (*) | |
728 OCTAVE_INT_FLOAT_BIN_OP (/) | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
729 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
730 #define OCTAVE_FLOAT_INT_BIN_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
731 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
732 octave_int<T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
733 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
|
734 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
735 double ty = static_cast<double> (y.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
736 double r = x OP ty; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
737 r = __lo_ieee_isnan (r) ? 0 : xround (r); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
738 return OCTAVE_INT_FIT_TO_RANGE (r, T); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
739 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
740 |
7999 | 741 OCTAVE_FLOAT_INT_BIN_OP (+) |
742 OCTAVE_FLOAT_INT_BIN_OP (-) | |
743 OCTAVE_FLOAT_INT_BIN_OP (*) | |
744 OCTAVE_FLOAT_INT_BIN_OP (/) | |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
745 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
746 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
747 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
748 bool \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
749 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
|
750 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
751 double tx = static_cast<double> (x.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
752 return tx OP y; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
753 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
754 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
755 OCTAVE_INT_FLOAT_CMP_OP (<) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
756 OCTAVE_INT_FLOAT_CMP_OP (<=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
757 OCTAVE_INT_FLOAT_CMP_OP (>=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
758 OCTAVE_INT_FLOAT_CMP_OP (>) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
759 OCTAVE_INT_FLOAT_CMP_OP (==) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
760 OCTAVE_INT_FLOAT_CMP_OP (!=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
761 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
762 #define OCTAVE_FLOAT_INT_CMP_OP(OP) \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
763 template <class T> \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
764 bool \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
765 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
|
766 { \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
767 double ty = static_cast<double> (y.value ()); \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
768 return x OP ty; \ |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
769 } |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
770 |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
771 OCTAVE_FLOAT_INT_CMP_OP (<) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
772 OCTAVE_FLOAT_INT_CMP_OP (<=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
773 OCTAVE_FLOAT_INT_CMP_OP (>=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
774 OCTAVE_FLOAT_INT_CMP_OP (>) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
775 OCTAVE_FLOAT_INT_CMP_OP (==) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
776 OCTAVE_FLOAT_INT_CMP_OP (!=) |
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
777 |
4906 | 778 #define OCTAVE_INT_BITCMP_OP(OP) \ |
779 template <class T> \ | |
780 octave_int<T> \ | |
781 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ | |
782 { \ | |
783 return x.value () OP y.value (); \ | |
784 } | |
785 | |
786 OCTAVE_INT_BITCMP_OP (&) | |
787 OCTAVE_INT_BITCMP_OP (|) | |
788 OCTAVE_INT_BITCMP_OP (^) | |
789 | |
4952 | 790 template <class T1, class T2> |
791 octave_int<T1> | |
792 operator << (const octave_int<T1>& x, const T2& y) | |
793 { | |
5030 | 794 octave_int<T1> retval = x; |
4952 | 795 return retval <<= y; |
796 } | |
4906 | 797 |
4952 | 798 template <class T1, class T2> |
799 octave_int<T1> | |
800 operator >> (const octave_int<T1>& x, const T2& y) | |
801 { | |
5030 | 802 octave_int<T1> retval = x; |
4952 | 803 return retval >>= y; |
804 } | |
4906 | 805 |
4909 | 806 template <class T> |
807 octave_int<T> | |
4920 | 808 bitshift (const octave_int<T>& a, int n, |
809 const octave_int<T>& mask = std::numeric_limits<T>::max ()) | |
4909 | 810 { |
811 if (n > 0) | |
4952 | 812 return (a << n) & mask; |
4909 | 813 else if (n < 0) |
4952 | 814 return (a >> -n) & mask; |
4909 | 815 else |
816 return a; | |
817 } | |
818 | |
4902 | 819 #define OCTAVE_INT_CMP_OP(OP) \ |
820 template <class T1, class T2> \ | |
821 bool \ | |
822 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ | |
823 { \ | |
5072 | 824 return x.value () OP y.value (); \ |
4906 | 825 } |
4902 | 826 |
827 OCTAVE_INT_CMP_OP (<) | |
828 OCTAVE_INT_CMP_OP (<=) | |
829 OCTAVE_INT_CMP_OP (>=) | |
830 OCTAVE_INT_CMP_OP (>) | |
831 OCTAVE_INT_CMP_OP (==) | |
832 OCTAVE_INT_CMP_OP (!=) | |
833 | |
5072 | 834 // The following apply if the unsigned type is at least as wide as the |
835 // signed type (then we can cast postive signed values to the unsigned | |
836 // type and compare). | |
837 | |
838 #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
|
839 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
840 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
841 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 842 |
843 #define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
844 OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ | |
845 OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ | |
846 OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ | |
847 OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ | |
848 OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ | |
849 OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) | |
850 | |
851 #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
|
852 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
853 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
854 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 855 |
856 #define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ | |
857 OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ | |
858 OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ | |
859 OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ | |
860 OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ | |
861 OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ | |
862 OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) | |
863 | |
864 #define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ | |
865 OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ | |
866 OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) | |
867 | |
5828 | 868 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) |
869 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) | |
870 OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) | |
5072 | 871 |
5828 | 872 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) |
873 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) | |
874 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) | |
875 OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) | |
5072 | 876 |
877 // The following apply if the signed type is wider than the unsigned | |
878 // type (then we can cast unsigned values to the signed type and | |
879 // compare if the signed value is positive). | |
880 | |
881 #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
|
882 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
883 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
884 OCTAVE_API operator OP (const octave_int<UT>& lhs, const octave_int<ST>& rhs); |
5072 | 885 |
886 #define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
887 OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ | |
888 OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ | |
889 OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ | |
890 OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ | |
891 OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
892 OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
893 | |
894 #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
|
895 template <> \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
896 bool \ |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
897 OCTAVE_API operator OP (const octave_int<ST>& lhs, const octave_int<UT>& rhs); |
5072 | 898 |
899 #define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ | |
900 OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ | |
901 OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ | |
902 OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ | |
903 OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ | |
904 OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ | |
905 OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) | |
906 | |
907 #define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ | |
908 OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ | |
909 OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) | |
910 | |
5828 | 911 OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) |
5072 | 912 |
4902 | 913 #undef OCTAVE_INT_BINOP_TRAIT |
5072 | 914 #undef OCTAVE_US_S_FTR |
915 #undef OCTAVE_US_S_FTR_FCNS | |
916 #undef OCTAVE_S_US_FTR | |
917 #undef OCTAVE_S_US_FTR_FCNS | |
4902 | 918 #undef OCTAVE_INT_FIT_TO_RANGE |
919 #undef OCTAVE_INT_MIN_VAL2 | |
920 #undef OCTAVE_INT_MAX_VAL2 | |
921 #undef OCTAVE_INT_FIT_TO_RANGE2 | |
922 #undef OCTAVE_INT_BIN_OP | |
5072 | 923 #undef OCTAVE_INT_DOUBLE_BIN_OP |
924 #undef OCTAVE_DOUBLE_INT_BIN_OP | |
925 #undef OCTAVE_INT_DOUBLE_CMP_OP | |
926 #undef OCTAVE_DOUBLE_INT_CMP_OP | |
927 #undef OCTAVE_INT_BITCMP_OP | |
4902 | 928 #undef OCTAVE_INT_CMP_OP |
5072 | 929 #undef OCTAVE_US_TYPE1_CMP_OP_DECL |
930 #undef OCTAVE_US_TYPE1_CMP_OP_DECLS | |
931 #undef OCTAVE_SU_TYPE1_CMP_OP_DECL | |
932 #undef OCTAVE_SU_TYPE1_CMP_OP_DECLS | |
933 #undef OCTAVE_TYPE1_CMP_OP_DECLS | |
934 #undef OCTAVE_US_TYPE2_CMP_OP_DECL | |
935 #undef OCTAVE_US_TYPE2_CMP_OP_DECLS | |
936 #undef OCTAVE_SU_TYPE2_CMP_OP_DECL | |
937 #undef OCTAVE_SU_TYPE2_CMP_OP_DECLS | |
5074 | 938 #undef OCTAVE_TYPE2_CMP_OP_DECLS |
4902 | 939 |
940 #endif | |
941 | |
942 /* | |
943 ;;; Local Variables: *** | |
944 ;;; mode: C++ *** | |
945 ;;; End: *** | |
946 */ |