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