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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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 |
|
44 // XXX FIXME XXX -- could probably eliminate some code duplication by |
|
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 \ |
|
253 print_usage (FNAME); \ |
|
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\ |
|
262 @end deftypefn\n\ |
|
263 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
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\ |
|
273 @end deftypefn\n\ |
|
274 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
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\ |
|
284 @end deftypefn\n\ |
|
285 @seealso{bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax}") |
|
286 { |
|
287 BITOP (^, "bitxor"); |
|
288 } |
|
289 |
4915
|
290 static EIGHT_BYTE_INT |
4920
|
291 bitshift (double a, int n, EIGHT_BYTE_INT mask) |
4908
|
292 { |
4915
|
293 if (n > 0) |
4920
|
294 return (static_cast<EIGHT_BYTE_INT> (a) << n) & mask; |
4915
|
295 else if (n < 0) |
4920
|
296 return (static_cast<EIGHT_BYTE_INT> (a) >> -n) & mask; |
4915
|
297 else |
4920
|
298 return static_cast<EIGHT_BYTE_INT> (a) & mask; |
4908
|
299 } |
|
300 |
4919
|
301 // Note that the bitshift operators are undefined if shifted by more |
|
302 // bits than in the type, so we need to test for the size of the |
|
303 // shift. |
|
304 |
4908
|
305 #define DO_BITSHIFT(T) \ |
4919
|
306 if (! error_state) \ |
|
307 { \ |
|
308 double d1, d2; \ |
4908
|
309 \ |
4919
|
310 if (n.all_integers (d1, d2)) \ |
|
311 { \ |
|
312 int m_nel = m.numel (); \ |
|
313 int n_nel = n.numel (); \ |
4908
|
314 \ |
4919
|
315 bool is_scalar_op = (m_nel == 1 || n_nel == 1); \ |
4908
|
316 \ |
4919
|
317 dim_vector m_dv = m.dims (); \ |
|
318 dim_vector n_dv = n.dims (); \ |
|
319 \ |
|
320 bool is_array_op = (m_dv == n_dv); \ |
4908
|
321 \ |
4919
|
322 if (is_array_op || is_scalar_op) \ |
|
323 { \ |
|
324 T ## NDArray result; \ |
4908
|
325 \ |
4919
|
326 if (m_nel != 1) \ |
|
327 result.resize (m_dv); \ |
|
328 else \ |
|
329 result.resize (n_dv); \ |
4908
|
330 \ |
4919
|
331 for (int i = 0; i < m_nel; i++) \ |
|
332 if (is_scalar_op) \ |
|
333 for (int k = 0; k < n_nel; k++) \ |
|
334 if (static_cast<int> (n(k)) >= bits_in_type) \ |
|
335 result(i+k) = 0; \ |
4908
|
336 else \ |
4920
|
337 result(i+k) = bitshift (m(i), static_cast<int> (n(k)), mask); \ |
4919
|
338 else \ |
|
339 if (static_cast<int> (n(i)) >= bits_in_type) \ |
|
340 result(i) = 0; \ |
|
341 else \ |
4920
|
342 result(i) = bitshift (m(i), static_cast<int> (n(i)), mask); \ |
4908
|
343 \ |
4919
|
344 retval = result; \ |
4908
|
345 } \ |
4919
|
346 else \ |
|
347 error ("bitshift: size of A and N must match, or one operand must be a scalar"); \ |
|
348 } \ |
|
349 else \ |
|
350 error ("bitshift: expecting second argument to be integer"); \ |
|
351 } |
4915
|
352 |
4919
|
353 #define DO_UBITSHIFT(T, N) \ |
|
354 do \ |
|
355 { \ |
4920
|
356 int bits_in_type = octave_ ## T :: nbits (); \ |
4919
|
357 T ## NDArray m = m_arg.T ## _array_value (); \ |
|
358 octave_ ## T mask = ~0ULL; \ |
4920
|
359 if ((N) < bits_in_type) \ |
|
360 mask = bitshift (mask, (N) - bits_in_type); \ |
4919
|
361 else if ((N) < 1) \ |
|
362 mask = 0; \ |
|
363 DO_BITSHIFT (T); \ |
|
364 } \ |
4915
|
365 while (0) |
|
366 |
4919
|
367 #define DO_SBITSHIFT(T, N) \ |
|
368 do \ |
|
369 { \ |
4920
|
370 int bits_in_type = octave_ ## T :: nbits (); \ |
4919
|
371 T ## NDArray m = m_arg.T ## _array_value (); \ |
|
372 octave_ ## T mask = -1; \ |
4920
|
373 if ((N) < bits_in_type) \ |
|
374 mask = bitshift (mask, (N) - bits_in_type); \ |
4919
|
375 else if ((N) < 1) \ |
|
376 mask = 0; \ |
|
377 DO_BITSHIFT (T); \ |
|
378 } \ |
4908
|
379 while (0) |
|
380 |
|
381 DEFUN (bitshift, args, , |
|
382 "-*- texinfo -*-\n\ |
|
383 @deftypefn {Function File} {} bitshift (@var{a}, @var{k})\n\ |
|
384 @deftypefnx {Function File} {} bitshift (@var{a}, @var{k}, @var{n})\n\ |
4920
|
385 Return a @var{k} bit shift of @var{n}- digit unsigned\n\ |
|
386 integers in @var{a}. A positive @var{k} leads to a left shift.\n\ |
|
387 A negative value to a right shift. If @var{n} is omitted it defaults\n\ |
|
388 to log2(bitmax)+1.\n\ |
4915
|
389 @var{n} must be in range [1,log2(bitmax)+1] usually [1,33]\n\ |
4908
|
390 \n\ |
|
391 @example\n\ |
|
392 bitshift (eye (3), 1))\n\ |
|
393 @result{}\n\ |
|
394 @group\n\ |
|
395 2 0 0\n\ |
|
396 0 2 0\n\ |
|
397 0 0 2\n\ |
|
398 @end group\n\ |
|
399 \n\ |
|
400 bitshift (10, [-2, -1, 0, 1, 2])\n\ |
|
401 @result{} 2 5 10 20 40\n\ |
|
402 \n\ |
|
403 bitshift ([1, 10], 2, [3,4])\n\ |
|
404 @result{} 4 8\n\ |
|
405 @end example\n\ |
|
406 @end deftypefn\n\ |
|
407 @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}") |
|
408 { |
|
409 octave_value retval; |
|
410 |
|
411 int nargin = args.length (); |
|
412 |
4915
|
413 if (nargin == 2 || nargin == 3) |
4908
|
414 { |
4915
|
415 int nbits = 64; |
|
416 |
4920
|
417 NDArray n = args(1).array_value (); |
|
418 |
|
419 if (error_state) |
|
420 error ("bitshift: expecting integer as second argument"); |
|
421 else |
4915
|
422 { |
4920
|
423 if (nargin == 3) |
|
424 { |
|
425 nbits = args(2).int_value (); |
4915
|
426 |
4920
|
427 if (error_state) |
|
428 error ("bitshift: expecting integer as third argument"); |
|
429 else if (nbits < 0) |
|
430 error ("bitshift: number of bits to mask must be positive"); |
|
431 } |
4915
|
432 } |
|
433 |
|
434 if (error_state) |
|
435 return retval; |
4908
|
436 |
|
437 octave_value m_arg = args(0); |
|
438 std::string cname = m_arg.class_name (); |
|
439 |
|
440 if (cname == "uint8") |
4915
|
441 DO_UBITSHIFT (uint8, nbits < 8 ? nbits : 8); |
4908
|
442 else if (cname == "uint16") |
4915
|
443 DO_UBITSHIFT (uint16, nbits < 16 ? nbits : 16); |
4908
|
444 else if (cname == "uint32") |
4915
|
445 DO_UBITSHIFT (uint32, nbits < 32 ? nbits : 32); |
4908
|
446 else if (cname == "uint64") |
4915
|
447 DO_UBITSHIFT (uint64, nbits < 64 ? nbits : 64); |
|
448 else if (cname == "int8") |
|
449 DO_SBITSHIFT (int8, nbits < 8 ? nbits : 8); |
|
450 else if (cname == "int16") |
|
451 DO_SBITSHIFT (int16, nbits < 16 ? nbits : 16); |
|
452 else if (cname == "int32") |
|
453 DO_SBITSHIFT (int32, nbits < 32 ? nbits : 32); |
|
454 else if (cname == "int64") |
|
455 DO_SBITSHIFT (int64, nbits < 64 ? nbits : 64); |
|
456 else if (cname == "double") |
|
457 { |
|
458 nbits = (nbits < 53 ? nbits : 53); |
|
459 EIGHT_BYTE_INT mask = 0x1FFFFFFFFFFFFFLL; |
|
460 if (nbits < 53) |
|
461 mask = mask >> (53 - nbits); |
|
462 else if (nbits < 1) |
|
463 mask = 0; |
|
464 int bits_in_type = 64; |
|
465 NDArray m = m_arg.array_value (); |
|
466 DO_BITSHIFT ( ); |
|
467 } |
4908
|
468 else |
|
469 error ("bitshift: not defined for %s objects", cname.c_str ()); |
|
470 } |
|
471 else |
|
472 print_usage ("bitshift"); |
|
473 |
|
474 return retval; |
|
475 } |
|
476 |
|
477 DEFUN (bitmax, args, , |
|
478 "-*- texinfo -*-\n\ |
4915
|
479 @deftypefn {Built-in Function} {} bitmax ()\n\ |
4920
|
480 Return the largest integer that can be represented as a floating point\n\ |
|
481 value. On IEEE-754 compatiable systems, @code{bitmax} is @code{2^53 - 1}.\n\ |
4915
|
482 @end deftypefn") |
|
483 { |
|
484 octave_value retval; |
4919
|
485 if (args.length () != 0) |
4915
|
486 print_usage ("bitmax"); |
|
487 else |
4919
|
488 retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL)); |
4915
|
489 return retval; |
|
490 } |
|
491 |
|
492 DEFUN (intmax, args, , |
|
493 "-*- texinfo -*-\n\ |
|
494 @deftypefn {Built-in Function} {} intmax (@var{type})\n\ |
5040
|
495 Return the largest integer that can be represented in an integer type.\n\ |
|
496 The variable @var{type} can be\n\ |
|
497 \n\ |
|
498 @table @code\n\ |
|
499 @item int8\n\ |
|
500 signed 8-bit integer.\n\ |
|
501 @item int16\n\ |
|
502 signed 16-bit integer.\n\ |
|
503 @item int32\n\ |
|
504 signed 32-bit integer.\n\ |
|
505 @item int64\n\ |
|
506 signed 64-bit integer.\n\ |
|
507 @item uint8\n\ |
|
508 unsigned 8-bit integer.\n\ |
|
509 @item uint16\n\ |
|
510 unsigned 16-bit integer.\n\ |
|
511 @item uint32\n\ |
|
512 unsigned 32-bit integer.\n\ |
|
513 @item uint64\n\ |
|
514 unsigned 64-bit integer.\n\ |
|
515 @end table\n\ |
|
516 \n\ |
|
517 The default for @var{type} is @code{uint32}.\n\ |
|
518 @seealso{intmin,bitmax}\n\ |
4908
|
519 @end deftypefn") |
|
520 { |
|
521 octave_value retval; |
4915
|
522 std::string cname = "int32"; |
|
523 int nargin = args.length (); |
|
524 |
4919
|
525 if (nargin == 1 && args(0).is_string ()) |
4915
|
526 cname = args(0).string_value (); |
|
527 else if (nargin != 0) |
|
528 { |
|
529 print_usage ("intmax"); |
|
530 return retval; |
|
531 } |
|
532 |
|
533 if (cname == "uint8") |
4919
|
534 retval = octave_uint8 (std::numeric_limits<octave_uint8_t>::max ()); |
4915
|
535 else if (cname == "uint16") |
4919
|
536 retval = octave_uint16 (std::numeric_limits<octave_uint16_t>::max ()); |
4915
|
537 else if (cname == "uint32") |
4919
|
538 retval = octave_uint32 (std::numeric_limits<octave_uint32_t>::max ()); |
4915
|
539 else if (cname == "uint64") |
4919
|
540 retval = octave_uint64 (std::numeric_limits<octave_uint64_t>::max ()); |
4915
|
541 else if (cname == "int8") |
4919
|
542 retval = octave_int8 (std::numeric_limits<octave_int8_t>::max ()); |
4915
|
543 else if (cname == "int16") |
4919
|
544 retval = octave_int16 (std::numeric_limits<octave_int16_t>::max ()); |
4915
|
545 else if (cname == "int32") |
4919
|
546 retval = octave_int32 (std::numeric_limits<octave_int32_t>::max ()); |
4915
|
547 else if (cname == "int64") |
4919
|
548 retval = octave_int64 (std::numeric_limits<octave_int64_t>::max ()); |
4915
|
549 else |
|
550 error ("intmax: not defined for '%s' objects", cname.c_str ()); |
|
551 |
|
552 return retval; |
|
553 } |
|
554 |
|
555 DEFUN (intmin, args, , |
|
556 "-*- texinfo -*-\n\ |
|
557 @deftypefn {Built-in Function} {} intmin (@var{type})\n\ |
5040
|
558 Return the smallest integer that can be represented in an integer type.\n\ |
|
559 The variable @var{type} can be\n\ |
|
560 \n\ |
|
561 @table @code\n\ |
|
562 @item int8\n\ |
|
563 signed 8-bit integer.\n\ |
|
564 @item int16\n\ |
|
565 signed 16-bit integer.\n\ |
|
566 @item int32\n\ |
|
567 signed 32-bit integer.\n\ |
|
568 @item int64\n\ |
|
569 signed 64-bit integer.\n\ |
|
570 @item uint8\n\ |
|
571 unsigned 8-bit integer.\n\ |
|
572 @item uint16\n\ |
|
573 unsigned 16-bit integer.\n\ |
|
574 @item uint32\n\ |
|
575 unsigned 32-bit integer.\n\ |
|
576 @item uint64\n\ |
|
577 unsigned 64-bit integer.\n\ |
|
578 @end table\n\ |
|
579 \n\ |
|
580 The default for @var{type} is @code{uint32}.\n\ |
|
581 @seealso{intmax,bitmax}\n\ |
4915
|
582 @end deftypefn") |
|
583 { |
|
584 octave_value retval; |
|
585 std::string cname = "int32"; |
|
586 int nargin = args.length (); |
|
587 |
4919
|
588 if (nargin == 1 && args(0).is_string ()) |
4915
|
589 cname = args(0).string_value (); |
|
590 else if (nargin != 0) |
|
591 { |
|
592 print_usage ("intmin"); |
|
593 return retval; |
|
594 } |
|
595 |
|
596 if (cname == "uint8") |
4919
|
597 retval = octave_uint8 (std::numeric_limits<octave_uint8_t>::min ()); |
4915
|
598 else if (cname == "uint16") |
|
599 retval = octave_uint16 (std::numeric_limits<octave_uint16_t>::min()); |
|
600 else if (cname == "uint32") |
4919
|
601 retval = octave_uint32 (std::numeric_limits<octave_uint32_t>::min ()); |
4915
|
602 else if (cname == "uint64") |
4919
|
603 retval = octave_uint64 (std::numeric_limits<octave_uint64_t>::min ()); |
4915
|
604 else if (cname == "int8") |
4919
|
605 retval = octave_int8 (std::numeric_limits<octave_int8_t>::min ()); |
4915
|
606 else if (cname == "int16") |
4919
|
607 retval = octave_int16 (std::numeric_limits<octave_int16_t>::min ()); |
4915
|
608 else if (cname == "int32") |
4919
|
609 retval = octave_int32 (std::numeric_limits<octave_int32_t>::min ()); |
4915
|
610 else if (cname == "int64") |
4919
|
611 retval = octave_int64 (std::numeric_limits<octave_int64_t>::min ()); |
4915
|
612 else |
|
613 error ("intmin: not defined for '%s' objects", cname.c_str ()); |
|
614 |
4908
|
615 return retval; |
|
616 } |
|
617 |
|
618 /* |
|
619 ;;; Local Variables: *** |
|
620 ;;; mode: C++ *** |
|
621 ;;; End: *** |
|
622 */ |