comparison liboctave/oct-inttypes.h @ 4943:1a499d0c58f5

[project @ 2004-08-31 00:51:31 by jwe]
author jwe
date Tue, 31 Aug 2004 00:51:31 +0000
parents b22a7a1db0d5
children f6b63ff1119b
comparison
equal deleted inserted replaced
4942:a0f2839f6ac8 4943:1a499d0c58f5
133 octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx) 133 octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx)
134 { 134 {
135 return (x > mx ? mx : (x < mn ? mn : static_cast<T2> (x))); 135 return (x > mx ? mx : (x < mn ? mn : static_cast<T2> (x)));
136 } 136 }
137 137
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
144 #define US_S_FTR(T1, T2, TC) \
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
152 #define US_S_FTR_FCNS(T) \
153 US_S_FTR(T, char, unsigned char) \
154 US_S_FTR(T, signed char, unsigned char) \
155 US_S_FTR(T, short, unsigned short) \
156 US_S_FTR(T, int, unsigned int) \
157 US_S_FTR(T, long, unsigned long) \
158 US_S_FTR(T, long long, unsigned long long)
159
160 US_S_FTR_FCNS (unsigned char)
161 US_S_FTR_FCNS (unsigned short)
162 US_S_FTR_FCNS (unsigned int)
163 US_S_FTR_FCNS (unsigned long)
164 US_S_FTR_FCNS (unsigned long long)
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
171 #define S_US_FTR(T1, T2) \
172 template <> \
173 inline T2 \
174 octave_int_fit_to_range<T1, T2> (const T1& x, const T2&, const T2&) \
175 { \
176 return x < 0 ? 0 : x; \
177 }
178
179 #define S_US_FTR_FCNS(T) \
180 S_US_FTR(T, unsigned char) \
181 S_US_FTR(T, unsigned short) \
182 S_US_FTR(T, unsigned int) \
183 S_US_FTR(T, unsigned long) \
184 S_US_FTR(T, unsigned long long)
185
186 S_US_FTR_FCNS (char)
187 S_US_FTR_FCNS (signed char)
188 S_US_FTR_FCNS (short)
189 S_US_FTR_FCNS (int)
190 S_US_FTR_FCNS (long)
191 S_US_FTR_FCNS (long long)
192
138 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \ 193 #define OCTAVE_INT_FIT_TO_RANGE(r, T) \
139 octave_int_fit_to_range (r, std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()) 194 octave_int_fit_to_range (r, \
195 std::numeric_limits<T>::min (), \
196 std::numeric_limits<T>::max ())
140 197
141 #define OCTAVE_INT_MIN_VAL2(T1, T2) \ 198 #define OCTAVE_INT_MIN_VAL2(T1, T2) \
142 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min () 199 std::numeric_limits<typename octave_int_binop_traits<T1, T2>::TR>::min ()
143 200
144 #define OCTAVE_INT_MAX_VAL2(T1, T2) \ 201 #define OCTAVE_INT_MAX_VAL2(T1, T2) \
152 template <class T> 209 template <class T>
153 class 210 class
154 octave_int 211 octave_int
155 { 212 {
156 public: 213 public:
214
215 typedef T val_type;
157 216
158 octave_int (void) : ival () { } 217 octave_int (void) : ival () { }
159 218
160 template <class U> 219 template <class U>
161 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { } 220 octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { }