41 if (old_dims.
length () == 2 && old_dims(0) == 1)
43 else if (old_dims.
length () == 2 && old_dims (0) == 0 && old_dims (1) == 0)
49 template <
class ArrayType>
61 data =
reinterpret_cast<const void *
> (array.data ());
62 byte_size = array.byte_size ();
64 old_dims = array.dims ();
67 template <
class ArrayType>
72 typedef typename ArrayType::element_type T;
75 if (n * static_cast<int> (
sizeof (T)) == byte_size)
78 T *dest = retval.fortran_vec ();
79 std::memcpy (dest, data, n *
sizeof (T));
85 error (
"typecast: incorrect number of input values to make output value");
91 DEFUN (typecast, args, ,
93 @deftypefn {Built-in Function} {@var{y} =} typecast (@var{x}, \"@var{class}\")\n\
94 Return a new array @var{y} resulting from interpreting the data of @var{x}\n\
95 in memory as data of the numeric class @var{class}.\n\
97 Both the class of @var{x} and @var{class} must be one of the built-in\n\
114 \"double complex\"\n\
115 \"single complex\"\n\
120 the last two are only used with @var{class}; they indicate that a\n\
121 complex-valued result is requested. Complex arrays are stored in memory as\n\
122 consecutive pairs of real numbers. The sizes of integer types are given by\n\
123 their bit counts. Both logical and char are typically one byte wide;\n\
124 however, this is not guaranteed by C++. If your system is IEEE conformant,\n\
125 single and double will be 4 bytes and 8 bytes wide, respectively.\n\
126 @qcode{\"logical\"} is not allowed for @var{class}.\n\
128 If the input is a row vector, the return value is a row vector, otherwise it\n\
129 is a column vector.\n\
131 If the bit length of @var{x} is not divisible by that of @var{class}, an\n\
134 An example of the use of typecast on a little-endian machine is\n\
138 @var{x} = uint16 ([1, 65535]);\n\
139 typecast (@var{x}, \"uint8\")\n\
140 @result{} [ 1, 0, 255, 255]\n\
143 @seealso{cast, bitpack, bitunpack, swapbytes}\n\
148 if (args.length () == 2)
151 const void *data = 0;
196 byte_size, old_dims, frame);
199 byte_size, old_dims, frame);
210 error (
"typecast: invalid input class: %s",
213 std::string numclass = args(1).string_value ();
217 else if (numclass ==
"char")
219 (data, byte_size, old_dims), array.
is_dq_string () ?
'"'
221 else if (numclass[0] ==
'i')
223 if (numclass ==
"int8")
224 retval = reinterpret_copy<int8NDArray> (data, byte_size, old_dims);
225 else if (numclass ==
"int16")
226 retval = reinterpret_copy<int16NDArray> (data, byte_size, old_dims);
227 else if (numclass ==
"int32")
228 retval = reinterpret_copy<int32NDArray> (data, byte_size, old_dims);
229 else if (numclass ==
"int64")
230 retval = reinterpret_copy<int64NDArray> (data, byte_size, old_dims);
232 else if (numclass[0] ==
'u')
234 if (numclass ==
"uint8")
235 retval = reinterpret_copy<uint8NDArray> (data, byte_size, old_dims);
236 else if (numclass ==
"uint16")
237 retval = reinterpret_copy<uint16NDArray> (data, byte_size,
239 else if (numclass ==
"uint32")
240 retval = reinterpret_copy<uint32NDArray> (data, byte_size,
242 else if (numclass ==
"uint64")
243 retval = reinterpret_copy<uint64NDArray> (data, byte_size,
246 else if (numclass ==
"single")
247 retval = reinterpret_copy<FloatNDArray> (data, byte_size, old_dims);
248 else if (numclass ==
"double")
249 retval = reinterpret_copy<NDArray> (data, byte_size, old_dims);
250 else if (numclass ==
"single complex")
251 retval = reinterpret_copy<FloatComplexNDArray> (data, byte_size,
253 else if (numclass ==
"double complex")
254 retval = reinterpret_copy<ComplexNDArray> (data, byte_size, old_dims);
257 error (
"typecast: cannot convert to %s class", numclass.c_str ());
265 template <
class ArrayType>
269 typedef typename ArrayType::element_type T;
271 = bitp.
numel () / (
sizeof (T) * std::numeric_limits<unsigned char>::digits);
273 if (n * static_cast<int> (
sizeof (T)) * std::numeric_limits<unsigned char>::digits == bitp.
numel ())
279 char *packed =
reinterpret_cast<char *
> (retval.fortran_vec ());
286 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
290 bits += std::numeric_limits<unsigned char>::digits;
297 error (
"bitpack: incorrect number of bits to make up output value");
302 DEFUN (bitpack, args, ,
304 @deftypefn {Built-in Function} {@var{y} =} bitpack (@var{x}, @var{class})\n\
305 Return a new array @var{y} resulting from interpreting the logical array\n\
306 @var{x} as raw bit patterns for data of the numeric class @var{class}.\n\
308 @var{class} must be one of the built-in numeric classes:\n\
314 \"double complex\"\n\
315 \"single complex\"\n\
328 The number of elements of @var{x} should be divisible by the bit length of\n\
329 @var{class}. If it is not, excess bits are discarded. Bits come in\n\
330 increasing order of significance, i.e., @code{x(1)} is bit 0, @code{x(2)} is\n\
333 The result is a row vector if @var{x} is a row vector, otherwise it is a\n\
335 @seealso{bitunpack, typecast}\n\
340 if (args.length () != 2)
342 else if (! args(0).is_bool_type ())
343 error (
"bitpack: X must be a logical array");
348 std::string numclass = args(1).string_value ();
352 else if (numclass ==
"char")
353 retval =
octave_value (do_bitpack<charNDArray> (bitp),
'\'');
354 else if (numclass[0] ==
'i')
356 if (numclass ==
"int8")
357 retval = do_bitpack<int8NDArray> (bitp);
358 else if (numclass ==
"int16")
359 retval = do_bitpack<int16NDArray> (bitp);
360 else if (numclass ==
"int32")
361 retval = do_bitpack<int32NDArray> (bitp);
362 else if (numclass ==
"int64")
363 retval = do_bitpack<int64NDArray> (bitp);
365 else if (numclass[0] ==
'u')
367 if (numclass ==
"uint8")
368 retval = do_bitpack<uint8NDArray> (bitp);
369 else if (numclass ==
"uint16")
370 retval = do_bitpack<uint16NDArray> (bitp);
371 else if (numclass ==
"uint32")
372 retval = do_bitpack<uint32NDArray> (bitp);
373 else if (numclass ==
"uint64")
374 retval = do_bitpack<uint64NDArray> (bitp);
376 else if (numclass ==
"single")
377 retval = do_bitpack<FloatNDArray> (bitp);
378 else if (numclass ==
"double")
379 retval = do_bitpack<NDArray> (bitp);
380 else if (numclass ==
"single complex")
381 retval = do_bitpack<FloatComplexNDArray> (bitp);
382 else if (numclass ==
"double complex")
383 retval = do_bitpack<ComplexNDArray> (bitp);
386 error (
"bitpack: cannot pack to %s class", numclass.c_str ());
392 template <
class ArrayType>
396 typedef typename ArrayType::element_type T;
398 * std::numeric_limits<unsigned char>::digits;
402 const char *packed =
reinterpret_cast<const char *
> (array.fortran_vec ());
411 for (
int j = 1; j < std::numeric_limits<unsigned char>::digits; j++)
412 bits[j] = (c >>= 1) & 1;
413 bits += std::numeric_limits<unsigned char>::digits;
419 DEFUN (bitunpack, args, ,
421 @deftypefn {Built-in Function} {@var{y} =} bitunpack (@var{x})\n\
422 Return a logical array @var{y} corresponding to the raw bit patterns of\n\
425 @var{x} must belong to one of the built-in numeric classes:\n\
443 The result is a row vector if @var{x} is a row vector; otherwise, it is a\n\
445 @seealso{bitpack, typecast}\n\
450 if (args.length () == 1
451 && (args(0).is_numeric_type () || args(0).is_string ()))
493 error (
"bitunpack: invalid input class: %s",
uint8NDArray uint8_array_value(void) const
ComplexNDArray complex_array_value(bool frc_str_conv=false) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool is_real_type(void) const
int8NDArray int8_array_value(void) const
bool is_uint16_type(void) const
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
int16NDArray int16_array_value(void) const
uint64NDArray uint64_array_value(void) const
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
int32NDArray int32_array_value(void) const
bool is_int8_type(void) const
bool is_int32_type(void) const
boolNDArray bool_array_value(bool warn=false) const
ArrayType do_bitpack(const boolNDArray &bitp)
int64NDArray int64_array_value(void) const
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
FloatNDArray float_array_value(bool frc_str_conv=false) const
bool is_bool_type(void) const
FloatComplexNDArray float_complex_array_value(bool frc_str_conv=false) const
bool is_string(void) const
bool is_complex_type(void) const
bool is_int64_type(void) const
bool is_int16_type(void) const
static void get_data_and_bytesize(const ArrayType &array, const void *&data, octave_idx_type &byte_size, dim_vector &old_dims, unwind_protect &frame)
NDArray array_value(bool frc_str_conv=false) const
static ArrayType reinterpret_copy(const void *data, octave_idx_type byte_size, const dim_vector &old_dims)
bool is_dq_string(void) const
boolNDArray do_bitunpack(const ArrayType &array)
bool is_uint8_type(void) const
std::string class_name(void) const
static dim_vector get_vec_dims(const dim_vector &old_dims, octave_idx_type n)
bool is_undefined(void) const
bool is_uint64_type(void) const
const T * fortran_vec(void) const
bool is_single_type(void) const
bool is_uint32_type(void) const
uint32NDArray uint32_array_value(void) const
uint16NDArray uint16_array_value(void) const
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
bool is_integer_type(void) const