Mercurial > hg > octave-nkf
changeset 4952:bfd57b466752
[project @ 2004-09-01 00:49:05 by jwe]
author | jwe |
---|---|
date | Wed, 01 Sep 2004 00:49:06 +0000 |
parents | 0a21e1bf18c4 |
children | 7a3a480e8645 |
files | liboctave/ChangeLog liboctave/int16NDArray.cc liboctave/int32NDArray.cc liboctave/int8NDArray.cc liboctave/oct-inttypes.h liboctave/uint16NDArray.cc liboctave/uint32NDArray.cc liboctave/uint8NDArray.cc src/ChangeLog src/OPERATORS/op-int-conv.cc src/OPERATORS/op-int.h src/bitfcns.cc src/ov-type-conv.h |
diffstat | 13 files changed, 217 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,24 @@ +2004-08-31 John W. Eaton <jwe@octave.org> + + * oct-inttypes.h (pow): Args now const reference. + (octave_int<T>::operator *=, octave_int<T>::operator /=, + octave_int<T>::operator <<=, octave_int<T>::operator >>=): + New member functions. + (OCTAVE_INT_BITSHIFT_OP): Delete macro. + (operator >> (const octave_int<T1>& x, const T2& y)): + Define in terms of >>=. + (operator << (const octave_int<T1>& x, const T2& y)): + Define in terms of <<=. + (bitshift): Operate on octave_int<T> objects, not the values, so + we get proper saturation properties. + +2004-08-31 David Bateman <dbateman@free.fr> + + * oct-inttypes.h (pow (constT, T)): New template. + + * int8NDArray.cc, int16NDArray.cc, int32NDArray.cc, uint8NDArray.cc, + uint16NDArray.cc, uint32NDArray.cc: Instantiate power function. + 2004-08-31 John W. Eaton <jwe@octave.org> * oct-inttypes.h (octave_int::byte_size): New function.
--- a/liboctave/int16NDArray.cc +++ b/liboctave/int16NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_int16>; +template octave_int16 pow (const octave_int16& a, const octave_int16& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_int16>& a);
--- a/liboctave/int32NDArray.cc +++ b/liboctave/int32NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_int32>; +template octave_int32 pow (const octave_int32& a, const octave_int32& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_int32>& a);
--- a/liboctave/int8NDArray.cc +++ b/liboctave/int8NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_int8>; +template octave_int8 pow (const octave_int8& a, const octave_int8& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_int8>& a);
--- a/liboctave/oct-inttypes.h +++ b/liboctave/oct-inttypes.h @@ -267,6 +267,36 @@ return *this; } + octave_int<T>& operator *= (const octave_int<T>& x) + { + double t = static_cast<double> (value ()); + double tx = static_cast<double> (x.value ()); + ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); + return *this; + } + + octave_int<T>& operator /= (const octave_int<T>& x) + { + double t = static_cast<double> (value ()); + double tx = static_cast<double> (x.value ()); + ival = OCTAVE_INT_FIT_TO_RANGE (t / tx, T); + return *this; + } + + template <class T2> + octave_int<T>& operator <<= (const T2& x) + { + ival = ((ival << x) > std::numeric_limits<T>::max ()) ? 0 : (ival << x); + return *this; + } + + template <class T2> + octave_int<T>& operator >>= (const T2& x) + { + ival >>= x; + return *this; + } + octave_int<T> min (void) const { return std::numeric_limits<T>::min (); } octave_int<T> max (void) const { return std::numeric_limits<T>::max (); } @@ -280,6 +310,43 @@ }; template <class T> +T +pow (const T& a, const T& b) +{ + T retval; + + T zero = T (0); + T one = T (1); + + if (b == zero) + retval = one; + else if (b < zero) + retval = zero; + else + { + T a_val = a; + T b_val = b; + + retval = a; + + b_val -= 1; + + while (b_val) + { + if (b_val & one) + retval = retval * a_val; + + b_val = b_val >> 1; + + if (b_val > zero) + a_val = a_val * a_val; + } + } + + return retval; +} + +template <class T> std::ostream& operator << (std::ostream& os, const octave_int<T>& ival) { @@ -337,17 +404,21 @@ OCTAVE_INT_BITCMP_OP (|) OCTAVE_INT_BITCMP_OP (^) -#define OCTAVE_INT_BITSHIFT_OP(OP) \ - \ - template <class T1, class T2> \ - octave_int<T1> \ - operator OP (const octave_int<T1>& x, const T2& y) \ - { \ - return ((x.value () OP y) > std::numeric_limits<T1>::max ()) ? 0 : (x.value () OP y); \ - } +template <class T1, class T2> +octave_int<T1> +operator << (const octave_int<T1>& x, const T2& y) +{ + T1 retval = x; + return retval <<= y; +} -OCTAVE_INT_BITSHIFT_OP (<<) -OCTAVE_INT_BITSHIFT_OP (>>) +template <class T1, class T2> +octave_int<T1> +operator >> (const octave_int<T1>& x, const T2& y) +{ + T1 retval = x; + return retval >>= y; +} template <class T> octave_int<T> @@ -355,9 +426,9 @@ const octave_int<T>& mask = std::numeric_limits<T>::max ()) { if (n > 0) - return (a.value () << n) & mask.value (); + return (a << n) & mask; else if (n < 0) - return (a.value () >> -n) & mask.value (); + return (a >> -n) & mask; else return a; }
--- a/liboctave/uint16NDArray.cc +++ b/liboctave/uint16NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_uint16>; +template octave_uint16 pow (const octave_uint16& a, const octave_uint16& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_uint16>& a);
--- a/liboctave/uint32NDArray.cc +++ b/liboctave/uint32NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_uint32>; +template octave_uint32 pow (const octave_uint32& a, const octave_uint32& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_uint32>& a);
--- a/liboctave/uint8NDArray.cc +++ b/liboctave/uint8NDArray.cc @@ -35,6 +35,8 @@ template class intNDArray<octave_uint8>; +template octave_uint8 pow (const octave_uint8& a, const octave_uint8& b); + template std::ostream& operator << (std::ostream& os, const intNDArray<octave_uint8>& a);
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,26 @@ +2004-08-31 David Bateman <dbateman@free.fr> + + * OPERATORS/op-int.h (ss_pow, ms_el_pow, mm_el_pow): New power + functions. + (sm_el_pow): Return correct type. + (OCTAVE_SS_POW_OPS, OCTAVE_SM_POW_OPS, OCTAVE_MS_POW_OPS, + OCTAVE_MM_POW_OPS): New macros to define the power operator over + integer types. + (OCTAVE_SS_INT_OPS, OCTAVE_SM_INT_OPS, OCTAVE_MS_INT_OPS, + OCTAVE_MM_INT_OPS): Use the new macros for the power operators. + (OCTAVE_INSTALL_SS_INT_OPS, OCTAVE_INSTALL_MS_INT_OPS, + OCTAVE_INSTALL_MM_INT_OPS): Install new power functions. + + * OPERATORS/op-int-conv.cc (INT_CONV_FUNCTIONS): Correct return + types for unsigned integer scalars and signed integer matrices. + + * ov-type-conv.h (OCTAVE_TYPE_CONV_BODY3): Conversion betwen same + types returns the original value. + + * bitfcns.cc (BITOP): Fix for mixed scalar/matrix ops. Allow + octave_range as an argument, by using class_name rather than + type_id to test. + 2004-08-31 John W. Eaton <jwe@octave.org> * pr-output.cc (pr_int): Also handle bank_format.
--- a/src/OPERATORS/op-int-conv.cc +++ b/src/OPERATORS/op-int-conv.cc @@ -83,15 +83,15 @@ DEFCONVFN2 (tfrom ## _scalar_to_int32, tfrom, scalar, int32) \ DEFCONVFN2 (tfrom ## _scalar_to_int64, tfrom, scalar, int64) \ \ - DEFCONVFN2 (tfrom ## _scalar_to_uint8, tfrom, scalar, int8) \ - DEFCONVFN2 (tfrom ## _scalar_to_uint16, tfrom, scalar, int16) \ - DEFCONVFN2 (tfrom ## _scalar_to_uint32, tfrom, scalar, int32) \ - DEFCONVFN2 (tfrom ## _scalar_to_uint64, tfrom, scalar, int64) \ + DEFCONVFN2 (tfrom ## _scalar_to_uint8, tfrom, scalar, uint8) \ + DEFCONVFN2 (tfrom ## _scalar_to_uint16, tfrom, scalar, uint16) \ + DEFCONVFN2 (tfrom ## _scalar_to_uint32, tfrom, scalar, uint32) \ + DEFCONVFN2 (tfrom ## _scalar_to_uint64, tfrom, scalar, uint64) \ \ - DEFCONVFN2 (tfrom ## _matrix_to_int8, tfrom, matrix, uint8) \ - DEFCONVFN2 (tfrom ## _matrix_to_int16, tfrom, matrix, uint16) \ - DEFCONVFN2 (tfrom ## _matrix_to_int32, tfrom, matrix, uint32) \ - DEFCONVFN2 (tfrom ## _matrix_to_int64, tfrom, matrix, uint64) \ + DEFCONVFN2 (tfrom ## _matrix_to_int8, tfrom, matrix, int8) \ + DEFCONVFN2 (tfrom ## _matrix_to_int16, tfrom, matrix, int16) \ + DEFCONVFN2 (tfrom ## _matrix_to_int32, tfrom, matrix, int32) \ + DEFCONVFN2 (tfrom ## _matrix_to_int64, tfrom, matrix, int64) \ \ DEFCONVFN2 (tfrom ## _matrix_to_uint8, tfrom, matrix, uint8) \ DEFCONVFN2 (tfrom ## _matrix_to_uint16, tfrom, matrix, uint16) \
--- a/src/OPERATORS/op-int.h +++ b/src/OPERATORS/op-int.h @@ -20,6 +20,8 @@ */ +#include "quit.h" + #define OCTAVE_CONCAT_FN(TYPE) \ DEFNDCATOP_FN (TYPE ## _s_s, TYPE ## _scalar, TYPE ## _scalar, TYPE ## _array, TYPE ## _array, concat) \ DEFNDCATOP_FN (TYPE ## _s_m, TYPE ## _scalar, TYPE ## _matrix, TYPE ## _array, TYPE ## _array, concat) \ @@ -62,7 +64,7 @@ return octave_value (v1.T1 ## _scalar_value () / d); \ } \ \ - /* DEFBINOP_FN (ss_pow, T1 ## _scalar, T2 ## _scalar, xpow) */ \ + DEFBINOP_FN (ss_pow, T1 ## _scalar, T2 ## _scalar, xpow) \ \ DEFBINOP (ss_ldiv, T1 ## _scalar, T2 ## _scalar) \ { \ @@ -116,8 +118,12 @@ DEFBINOP_OP (ss_gt, T1 ## _scalar, T2 ## _scalar, >) \ DEFBINOP_OP (ss_ne, T1 ## _scalar, T2 ## _scalar, !=) +#define OCTAVE_SS_POW_OPS(T1, T2) \ + octave_value xpow (octave_ ## T1 a, octave_ ## T2 b) {return pow (a, b);} + #define OCTAVE_SS_INT_OPS(TYPE) \ OCTAVE_S_INT_UNOPS (TYPE) \ + OCTAVE_SS_POW_OPS (TYPE, TYPE) \ OCTAVE_SS_INT_ARITH_OPS (TYPE, TYPE) \ OCTAVE_SS_INT_CMP_OPS (TYPE, TYPE) \ OCTAVE_SS_INT_BOOL_OPS (TYPE, TYPE) @@ -160,15 +166,7 @@ \ DEFNDBINOP_OP (sm_el_mul, TS ## _scalar, TM ## _matrix, TS ## _scalar, TM ## _array, *) \ /* DEFNDBINOP_FN (sm_el_div, TS ## _scalar, TM ## _matrix, TS ## _scalar, TM ## _array, x_el_div) */ \ - DEFBINOP (sm_el_pow, TS ## _scalar, TM ## _matrix) \ - { \ - CAST_BINOP_ARGS (const octave_ ## TS ## _scalar&, const octave_ ## TM ## _matrix&); \ - \ - double d = v1.TS ## _scalar_value (); \ - \ - /* XXX FIXME XXX Return type wrong */ \ - return octave_value (elem_xpow (d, v2.array_value())); \ - } \ + DEFNDBINOP_FN (sm_el_pow, TS ## _scalar, TM ## _matrix, TS ## _scalar, TM ## _array, elem_xpow) \ \ /* DEFBINOP (sm_el_ldiv, TS ## _scalar, TM ## _matrix) */ \ /* { */ \ @@ -194,7 +192,20 @@ /* DEFNDBINOP_FN (sm_el_and, TS ## _scalar, TYPE ## _matrix, TS ## _scalar, TYPE ## _array, mx_el_and) */ \ /* DEFNDBINOP_FN (sm_el_or, TS ## _scalar, TYPE ## _matrix, TS ## _scalar, TYPE ## _array, mx_el_or) */ +#define OCTAVE_SM_POW_OPS(T1, T2) \ +octave_value elem_xpow (octave_ ## T1 a, T2 ## NDArray b) \ +{ \ + T2 ## NDArray result (b.dims ()); \ + for (int i = 0; i < b.length (); i++) \ + { \ + OCTAVE_QUIT; \ + result (i) = pow (a, b(i)); \ + } \ + return octave_value (result); \ +} + #define OCTAVE_SM_INT_OPS(TYPE) \ + OCTAVE_SM_POW_OPS (TYPE, TYPE) \ OCTAVE_SM_INT_ARITH_OPS (TYPE, TYPE) \ OCTAVE_SM_INT_CMP_OPS (TYPE, TYPE) \ OCTAVE_SM_INT_BOOL_OPS (TYPE, TYPE) \ @@ -256,7 +267,7 @@ /* return octave_value (v1.TM ## _array_value () / d); */ \ /* } */ \ \ - /* DEFNDBINOP_FN (ms_el_pow, TM ## _matrix, TS ## _scalar, TM ## _array, TS ## _scalar, elem_xpow) */ \ + DEFNDBINOP_FN (ms_el_pow, TM ## _matrix, TS ## _scalar, TM ## _array, TS ## _scalar, elem_xpow) \ \ /* DEFBINOP (el_ldiv, TM ## _matrix, TS ## _scalar) */ \ /* { */ \ @@ -281,7 +292,20 @@ #define OCTAVE_MS_INT_ASSIGN_OPS(TM, TS) \ DEFNDASSIGNOP_FN (ms_assign, TM ## _matrix, TS ## _scalar, TS ## _array, assign) +#define OCTAVE_MS_POW_OPS(T1, T2) \ +octave_value elem_xpow (T1 ## NDArray a, octave_ ## T2 b) \ +{ \ + T1 ## NDArray result (a.dims ()); \ + for (int i = 0; i < a.length (); i++) \ + { \ + OCTAVE_QUIT; \ + result (i) = pow (a(i), b); \ + } \ + return octave_value (result); \ +} + #define OCTAVE_MS_INT_OPS(TYPE) \ + OCTAVE_MS_POW_OPS (TYPE, TYPE) \ OCTAVE_MS_INT_ARITH_OPS (TYPE, TYPE) \ OCTAVE_MS_INT_CMP_OPS (TYPE, TYPE) \ OCTAVE_MS_INT_BOOL_OPS (TYPE, TYPE) \ @@ -330,7 +354,7 @@ \ DEFNDBINOP_FN (mm_el_div, T1 ## _matrix, T2 ## _matrix, T1 ## _array, T2 ## _array, quotient) \ \ - /* DEFNDBINOP_FN (mm_el_pow, T1 ## _matrix, T2 ## _matrix, T1 ## _array, T2 ## _array, elem_xpow) */ \ + DEFNDBINOP_FN (mm_el_pow, T1 ## _matrix, T2 ## _matrix, T1 ## _array, T2 ## _array, elem_xpow) \ \ /* DEFBINOP (mm_el_ldiv, T1 ## _matrix, T2 ## _matrix) */ \ /* { */ \ @@ -354,8 +378,28 @@ #define OCTAVE_MM_INT_ASSIGN_OPS(TYPE) \ DEFNDASSIGNOP_FN (mm_assign, TYPE ## _matrix, TYPE ## _matrix, TYPE ## _array, assign) +#define OCTAVE_MM_POW_OPS(T1, T2) \ +octave_value elem_xpow (T1 ## NDArray a, T2 ## NDArray b) \ +{ \ + dim_vector a_dims = a.dims (); \ + dim_vector b_dims = b.dims (); \ + if (a_dims != b_dims) \ + { \ + gripe_nonconformant ("operator .^", a_dims, b_dims); \ + return octave_value (); \ + } \ + T1 ## NDArray result (a_dims); \ + for (int i = 0; i < a.length (); i++) \ + { \ + OCTAVE_QUIT; \ + result (i) = pow (a(i), b(i)); \ + } \ + return octave_value (result); \ +} + #define OCTAVE_MM_INT_OPS(TYPE) \ OCTAVE_M_INT_UNOPS (TYPE) \ + OCTAVE_MM_POW_OPS (TYPE, TYPE) \ OCTAVE_MM_INT_ARITH_OPS (TYPE, TYPE) \ OCTAVE_MM_INT_CMP_OPS (TYPE, TYPE) \ OCTAVE_MM_INT_BOOL_OPS (TYPE, TYPE) \ @@ -387,7 +431,7 @@ INSTALL_BINOP (op_sub, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_sub); \ INSTALL_BINOP (op_mul, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_mul); \ INSTALL_BINOP (op_div, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_div); \ - /* INSTALL_BINOP (op_pow, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_pow); */ \ + INSTALL_BINOP (op_pow, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_pow); \ INSTALL_BINOP (op_ldiv, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_ldiv); \ INSTALL_BINOP (op_el_mul, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_el_mul); \ INSTALL_BINOP (op_el_div, octave_ ## T1 ## _scalar, octave_ ## T2 ## _scalar, ss_el_div); \ @@ -464,7 +508,7 @@ \ INSTALL_BINOP (op_el_mul, octave_ ## T1 ## _matrix, octave_ ## T2 ## _scalar, ms_el_mul); \ /* INSTALL_BINOP (op_el_div, octave_ ## T1 ## _matrix, octave_ ## T2 ## _scalar, ms_el_div); */ \ - /* INSTALL_BINOP (op_el_pow, octave_ ## T1 ## _matrix, octave_ ## T2 ## _scalar, ms_el_pow); */ \ + INSTALL_BINOP (op_el_pow, octave_ ## T1 ## _matrix, octave_ ## T2 ## _scalar, ms_el_pow); \ /* INSTALL_BINOP (op_el_ldiv, octave_ ## T1 ## _matrix, octave_ ## T2 ## _scalar, ms_el_ldiv); */ #define OCTAVE_INSTALL_MS_INT_CMP_OPS(T1, T2) \ @@ -513,7 +557,7 @@ /* INSTALL_BINOP (op_ldiv, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_ldiv); */ \ INSTALL_BINOP (op_el_mul, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_el_mul); \ INSTALL_BINOP (op_el_div, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_el_div); \ - /* INSTALL_BINOP (op_el_pow, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_el_pow); */ \ + INSTALL_BINOP (op_el_pow, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_el_pow); \ /* INSTALL_BINOP (op_el_ldiv, octave_ ## T1 ## _matrix, octave_ ## T2 ## _matrix, mm_el_ldiv); */ #define OCTAVE_INSTALL_MM_INT_CMP_OPS(T1, T2) \
--- a/src/bitfcns.cc +++ b/src/bitfcns.cc @@ -86,23 +86,15 @@ \ if (nargin == 2) \ { \ - if (args(0).type_id () == octave_matrix::static_type_id () \ - || args(0).type_id () == octave_scalar::static_type_id () \ - || args(1).type_id () == octave_matrix::static_type_id () \ - || args(1).type_id () == octave_scalar::static_type_id ()) \ + if ((args(0).class_name () == octave_scalar::static_class_name ()) \ + || (args(1).class_name () == octave_scalar::static_class_name ())) \ { \ - bool arg0_is_int = true; \ - bool arg1_is_int = true; \ - \ - if (args(0).type_id () == octave_matrix::static_type_id () \ - || args(0).type_id () == octave_scalar::static_type_id ()) \ - arg0_is_int = false; \ - \ - if (args(1).type_id () == octave_matrix::static_type_id () \ - || args(1).type_id () == octave_scalar::static_type_id ()) \ - arg1_is_int = false; \ - \ - if (!arg0_is_int && !arg1_is_int) \ + bool arg0_is_int = (args(0).class_name () != \ + octave_scalar::static_class_name ()); \ + bool arg1_is_int = (args(1).class_name () != \ + octave_scalar::static_class_name ()); \ + \ + if (! (arg0_is_int || arg1_is_int)) \ { \ uint64NDArray x (args(0).array_value ()); \ uint64NDArray y (args(1).array_value ()); \ @@ -185,7 +177,7 @@ error ("%s: invalid operand type", FNAME); \ } \ } \ - else if (args(0).type_id () == args(1).type_id ()) \ + else if (args(0).class_name () == args(1).class_name ()) \ { \ if (args(0).type_id () == octave_uint64_matrix::static_type_id () \ || args(0).type_id () == octave_uint64_scalar::static_type_id ()) \