4908
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 John W. Eaton |
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4908
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "str-vec.h" |
|
29 #include "quit.h" |
|
30 |
|
31 #include "defun.h" |
|
32 #include "error.h" |
|
33 #include "ov.h" |
|
34 #include "ov-uint64.h" |
4915
|
35 #include "ov-uint32.h" |
|
36 #include "ov-uint16.h" |
|
37 #include "ov-uint8.h" |
|
38 #include "ov-int64.h" |
|
39 #include "ov-int32.h" |
|
40 #include "ov-int16.h" |
|
41 #include "ov-int8.h" |
|
42 #include "ov-scalar.h" |
|
43 #include "ov-re-mat.h" |
4908
|
44 |
|
45 // XXX FIXME XXX -- could probably eliminate some code duplication by |
|
46 // clever use of templates. |
|
47 |
4915
|
48 #define BITOPX(OP, FNAME, RET) \ |
|
49 { \ |
|
50 int nelx = x.numel (); \ |
|
51 int nely = y.numel (); \ |
|
52 \ |
|
53 bool is_scalar_op = (nelx == 1 || nely == 1); \ |
|
54 \ |
|
55 dim_vector dvx = x.dims (); \ |
|
56 dim_vector dvy = y.dims (); \ |
|
57 \ |
|
58 bool is_array_op = (dvx == dvy); \ |
|
59 \ |
|
60 if (is_array_op || is_scalar_op) \ |
|
61 { \ |
|
62 RET result; \ |
|
63 \ |
|
64 if (nelx != 1) \ |
|
65 result.resize (dvx); \ |
|
66 else \ |
|
67 result.resize (dvy); \ |
|
68 \ |
|
69 for (int i = 0; i < nelx; i++) \ |
|
70 if (is_scalar_op) \ |
|
71 for (int k = 0; k < nely; k++) \ |
|
72 result(i+k) = x(i) OP y(k); \ |
|
73 else \ |
|
74 result(i) = x(i) OP y(i); \ |
|
75 \ |
|
76 retval = result; \ |
|
77 } \ |
|
78 else \ |
|
79 error ("%s: size of x and y must match, or one operand must be a scalar", FNAME); \ |
|
80 } |
|
81 |
4908
|
82 #define BITOP(OP, FNAME) \ |
|
83 \ |
|
84 octave_value retval; \ |
|
85 \ |
|
86 int nargin = args.length (); \ |
|
87 \ |
|
88 if (nargin == 2) \ |
|
89 { \ |
4952
|
90 if ((args(0).class_name () == octave_scalar::static_class_name ()) \ |
|
91 || (args(1).class_name () == octave_scalar::static_class_name ())) \ |
4908
|
92 { \ |
4952
|
93 bool arg0_is_int = (args(0).class_name () != \ |
|
94 octave_scalar::static_class_name ()); \ |
|
95 bool arg1_is_int = (args(1).class_name () != \ |
|
96 octave_scalar::static_class_name ()); \ |
|
97 \ |
|
98 if (! (arg0_is_int || arg1_is_int)) \ |
4915
|
99 { \ |
4919
|
100 uint64NDArray x (args(0).array_value ()); \ |
4915
|
101 uint64NDArray y (args(1).array_value ()); \ |
4919
|
102 if (! error_state) \ |
|
103 BITOPX (OP, FNAME, uint64NDArray); \ |
|
104 retval = retval.array_value (); \ |
4908
|
105 } \ |
|
106 else \ |
4915
|
107 { \ |
|
108 int p = (arg0_is_int ? 1 : 0); \ |
|
109 int q = (arg0_is_int ? 0 : 1); \ |
4919
|
110 \ |
4915
|
111 NDArray dx = args(p).array_value (); \ |
|
112 \ |
4919
|
113 if (args(q).type_id () == octave_uint64_matrix::static_type_id () \ |
|
114 || args(q).type_id () == octave_uint64_scalar::static_type_id ()) \ |
|
115 { \ |
|
116 uint64NDArray x (dx); \ |
|
117 uint64NDArray y = args(q).uint64_array_value (); \ |
|
118 if (! error_state) \ |
|
119 BITOPX (OP, FNAME, uint64NDArray); \ |
4915
|
120 } \ |
4919
|
121 else if (args(q).type_id () == octave_uint32_matrix::static_type_id () \ |
|
122 || args(q).type_id () == octave_uint32_scalar::static_type_id ()) \ |
|
123 { \ |
|
124 uint32NDArray x (dx); \ |
|
125 uint32NDArray y = args(q).uint32_array_value (); \ |
|
126 if (! error_state) \ |
|
127 BITOPX (OP, FNAME, uint32NDArray); \ |
|
128 } \ |
|
129 else if (args(q).type_id () == octave_uint16_matrix::static_type_id () \ |
|
130 || args(q).type_id () == octave_uint16_scalar::static_type_id ()) \ |
|
131 { \ |
|
132 uint16NDArray x (dx); \ |
|
133 uint16NDArray y = args(q).uint16_array_value (); \ |
|
134 if (! error_state) \ |
|
135 BITOPX (OP, FNAME, uint16NDArray); \ |
|
136 } \ |
|
137 else if (args(q).type_id () == octave_uint8_matrix::static_type_id () \ |
|
138 || args(q).type_id () == octave_uint8_scalar::static_type_id ()) \ |
|
139 { \ |
|
140 uint8NDArray x (dx); \ |
|
141 uint8NDArray y = args(q).uint8_array_value (); \ |
|
142 if (! error_state) \ |
|
143 BITOPX (OP, FNAME, uint8NDArray); \ |
4915
|
144 } \ |
4919
|
145 else if (args(q).type_id () == octave_int64_matrix::static_type_id () \ |
|
146 || args(q).type_id () == octave_int64_scalar::static_type_id ()) \ |
|
147 { \ |
|
148 int64NDArray x (dx); \ |
|
149 int64NDArray y = args(q).int64_array_value (); \ |
|
150 if (! error_state) \ |
|
151 BITOPX (OP, FNAME, int64NDArray); \ |
|
152 } \ |
|
153 else if (args(q).type_id () == octave_int32_matrix::static_type_id () \ |
|
154 || args(q).type_id () == octave_int32_scalar::static_type_id ()) \ |
|
155 { \ |
|
156 int32NDArray x (dx); \ |
|
157 int32NDArray y = args(q).int32_array_value (); \ |
|
158 if (! error_state) \ |
|
159 BITOPX (OP, FNAME, int32NDArray); \ |
|
160 } \ |
|
161 else if (args(q).type_id () == octave_int16_matrix::static_type_id () \ |
|
162 || args(q).type_id () == octave_int16_scalar::static_type_id ()) \ |
|
163 { \ |
|
164 int16NDArray x (dx); \ |
|
165 int16NDArray y = args(q).int16_array_value (); \ |
|
166 if (! error_state) \ |
|
167 BITOPX (OP, FNAME, int16NDArray); \ |
|
168 } \ |
|
169 else if (args(q).type_id () == octave_int8_matrix::static_type_id () \ |
|
170 || args(q).type_id () == octave_int8_scalar::static_type_id ()) \ |
|
171 { \ |
|
172 int8NDArray x (dx); \ |
|
173 int8NDArray y = args(q).int8_array_value (); \ |
|
174 if (! error_state) \ |
|
175 BITOPX (OP, FNAME, int8NDArray); \ |
4915
|
176 } \ |
4919
|
177 else \ |
|
178 error ("%s: invalid operand type", FNAME); \ |
|
179 } \ |
|
180 } \ |
4952
|
181 else if (args(0).class_name () == args(1).class_name ()) \ |
4915
|
182 { \ |
4919
|
183 if (args(0).type_id () == octave_uint64_matrix::static_type_id () \ |
|
184 || args(0).type_id () == octave_uint64_scalar::static_type_id ()) \ |
|
185 { \ |
|
186 uint64NDArray x = args(0).uint64_array_value (); \ |
|
187 uint64NDArray y = args(1).uint64_array_value (); \ |
|
188 if (! error_state) \ |
|
189 BITOPX (OP, FNAME, uint64NDArray); \ |
|
190 } \ |
|
191 else if (args(0).type_id () == octave_uint32_matrix::static_type_id () \ |
|
192 || args(0).type_id () == octave_uint32_scalar::static_type_id ()) \ |
4915
|
193 { \ |
4919
|
194 uint32NDArray x = args(0).uint32_array_value (); \ |
|
195 uint32NDArray y = args(1).uint32_array_value (); \ |
|
196 if (! error_state) \ |
|
197 BITOPX (OP, FNAME, uint32NDArray); \ |
|
198 } \ |
|
199 else if (args(0).type_id () == octave_uint16_matrix::static_type_id () \ |
|
200 || args(0).type_id () == octave_uint16_scalar::static_type_id ()) \ |
|
201 { \ |
|
202 uint16NDArray x = args(0).uint16_array_value (); \ |
|
203 uint16NDArray y = args(1).uint16_array_value (); \ |
|
204 if (! error_state) \ |
|
205 BITOPX (OP, FNAME, uint16NDArray); \ |
4915
|
206 } \ |
4919
|
207 else if (args(0).type_id () == octave_uint8_matrix::static_type_id () \ |
|
208 || args(0).type_id () == octave_uint8_scalar::static_type_id ()) \ |
4915
|
209 { \ |
4919
|
210 uint8NDArray x = args(0).uint8_array_value (); \ |
|
211 uint8NDArray y = args(1).uint8_array_value (); \ |
|
212 if (! error_state) \ |
|
213 BITOPX (OP, FNAME, uint8NDArray); \ |
4915
|
214 } \ |
4919
|
215 else if (args(0).type_id () == octave_int64_matrix::static_type_id () \ |
|
216 || args(0).type_id () == octave_int64_scalar::static_type_id ()) \ |
4915
|
217 { \ |
4919
|
218 int64NDArray x = args(0).int64_array_value (); \ |
|
219 int64NDArray y = args(1).int64_array_value (); \ |
|
220 if (! error_state) \ |
|
221 BITOPX (OP, FNAME, int64NDArray); \ |
4915
|
222 } \ |
4919
|
223 else if (args(0).type_id () == octave_int32_matrix::static_type_id () \ |
|
224 || args(0).type_id () == octave_int32_scalar::static_type_id ()) \ |
4915
|
225 { \ |
4919
|
226 int32NDArray x = args(0).int32_array_value (); \ |
|
227 int32NDArray y = args(1).int32_array_value (); \ |
|
228 if (! error_state) \ |
|
229 BITOPX (OP, FNAME, int32NDArray); \ |
4915
|
230 } \ |
4919
|
231 else if (args(0).type_id () == octave_int16_matrix::static_type_id () \ |
|
232 || args(0).type_id () == octave_int16_scalar::static_type_id ()) \ |
4915
|
233 { \ |
4919
|
234 int16NDArray x = args(0).int16_array_value (); \ |
|
235 int16NDArray y = args(1).int16_array_value (); \ |
|
236 if (! error_state) \ |
|
237 BITOPX (OP, FNAME, int16NDArray); \ |
4915
|
238 } \ |
4919
|
239 else if (args(0).type_id () == octave_int8_matrix::static_type_id () \ |
|
240 || args(0).type_id () == octave_int8_scalar::static_type_id ()) \ |
4915
|
241 { \ |
4919
|
242 int8NDArray x = args(0).int8_array_value (); \ |
|
243 int8NDArray y = args(1).int8_array_value (); \ |
|
244 if (! error_state) \ |
|
245 BITOPX (OP, FNAME, int8NDArray); \ |
4915
|
246 } \ |
|
247 else \ |
4919
|
248 error ("%s: invalid operand type", FNAME); \ |
4915
|
249 } \ |
4908
|
250 else \ |
4915
|
251 error ("%s: must have matching operand types", FNAME); \ |
4908
|
252 } \ |
|
253 else \ |
|
254 print_usage (FNAME); \ |
|
255 \ |
|
256 return retval |
|
257 |
|
258 DEFUN (bitand, args, , |
|
259 "-*- texinfo -*-\n\ |
|
260 @deftypefn {Built-in Function} {} bitand (@var{x}, @var{y})\n\ |
4920
|
261 Return the bitwise AND of nonnegative integers.\n\ |
4908
|
262 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
263 @end deftypefn\n\ |
|
264 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
265 { |
|
266 BITOP (&, "bitand"); |
|
267 } |
|
268 |
|
269 DEFUN (bitor, args, , |
|
270 "-*- texinfo -*-\n\ |
|
271 @deftypefn {Built-in Function} {} bitor (@var{x}, @var{y})\n\ |
4920
|
272 Return the bitwise OR of nonnegative integers.\n\ |
4908
|
273 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
274 @end deftypefn\n\ |
|
275 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
276 { |
|
277 BITOP (|, "bitor"); |
|
278 } |
|
279 |
|
280 DEFUN (bitxor, args, , |
|
281 "-*- texinfo -*-\n\ |
|
282 @deftypefn {Built-in Function} {} bitxor (@var{x}, @var{y})\n\ |
4920
|
283 Return the bitwise XOR of nonnegative integers.\n\ |
4908
|
284 @var{x}, @var{y} must be in range [0..bitmax]\n\ |
|
285 @end deftypefn\n\ |
|
286 @seealso{bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
287 { |
|
288 BITOP (^, "bitxor"); |
|
289 } |
|
290 |
4915
|
291 static EIGHT_BYTE_INT |
4920
|
292 bitshift (double a, int n, EIGHT_BYTE_INT mask) |
4908
|
293 { |
4915
|
294 if (n > 0) |
4920
|
295 return (static_cast<EIGHT_BYTE_INT> (a) << n) & mask; |
4915
|
296 else if (n < 0) |
4920
|
297 return (static_cast<EIGHT_BYTE_INT> (a) >> -n) & mask; |
4915
|
298 else |
4920
|
299 return static_cast<EIGHT_BYTE_INT> (a) & mask; |
4908
|
300 } |
|
301 |
4919
|
302 // Note that the bitshift operators are undefined if shifted by more |
|
303 // bits than in the type, so we need to test for the size of the |
|
304 // shift. |
|
305 |
4908
|
306 #define DO_BITSHIFT(T) \ |
4919
|
307 if (! error_state) \ |
|
308 { \ |
|
309 double d1, d2; \ |
4908
|
310 \ |
4919
|
311 if (n.all_integers (d1, d2)) \ |
|
312 { \ |
|
313 int m_nel = m.numel (); \ |
|
314 int n_nel = n.numel (); \ |
4908
|
315 \ |
4919
|
316 bool is_scalar_op = (m_nel == 1 || n_nel == 1); \ |
4908
|
317 \ |
4919
|
318 dim_vector m_dv = m.dims (); \ |
|
319 dim_vector n_dv = n.dims (); \ |
|
320 \ |
|
321 bool is_array_op = (m_dv == n_dv); \ |
4908
|
322 \ |
4919
|
323 if (is_array_op || is_scalar_op) \ |
|
324 { \ |
|
325 T ## NDArray result; \ |
4908
|
326 \ |
4919
|
327 if (m_nel != 1) \ |
|
328 result.resize (m_dv); \ |
|
329 else \ |
|
330 result.resize (n_dv); \ |
4908
|
331 \ |
4919
|
332 for (int i = 0; i < m_nel; i++) \ |
|
333 if (is_scalar_op) \ |
|
334 for (int k = 0; k < n_nel; k++) \ |
|
335 if (static_cast<int> (n(k)) >= bits_in_type) \ |
|
336 result(i+k) = 0; \ |
4908
|
337 else \ |
4920
|
338 result(i+k) = bitshift (m(i), static_cast<int> (n(k)), mask); \ |
4919
|
339 else \ |
|
340 if (static_cast<int> (n(i)) >= bits_in_type) \ |
|
341 result(i) = 0; \ |
|
342 else \ |
4920
|
343 result(i) = bitshift (m(i), static_cast<int> (n(i)), mask); \ |
4908
|
344 \ |
4919
|
345 retval = result; \ |
4908
|
346 } \ |
4919
|
347 else \ |
|
348 error ("bitshift: size of A and N must match, or one operand must be a scalar"); \ |
|
349 } \ |
|
350 else \ |
|
351 error ("bitshift: expecting second argument to be integer"); \ |
|
352 } |
4915
|
353 |
4919
|
354 #define DO_UBITSHIFT(T, N) \ |
|
355 do \ |
|
356 { \ |
4920
|
357 int bits_in_type = octave_ ## T :: nbits (); \ |
4919
|
358 T ## NDArray m = m_arg.T ## _array_value (); \ |
|
359 octave_ ## T mask = ~0ULL; \ |
4920
|
360 if ((N) < bits_in_type) \ |
|
361 mask = bitshift (mask, (N) - bits_in_type); \ |
4919
|
362 else if ((N) < 1) \ |
|
363 mask = 0; \ |
|
364 DO_BITSHIFT (T); \ |
|
365 } \ |
4915
|
366 while (0) |
|
367 |
4919
|
368 #define DO_SBITSHIFT(T, N) \ |
|
369 do \ |
|
370 { \ |
4920
|
371 int bits_in_type = octave_ ## T :: nbits (); \ |
4919
|
372 T ## NDArray m = m_arg.T ## _array_value (); \ |
|
373 octave_ ## T mask = -1; \ |
4920
|
374 if ((N) < bits_in_type) \ |
|
375 mask = bitshift (mask, (N) - bits_in_type); \ |
4919
|
376 else if ((N) < 1) \ |
|
377 mask = 0; \ |
|
378 DO_BITSHIFT (T); \ |
|
379 } \ |
4908
|
380 while (0) |
|
381 |
|
382 DEFUN (bitshift, args, , |
|
383 "-*- texinfo -*-\n\ |
|
384 @deftypefn {Function File} {} bitshift (@var{a}, @var{k})\n\ |
|
385 @deftypefnx {Function File} {} bitshift (@var{a}, @var{k}, @var{n})\n\ |
4920
|
386 Return a @var{k} bit shift of @var{n}- digit unsigned\n\ |
|
387 integers in @var{a}. A positive @var{k} leads to a left shift.\n\ |
|
388 A negative value to a right shift. If @var{n} is omitted it defaults\n\ |
|
389 to log2(bitmax)+1.\n\ |
4915
|
390 @var{n} must be in range [1,log2(bitmax)+1] usually [1,33]\n\ |
4908
|
391 \n\ |
|
392 @example\n\ |
|
393 bitshift (eye (3), 1))\n\ |
|
394 @result{}\n\ |
|
395 @group\n\ |
|
396 2 0 0\n\ |
|
397 0 2 0\n\ |
|
398 0 0 2\n\ |
|
399 @end group\n\ |
|
400 \n\ |
|
401 bitshift (10, [-2, -1, 0, 1, 2])\n\ |
|
402 @result{} 2 5 10 20 40\n\ |
|
403 \n\ |
|
404 bitshift ([1, 10], 2, [3,4])\n\ |
|
405 @result{} 4 8\n\ |
|
406 @end example\n\ |
|
407 @end deftypefn\n\ |
|
408 @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}") |
|
409 { |
|
410 octave_value retval; |
|
411 |
|
412 int nargin = args.length (); |
|
413 |
4915
|
414 if (nargin == 2 || nargin == 3) |
4908
|
415 { |
4915
|
416 int nbits = 64; |
|
417 |
4920
|
418 NDArray n = args(1).array_value (); |
|
419 |
|
420 if (error_state) |
|
421 error ("bitshift: expecting integer as second argument"); |
|
422 else |
4915
|
423 { |
4920
|
424 if (nargin == 3) |
|
425 { |
|
426 nbits = args(2).int_value (); |
4915
|
427 |
4920
|
428 if (error_state) |
|
429 error ("bitshift: expecting integer as third argument"); |
|
430 else if (nbits < 0) |
|
431 error ("bitshift: number of bits to mask must be positive"); |
|
432 } |
4915
|
433 } |
|
434 |
|
435 if (error_state) |
|
436 return retval; |
4908
|
437 |
|
438 octave_value m_arg = args(0); |
|
439 std::string cname = m_arg.class_name (); |
|
440 |
|
441 if (cname == "uint8") |
4915
|
442 DO_UBITSHIFT (uint8, nbits < 8 ? nbits : 8); |
4908
|
443 else if (cname == "uint16") |
4915
|
444 DO_UBITSHIFT (uint16, nbits < 16 ? nbits : 16); |
4908
|
445 else if (cname == "uint32") |
4915
|
446 DO_UBITSHIFT (uint32, nbits < 32 ? nbits : 32); |
4908
|
447 else if (cname == "uint64") |
4915
|
448 DO_UBITSHIFT (uint64, nbits < 64 ? nbits : 64); |
|
449 else if (cname == "int8") |
|
450 DO_SBITSHIFT (int8, nbits < 8 ? nbits : 8); |
|
451 else if (cname == "int16") |
|
452 DO_SBITSHIFT (int16, nbits < 16 ? nbits : 16); |
|
453 else if (cname == "int32") |
|
454 DO_SBITSHIFT (int32, nbits < 32 ? nbits : 32); |
|
455 else if (cname == "int64") |
|
456 DO_SBITSHIFT (int64, nbits < 64 ? nbits : 64); |
|
457 else if (cname == "double") |
|
458 { |
|
459 nbits = (nbits < 53 ? nbits : 53); |
|
460 EIGHT_BYTE_INT mask = 0x1FFFFFFFFFFFFFLL; |
|
461 if (nbits < 53) |
|
462 mask = mask >> (53 - nbits); |
|
463 else if (nbits < 1) |
|
464 mask = 0; |
|
465 int bits_in_type = 64; |
|
466 NDArray m = m_arg.array_value (); |
|
467 DO_BITSHIFT ( ); |
|
468 } |
4908
|
469 else |
|
470 error ("bitshift: not defined for %s objects", cname.c_str ()); |
|
471 } |
|
472 else |
|
473 print_usage ("bitshift"); |
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
|
478 DEFUN (bitmax, args, , |
|
479 "-*- texinfo -*-\n\ |
4915
|
480 @deftypefn {Built-in Function} {} bitmax ()\n\ |
4920
|
481 Return the largest integer that can be represented as a floating point\n\ |
|
482 value. On IEEE-754 compatiable systems, @code{bitmax} is @code{2^53 - 1}.\n\ |
4915
|
483 @end deftypefn") |
|
484 { |
|
485 octave_value retval; |
4919
|
486 if (args.length () != 0) |
4915
|
487 print_usage ("bitmax"); |
|
488 else |
4919
|
489 retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL)); |
4915
|
490 return retval; |
|
491 } |
|
492 |
|
493 DEFUN (intmax, args, , |
|
494 "-*- texinfo -*-\n\ |
|
495 @deftypefn {Built-in Function} {} intmax (@var{type})\n\ |
5040
|
496 Return the largest integer that can be represented in an integer type.\n\ |
|
497 The variable @var{type} can be\n\ |
|
498 \n\ |
|
499 @table @code\n\ |
|
500 @item int8\n\ |
|
501 signed 8-bit integer.\n\ |
|
502 @item int16\n\ |
|
503 signed 16-bit integer.\n\ |
|
504 @item int32\n\ |
|
505 signed 32-bit integer.\n\ |
|
506 @item int64\n\ |
|
507 signed 64-bit integer.\n\ |
|
508 @item uint8\n\ |
|
509 unsigned 8-bit integer.\n\ |
|
510 @item uint16\n\ |
|
511 unsigned 16-bit integer.\n\ |
|
512 @item uint32\n\ |
|
513 unsigned 32-bit integer.\n\ |
|
514 @item uint64\n\ |
|
515 unsigned 64-bit integer.\n\ |
|
516 @end table\n\ |
|
517 \n\ |
|
518 The default for @var{type} is @code{uint32}.\n\ |
|
519 @seealso{intmin,bitmax}\n\ |
4908
|
520 @end deftypefn") |
|
521 { |
|
522 octave_value retval; |
4915
|
523 std::string cname = "int32"; |
|
524 int nargin = args.length (); |
|
525 |
4919
|
526 if (nargin == 1 && args(0).is_string ()) |
4915
|
527 cname = args(0).string_value (); |
|
528 else if (nargin != 0) |
|
529 { |
|
530 print_usage ("intmax"); |
|
531 return retval; |
|
532 } |
|
533 |
|
534 if (cname == "uint8") |
4919
|
535 retval = octave_uint8 (std::numeric_limits<octave_uint8_t>::max ()); |
4915
|
536 else if (cname == "uint16") |
4919
|
537 retval = octave_uint16 (std::numeric_limits<octave_uint16_t>::max ()); |
4915
|
538 else if (cname == "uint32") |
4919
|
539 retval = octave_uint32 (std::numeric_limits<octave_uint32_t>::max ()); |
4915
|
540 else if (cname == "uint64") |
4919
|
541 retval = octave_uint64 (std::numeric_limits<octave_uint64_t>::max ()); |
4915
|
542 else if (cname == "int8") |
4919
|
543 retval = octave_int8 (std::numeric_limits<octave_int8_t>::max ()); |
4915
|
544 else if (cname == "int16") |
4919
|
545 retval = octave_int16 (std::numeric_limits<octave_int16_t>::max ()); |
4915
|
546 else if (cname == "int32") |
4919
|
547 retval = octave_int32 (std::numeric_limits<octave_int32_t>::max ()); |
4915
|
548 else if (cname == "int64") |
4919
|
549 retval = octave_int64 (std::numeric_limits<octave_int64_t>::max ()); |
4915
|
550 else |
|
551 error ("intmax: not defined for '%s' objects", cname.c_str ()); |
|
552 |
|
553 return retval; |
|
554 } |
|
555 |
|
556 DEFUN (intmin, args, , |
|
557 "-*- texinfo -*-\n\ |
|
558 @deftypefn {Built-in Function} {} intmin (@var{type})\n\ |
5040
|
559 Return the smallest integer that can be represented in an integer type.\n\ |
|
560 The variable @var{type} can be\n\ |
|
561 \n\ |
|
562 @table @code\n\ |
|
563 @item int8\n\ |
|
564 signed 8-bit integer.\n\ |
|
565 @item int16\n\ |
|
566 signed 16-bit integer.\n\ |
|
567 @item int32\n\ |
|
568 signed 32-bit integer.\n\ |
|
569 @item int64\n\ |
|
570 signed 64-bit integer.\n\ |
|
571 @item uint8\n\ |
|
572 unsigned 8-bit integer.\n\ |
|
573 @item uint16\n\ |
|
574 unsigned 16-bit integer.\n\ |
|
575 @item uint32\n\ |
|
576 unsigned 32-bit integer.\n\ |
|
577 @item uint64\n\ |
|
578 unsigned 64-bit integer.\n\ |
|
579 @end table\n\ |
|
580 \n\ |
|
581 The default for @var{type} is @code{uint32}.\n\ |
|
582 @seealso{intmax,bitmax}\n\ |
4915
|
583 @end deftypefn") |
|
584 { |
|
585 octave_value retval; |
|
586 std::string cname = "int32"; |
|
587 int nargin = args.length (); |
|
588 |
4919
|
589 if (nargin == 1 && args(0).is_string ()) |
4915
|
590 cname = args(0).string_value (); |
|
591 else if (nargin != 0) |
|
592 { |
|
593 print_usage ("intmin"); |
|
594 return retval; |
|
595 } |
|
596 |
|
597 if (cname == "uint8") |
4919
|
598 retval = octave_uint8 (std::numeric_limits<octave_uint8_t>::min ()); |
4915
|
599 else if (cname == "uint16") |
|
600 retval = octave_uint16 (std::numeric_limits<octave_uint16_t>::min()); |
|
601 else if (cname == "uint32") |
4919
|
602 retval = octave_uint32 (std::numeric_limits<octave_uint32_t>::min ()); |
4915
|
603 else if (cname == "uint64") |
4919
|
604 retval = octave_uint64 (std::numeric_limits<octave_uint64_t>::min ()); |
4915
|
605 else if (cname == "int8") |
4919
|
606 retval = octave_int8 (std::numeric_limits<octave_int8_t>::min ()); |
4915
|
607 else if (cname == "int16") |
4919
|
608 retval = octave_int16 (std::numeric_limits<octave_int16_t>::min ()); |
4915
|
609 else if (cname == "int32") |
4919
|
610 retval = octave_int32 (std::numeric_limits<octave_int32_t>::min ()); |
4915
|
611 else if (cname == "int64") |
4919
|
612 retval = octave_int64 (std::numeric_limits<octave_int64_t>::min ()); |
4915
|
613 else |
|
614 error ("intmin: not defined for '%s' objects", cname.c_str ()); |
|
615 |
4908
|
616 return retval; |
|
617 } |
|
618 |
|
619 /* |
|
620 ;;; Local Variables: *** |
|
621 ;;; mode: C++ *** |
|
622 ;;; End: *** |
|
623 */ |