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