bitfcns.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2004-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "str-vec.h"
00028 #include "quit.h"
00029 
00030 #include "defun.h"
00031 #include "error.h"
00032 #include "ov.h"
00033 #include "ov-uint64.h"
00034 #include "ov-uint32.h"
00035 #include "ov-uint16.h"
00036 #include "ov-uint8.h"
00037 #include "ov-int64.h"
00038 #include "ov-int32.h"
00039 #include "ov-int16.h"
00040 #include "ov-int8.h"
00041 #include "ov-scalar.h"
00042 #include "ov-re-mat.h"
00043 #include "ov-bool.h"
00044 
00045 // FIXME -- could probably eliminate some code duplication by
00046 // clever use of templates.
00047 
00048 #define BITOPX(OP, FNAME, RET) \
00049       { \
00050         int nelx = x.numel (); \
00051         int nely = y.numel (); \
00052  \
00053         bool is_scalar_op = (nelx == 1 || nely == 1); \
00054  \
00055         dim_vector dvx = x.dims (); \
00056         dim_vector dvy = y.dims (); \
00057  \
00058         bool is_array_op = (dvx == dvy); \
00059  \
00060         if (is_array_op || is_scalar_op) \
00061           { \
00062             RET result; \
00063  \
00064             if (nelx != 1) \
00065               result.resize (dvx); \
00066             else \
00067               result.resize (dvy); \
00068  \
00069             for (int i = 0; i < nelx; i++) \
00070               if (is_scalar_op) \
00071                 for (int k = 0; k < nely; k++) \
00072                   result(i+k) = x(i) OP y(k); \
00073               else \
00074                 result(i) = x(i) OP y(i); \
00075  \
00076               retval = result; \
00077           } \
00078         else \
00079           error ("%s: size of X and Y must match, or one operand must be a scalar", FNAME); \
00080       }
00081 
00082 #define BITOP(OP, FNAME) \
00083  \
00084   octave_value retval; \
00085  \
00086   int nargin = args.length (); \
00087  \
00088   if (nargin == 2) \
00089     { \
00090       if ((args(0).class_name () == octave_scalar::static_class_name ()) \
00091           || (args(0).class_name () == octave_bool::static_class_name ()) \
00092           || (args(1).class_name () == octave_scalar::static_class_name ()) \
00093           || (args(1).class_name () == octave_bool::static_class_name ())) \
00094         { \
00095           bool arg0_is_int = (args(0).class_name () !=  \
00096                               octave_scalar::static_class_name () && \
00097                               args(0).class_name () != \
00098                               octave_bool::static_class_name ()); \
00099           bool arg1_is_int = (args(1).class_name () !=  \
00100                               octave_scalar::static_class_name () && \
00101                               args(1).class_name () != \
00102                               octave_bool::static_class_name ()); \
00103           \
00104           if (! (arg0_is_int || arg1_is_int))   \
00105             { \
00106               uint64NDArray x (args(0).array_value ()); \
00107               uint64NDArray y (args(1).array_value ()); \
00108               if (! error_state) \
00109                 BITOPX (OP, FNAME, uint64NDArray); \
00110               retval = retval.array_value (); \
00111             } \
00112           else \
00113             { \
00114               int p = (arg0_is_int ? 1 : 0); \
00115               int q = (arg0_is_int ? 0 : 1); \
00116  \
00117               NDArray dx = args(p).array_value (); \
00118  \
00119               if (args(q).type_id () == octave_uint64_matrix::static_type_id () \
00120                   || args(q).type_id () == octave_uint64_scalar::static_type_id ()) \
00121                 { \
00122                   uint64NDArray x (dx); \
00123                   uint64NDArray y = args(q).uint64_array_value (); \
00124                   if (! error_state) \
00125                     BITOPX (OP, FNAME, uint64NDArray); \
00126                  } \
00127               else if (args(q).type_id () == octave_uint32_matrix::static_type_id () \
00128                        || args(q).type_id () == octave_uint32_scalar::static_type_id ()) \
00129                 { \
00130                   uint32NDArray x (dx); \
00131                   uint32NDArray y = args(q).uint32_array_value (); \
00132                   if (! error_state) \
00133                     BITOPX (OP, FNAME, uint32NDArray); \
00134                 } \
00135               else if (args(q).type_id () == octave_uint16_matrix::static_type_id () \
00136                        || args(q).type_id () == octave_uint16_scalar::static_type_id ()) \
00137                 { \
00138                   uint16NDArray x (dx); \
00139                   uint16NDArray y = args(q).uint16_array_value (); \
00140                   if (! error_state) \
00141                     BITOPX (OP, FNAME, uint16NDArray); \
00142                 } \
00143               else if (args(q).type_id () == octave_uint8_matrix::static_type_id () \
00144                        || args(q).type_id () == octave_uint8_scalar::static_type_id ()) \
00145                 { \
00146                   uint8NDArray x (dx); \
00147                   uint8NDArray y = args(q).uint8_array_value (); \
00148                   if (! error_state) \
00149                     BITOPX (OP, FNAME, uint8NDArray); \
00150                 } \
00151               else if (args(q).type_id () == octave_int64_matrix::static_type_id () \
00152                        || args(q).type_id () == octave_int64_scalar::static_type_id ()) \
00153                 { \
00154                   int64NDArray x (dx); \
00155                   int64NDArray y = args(q).int64_array_value (); \
00156                   if (! error_state) \
00157                     BITOPX (OP, FNAME, int64NDArray); \
00158                 } \
00159               else if (args(q).type_id () == octave_int32_matrix::static_type_id () \
00160                        || args(q).type_id () == octave_int32_scalar::static_type_id ()) \
00161                 { \
00162                   int32NDArray x (dx); \
00163                   int32NDArray y = args(q).int32_array_value (); \
00164                   if (! error_state) \
00165                     BITOPX (OP, FNAME, int32NDArray); \
00166                 } \
00167               else if (args(q).type_id () == octave_int16_matrix::static_type_id () \
00168                        || args(q).type_id () == octave_int16_scalar::static_type_id ()) \
00169                 { \
00170                   int16NDArray x (dx); \
00171                   int16NDArray y = args(q).int16_array_value (); \
00172                   if (! error_state) \
00173                     BITOPX (OP, FNAME, int16NDArray); \
00174                 } \
00175               else if (args(q).type_id () == octave_int8_matrix::static_type_id () \
00176                        || args(q).type_id () == octave_int8_scalar::static_type_id ()) \
00177                 { \
00178                   int8NDArray x (dx); \
00179                   int8NDArray y = args(q).int8_array_value (); \
00180                   if (! error_state) \
00181                     BITOPX (OP, FNAME, int8NDArray); \
00182                 } \
00183               else \
00184                 error ("%s: invalid operand type", FNAME); \
00185             } \
00186         } \
00187       else if (args(0).class_name () == args(1).class_name ()) \
00188         { \
00189           if (args(0).type_id () == octave_uint64_matrix::static_type_id () \
00190               || args(0).type_id () == octave_uint64_scalar::static_type_id ()) \
00191             { \
00192               uint64NDArray x = args(0).uint64_array_value (); \
00193               uint64NDArray y = args(1).uint64_array_value (); \
00194               if (! error_state) \
00195                 BITOPX (OP, FNAME, uint64NDArray); \
00196             } \
00197           else if (args(0).type_id () == octave_uint32_matrix::static_type_id () \
00198                    || args(0).type_id () == octave_uint32_scalar::static_type_id ()) \
00199             { \
00200               uint32NDArray x = args(0).uint32_array_value (); \
00201               uint32NDArray y = args(1).uint32_array_value (); \
00202               if (! error_state) \
00203                 BITOPX (OP, FNAME, uint32NDArray); \
00204             } \
00205           else if (args(0).type_id () == octave_uint16_matrix::static_type_id () \
00206                    || args(0).type_id () == octave_uint16_scalar::static_type_id ()) \
00207             { \
00208               uint16NDArray x = args(0).uint16_array_value (); \
00209               uint16NDArray y = args(1).uint16_array_value (); \
00210               if (! error_state) \
00211                 BITOPX (OP, FNAME, uint16NDArray); \
00212             } \
00213           else if (args(0).type_id () == octave_uint8_matrix::static_type_id () \
00214                    || args(0).type_id () == octave_uint8_scalar::static_type_id ()) \
00215             { \
00216               uint8NDArray x = args(0).uint8_array_value (); \
00217               uint8NDArray y = args(1).uint8_array_value (); \
00218               if (! error_state) \
00219                 BITOPX (OP, FNAME, uint8NDArray); \
00220             } \
00221           else if (args(0).type_id () == octave_int64_matrix::static_type_id () \
00222                    || args(0).type_id () == octave_int64_scalar::static_type_id ()) \
00223             { \
00224               int64NDArray x = args(0).int64_array_value (); \
00225               int64NDArray y = args(1).int64_array_value (); \
00226               if (! error_state) \
00227                 BITOPX (OP, FNAME, int64NDArray); \
00228             } \
00229           else if (args(0).type_id () == octave_int32_matrix::static_type_id () \
00230                    || args(0).type_id () == octave_int32_scalar::static_type_id ()) \
00231             { \
00232               int32NDArray x = args(0).int32_array_value (); \
00233               int32NDArray y = args(1).int32_array_value (); \
00234               if (! error_state) \
00235                 BITOPX (OP, FNAME, int32NDArray); \
00236             } \
00237           else if (args(0).type_id () == octave_int16_matrix::static_type_id () \
00238                    || args(0).type_id () == octave_int16_scalar::static_type_id ()) \
00239             { \
00240               int16NDArray x = args(0).int16_array_value (); \
00241               int16NDArray y = args(1).int16_array_value (); \
00242               if (! error_state) \
00243                 BITOPX (OP, FNAME, int16NDArray); \
00244             } \
00245           else if (args(0).type_id () == octave_int8_matrix::static_type_id () \
00246                    || args(0).type_id () == octave_int8_scalar::static_type_id ()) \
00247             { \
00248               int8NDArray x = args(0).int8_array_value (); \
00249               int8NDArray y = args(1).int8_array_value (); \
00250               if (! error_state) \
00251                 BITOPX (OP, FNAME, int8NDArray); \
00252             } \
00253           else \
00254             error ("%s: invalid operand type", FNAME); \
00255         } \
00256       else \
00257         error ("%s: must have matching operand types", FNAME); \
00258     } \
00259   else \
00260     print_usage (); \
00261  \
00262   return retval
00263 
00264 DEFUN (bitand, args, ,
00265   "-*- texinfo -*-\n\
00266 @deftypefn {Built-in Function} {} bitand (@var{x}, @var{y})\n\
00267 Return the bitwise AND of non-negative integers.\n\
00268 @var{x}, @var{y} must be in the range [0,bitmax]\n\
00269 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
00270 @end deftypefn")
00271 {
00272   BITOP (&, "bitand");
00273 }
00274 
00275 DEFUN (bitor, args, ,
00276   "-*- texinfo -*-\n\
00277 @deftypefn {Built-in Function} {} bitor (@var{x}, @var{y})\n\
00278 Return the bitwise OR of non-negative integers.\n\
00279 @var{x}, @var{y} must be in the range [0,bitmax]\n\
00280 @seealso{bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
00281 @end deftypefn")
00282 {
00283   BITOP (|, "bitor");
00284 }
00285 
00286 DEFUN (bitxor, args, ,
00287   "-*- texinfo -*-\n\
00288 @deftypefn {Built-in Function} {} bitxor (@var{x}, @var{y})\n\
00289 Return the bitwise XOR of non-negative integers.\n\
00290 @var{x}, @var{y} must be in the range [0,bitmax]\n\
00291 @seealso{bitand, bitor, bitset, bitget, bitcmp, bitshift, bitmax}\n\
00292 @end deftypefn")
00293 {
00294   BITOP (^, "bitxor");
00295 }
00296 
00297 static int64_t
00298 bitshift (double a, int n, int64_t mask)
00299 {
00300   // In the name of bug-for-bug compatibility.
00301   if (a < 0)
00302     return -bitshift (-a, n, mask);
00303 
00304   if (n > 0)
00305     return (static_cast<int64_t> (a) << n) & mask;
00306   else if (n < 0)
00307     return (static_cast<int64_t> (a) >> -n) & mask;
00308   else
00309     return static_cast<int64_t> (a) & mask;
00310 }
00311 
00312 static int64_t
00313 bitshift (float a, int n, int64_t mask)
00314 {
00315   // In the name of bug-for-bug compatibility.
00316   if (a < 0)
00317     return -bitshift (-a, n, mask);
00318 
00319   if (n > 0)
00320     return (static_cast<int64_t> (a) << n) & mask;
00321   else if (n < 0)
00322     return (static_cast<int64_t> (a) >> -n) & mask;
00323   else
00324     return static_cast<int64_t> (a) & mask;
00325 }
00326 
00327 // Note that the bitshift operators are undefined if shifted by more
00328 // bits than in the type, so we need to test for the size of the
00329 // shift.
00330 
00331 #define DO_BITSHIFT(T) \
00332   if (! error_state) \
00333     { \
00334       double d1, d2; \
00335  \
00336       if (n.all_integers (d1, d2)) \
00337         { \
00338           int m_nel = m.numel (); \
00339           int n_nel = n.numel (); \
00340  \
00341           bool is_scalar_op = (m_nel == 1 || n_nel == 1); \
00342  \
00343           dim_vector m_dv = m.dims (); \
00344           dim_vector n_dv = n.dims (); \
00345  \
00346           bool is_array_op = (m_dv == n_dv); \
00347  \
00348           if (is_array_op || is_scalar_op) \
00349             { \
00350               T ## NDArray result; \
00351  \
00352               if (m_nel != 1) \
00353                 result.resize (m_dv); \
00354               else \
00355                 result.resize (n_dv); \
00356  \
00357               for (int i = 0; i < m_nel; i++) \
00358                 if (is_scalar_op) \
00359                   for (int k = 0; k < n_nel; k++) \
00360                     if (static_cast<int> (n(k)) >= bits_in_type) \
00361                       result(i+k) = 0; \
00362                     else \
00363                       result(i+k) = bitshift (m(i), static_cast<int> (n(k)), mask); \
00364                 else \
00365                   if (static_cast<int> (n(i)) >= bits_in_type) \
00366                     result(i) = 0;                                      \
00367                   else                                          \
00368                     result(i) = bitshift (m(i), static_cast<int> (n(i)), mask); \
00369  \
00370               retval = result; \
00371             } \
00372           else \
00373             error ("bitshift: size of A and N must match, or one operand must be a scalar"); \
00374         } \
00375       else \
00376         error ("bitshift: expecting integer as second argument"); \
00377     }
00378 
00379 #define DO_UBITSHIFT(T, N) \
00380   do \
00381     { \
00382       int bits_in_type = octave_ ## T :: nbits (); \
00383       T ## NDArray m = m_arg.T ## _array_value (); \
00384         octave_ ## T mask = octave_ ## T::max (); \
00385       if ((N) < bits_in_type) \
00386         mask = bitshift (mask, (N) - bits_in_type); \
00387       else if ((N) < 1) \
00388         mask = 0; \
00389       DO_BITSHIFT (T); \
00390     } \
00391   while (0)
00392 
00393 #define DO_SBITSHIFT(T, N) \
00394   do \
00395     { \
00396       int bits_in_type = octave_ ## T :: nbits (); \
00397       T ## NDArray m = m_arg.T ## _array_value (); \
00398         octave_ ## T mask = octave_ ## T::max (); \
00399       if ((N) < bits_in_type) \
00400         mask = bitshift (mask, (N) - bits_in_type); \
00401       else if ((N) < 1) \
00402         mask = 0; \
00403       mask = mask | octave_ ## T :: min (); /* FIXME: 2's complement only? */ \
00404       DO_BITSHIFT (T); \
00405     } \
00406   while (0)
00407 
00408 DEFUN (bitshift, args, ,
00409   "-*- texinfo -*-\n\
00410 @deftypefn  {Built-in Function} {} bitshift (@var{a}, @var{k})\n\
00411 @deftypefnx {Built-in Function} {} bitshift (@var{a}, @var{k}, @var{n})\n\
00412 Return a @var{k} bit shift of @var{n}-digit unsigned\n\
00413 integers in @var{a}.  A positive @var{k} leads to a left shift;\n\
00414 A negative value to a right shift.  If @var{n} is omitted it defaults\n\
00415 to log2(bitmax)+1.\n\
00416 @var{n} must be in the range [1,log2(bitmax)+1] usually [1,33].\n\
00417 \n\
00418 @example\n\
00419 @group\n\
00420 bitshift (eye (3), 1)\n\
00421 @result{}\n\
00422 @group\n\
00423 2 0 0\n\
00424 0 2 0\n\
00425 0 0 2\n\
00426 @end group\n\
00427 \n\
00428 bitshift (10, [-2, -1, 0, 1, 2])\n\
00429 @result{} 2   5  10  20  40\n\
00430 @c FIXME -- restore this example when third arg is allowed to be an array.\n\
00431 @c\n\
00432 @c\n\
00433 @c bitshift ([1, 10], 2, [3,4])\n\
00434 @c @result{} 4  8\n\
00435 @end group\n\
00436 @end example\n\
00437 @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitmax}\n\
00438 @end deftypefn")
00439 {
00440   octave_value retval;
00441 
00442   int nargin = args.length ();
00443 
00444   if (nargin == 2 || nargin == 3)
00445     {
00446       int nbits = 64;
00447 
00448       NDArray n = args(1).array_value ();
00449 
00450       if (error_state)
00451         error ("bitshift: expecting integer as second argument");
00452       else
00453         {
00454           if (nargin == 3)
00455             {
00456               // FIXME -- for compatibility, we should accept an array
00457               // or a scalar as the third argument.
00458               if (args(2).numel () > 1)
00459                 error ("bitshift: N must be a scalar integer");
00460               else
00461                 {
00462                   nbits = args(2).int_value ();
00463 
00464                   if (error_state)
00465                     error ("bitshift: N must be an integer");
00466                   else if (nbits < 0)
00467                     error ("bitshift: N must be positive");
00468                 }
00469             }
00470         }
00471 
00472       if (error_state)
00473         return retval;
00474 
00475       octave_value m_arg = args(0);
00476       std::string cname = m_arg.class_name ();
00477 
00478       if (cname == "uint8")
00479         DO_UBITSHIFT (uint8, nbits < 8 ? nbits : 8);
00480       else if (cname == "uint16")
00481         DO_UBITSHIFT (uint16, nbits < 16 ? nbits : 16);
00482       else if (cname == "uint32")
00483         DO_UBITSHIFT (uint32, nbits < 32 ? nbits : 32);
00484       else if (cname == "uint64")
00485         DO_UBITSHIFT (uint64, nbits < 64 ? nbits : 64);
00486       else if (cname == "int8")
00487         DO_SBITSHIFT (int8, nbits < 8 ? nbits : 8);
00488       else if (cname == "int16")
00489         DO_SBITSHIFT (int16, nbits < 16 ? nbits : 16);
00490       else if (cname == "int32")
00491         DO_SBITSHIFT (int32, nbits < 32 ? nbits : 32);
00492       else if (cname == "int64")
00493         DO_SBITSHIFT (int64, nbits < 64 ? nbits : 64);
00494       else if (cname == "double")
00495         {
00496           nbits = (nbits < 53 ? nbits : 53);
00497           int64_t mask = 0x1FFFFFFFFFFFFFLL;
00498           if (nbits < 53)
00499             mask = mask >> (53 - nbits);
00500           else if (nbits < 1)
00501             mask = 0;
00502           int bits_in_type = 64;
00503           NDArray m = m_arg.array_value ();
00504           DO_BITSHIFT ( );
00505         }
00506       else
00507         error ("bitshift: not defined for %s objects", cname.c_str ());
00508     }
00509   else
00510     print_usage ();
00511 
00512   return retval;
00513 }
00514 
00515 DEFUN (bitmax, args, ,
00516   "-*- texinfo -*-\n\
00517 @deftypefn  {Built-in Function} {} bitmax ()\n\
00518 @deftypefnx {Built-in Function} {} bitmax (\"double\")\n\
00519 @deftypefnx {Built-in Function} {} bitmax (\"single\")\n\
00520 Return the largest integer that can be represented within a floating point\n\
00521 value.  The default class is \"double\", but \"single\" is a valid option.\n\
00522 On IEEE-754 compatible systems, @code{bitmax} is @w{@math{2^{53} - 1}}.\n\
00523 @end deftypefn")
00524 {
00525   octave_value retval;
00526   std::string cname = "double";
00527   int nargin = args.length ();
00528 
00529   if (nargin == 1 && args(0).is_string ())
00530     cname = args(0).string_value ();
00531   else if (nargin != 0)
00532     {
00533       print_usage ();
00534       return retval;
00535     }
00536 
00537   if (cname == "double")
00538     retval = (static_cast<double> (0x1FFFFFFFFFFFFFLL));
00539   else if (cname == "single")
00540     retval = (static_cast<double> (0xFFFFFFL));
00541   else
00542     error ("bitmax: not defined for class '%s'", cname.c_str ());
00543 
00544   return retval;
00545 }
00546 
00547 DEFUN (intmax, args, ,
00548   "-*- texinfo -*-\n\
00549 @deftypefn {Built-in Function} {} intmax (@var{type})\n\
00550 Return the largest integer that can be represented in an integer type.\n\
00551 The variable @var{type} can be\n\
00552 \n\
00553 @table @code\n\
00554 @item int8\n\
00555 signed 8-bit integer.\n\
00556 \n\
00557 @item int16\n\
00558 signed 16-bit integer.\n\
00559 \n\
00560 @item int32\n\
00561 signed 32-bit integer.\n\
00562 \n\
00563 @item int64\n\
00564 signed 64-bit integer.\n\
00565 \n\
00566 @item uint8\n\
00567 unsigned 8-bit integer.\n\
00568 \n\
00569 @item uint16\n\
00570 unsigned 16-bit integer.\n\
00571 \n\
00572 @item uint32\n\
00573 unsigned 32-bit integer.\n\
00574 \n\
00575 @item uint64\n\
00576 unsigned 64-bit integer.\n\
00577 @end table\n\
00578 \n\
00579 The default for @var{type} is @code{uint32}.\n\
00580 @seealso{intmin, bitmax}\n\
00581 @end deftypefn")
00582 {
00583   octave_value retval;
00584   std::string cname = "int32";
00585   int nargin = args.length ();
00586 
00587   if (nargin == 1 && args(0).is_string ())
00588     cname = args(0).string_value ();
00589   else if (nargin != 0)
00590     {
00591       print_usage ();
00592       return retval;
00593     }
00594 
00595   if (cname == "uint8")
00596     retval = octave_uint8 (std::numeric_limits<uint8_t>::max ());
00597   else if (cname == "uint16")
00598     retval = octave_uint16 (std::numeric_limits<uint16_t>::max ());
00599   else if (cname == "uint32")
00600     retval = octave_uint32 (std::numeric_limits<uint32_t>::max ());
00601   else if (cname == "uint64")
00602     retval = octave_uint64 (std::numeric_limits<uint64_t>::max ());
00603   else if (cname == "int8")
00604     retval = octave_int8 (std::numeric_limits<int8_t>::max ());
00605   else if (cname == "int16")
00606     retval = octave_int16 (std::numeric_limits<int16_t>::max ());
00607   else if (cname == "int32")
00608     retval = octave_int32 (std::numeric_limits<int32_t>::max ());
00609   else if (cname == "int64")
00610     retval = octave_int64 (std::numeric_limits<int64_t>::max ());
00611   else
00612     error ("intmax: not defined for '%s' objects", cname.c_str ());
00613 
00614   return retval;
00615 }
00616 
00617 DEFUN (intmin, args, ,
00618   "-*- texinfo -*-\n\
00619 @deftypefn {Built-in Function} {} intmin (@var{type})\n\
00620 Return the smallest integer that can be represented in an integer type.\n\
00621 The variable @var{type} can be\n\
00622 \n\
00623 @table @code\n\
00624 @item int8\n\
00625 signed 8-bit integer.\n\
00626 \n\
00627 @item int16\n\
00628 signed 16-bit integer.\n\
00629 \n\
00630 @item int32\n\
00631 signed 32-bit integer.\n\
00632 \n\
00633 @item int64\n\
00634 signed 64-bit integer.\n\
00635 \n\
00636 @item uint8\n\
00637 unsigned 8-bit integer.\n\
00638 \n\
00639 @item uint16\n\
00640 unsigned 16-bit integer.\n\
00641 \n\
00642 @item uint32\n\
00643 unsigned 32-bit integer.\n\
00644 \n\
00645 @item uint64\n\
00646 unsigned 64-bit integer.\n\
00647 @end table\n\
00648 \n\
00649 The default for @var{type} is @code{uint32}.\n\
00650 @seealso{intmax, bitmax}\n\
00651 @end deftypefn")
00652 {
00653   octave_value retval;
00654   std::string cname = "int32";
00655   int nargin = args.length ();
00656 
00657   if (nargin == 1 && args(0).is_string ())
00658     cname = args(0).string_value ();
00659   else if (nargin != 0)
00660     {
00661       print_usage ();
00662       return retval;
00663     }
00664 
00665   if (cname == "uint8")
00666     retval = octave_uint8 (std::numeric_limits<uint8_t>::min ());
00667   else if (cname == "uint16")
00668     retval = octave_uint16 (std::numeric_limits<uint16_t>::min());
00669   else if (cname == "uint32")
00670     retval = octave_uint32 (std::numeric_limits<uint32_t>::min ());
00671   else if (cname == "uint64")
00672     retval = octave_uint64 (std::numeric_limits<uint64_t>::min ());
00673   else if (cname == "int8")
00674     retval = octave_int8 (std::numeric_limits<int8_t>::min ());
00675   else if (cname == "int16")
00676     retval = octave_int16 (std::numeric_limits<int16_t>::min ());
00677   else if (cname == "int32")
00678     retval = octave_int32 (std::numeric_limits<int32_t>::min ());
00679   else if (cname == "int64")
00680     retval = octave_int64 (std::numeric_limits<int64_t>::min ());
00681   else
00682     error ("intmin: not defined for '%s' objects", cname.c_str ());
00683 
00684   return retval;
00685 }
00686 
00687 DEFUN (sizemax, args, ,
00688   "-*- texinfo -*-\n\
00689 @deftypefn {Built-in Function} {} sizemax ()\n\
00690 Return the largest value allowed for the size of an array.\n\
00691 If Octave is compiled with 64-bit indexing, the result is of class int64,\n\
00692 otherwise it is of class int32.  The maximum array size is slightly\n\
00693 smaller than the maximum value allowable for the relevant class as reported\n\
00694 by @code{intmax}.\n\
00695 @seealso{intmax}\n\
00696 @end deftypefn")
00697 {
00698   octave_value retval;
00699 
00700   if (args.length () == 0)
00701     retval = octave_int<octave_idx_type> (dim_vector::dim_max ());
00702   else
00703     print_usage ();
00704 
00705   return retval;
00706 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines