# HG changeset patch # User Jaroslav Hajek # Date 1222873513 14400 # Node ID 66bc6f9b4f7283a4a230c0c24f1acf707b4da45e # Parent dadf478ddc4212d7dceef3618b811bd36758d04c rewrite integer arithmetics and conversions diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-09-30 Jaroslav Hajek + + * aclocal.m4 (OCTAVE_FAST_INT_OPS): New macro. + * configure.in: Call OCTAVE_FAST_INT_OPS + 2008-09-08 John W. Eaton * mkoctfile.cc.in, octave-bug.cc.in, octave-config.cc.in: Style fixes. diff --git a/aclocal.m4 b/aclocal.m4 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -1206,3 +1206,68 @@ AC_SUBST([FT2_CFLAGS]) AC_SUBST([FT2_LIBS])]) dnl end of freetype2.m4 + +dnl Check whether fast signed integer arithmetics using bit tricks +dnl can be used in oct-inttypes.h. Defines HAVE_FAST_INT_OPS if +dnl the following conditions hold: +dnl 1. Signed numbers are represented by twos complement +dnl (see ) +dnl 2. static_cast to unsigned int counterpart works like interpreting +dnl the signed bit pattern as unsigned (and is thus zero-cost). +dnl 3. Signed addition and subtraction yield the same bit results as unsigned. +dnl (We use casts to prevent optimization interference, so there is no +dnl need for things like -ftrapv). +dnl 4. Bit operations on signed integers work like on unsigned integers, +dnl except for the shifts. Shifts are arithmetic. +dnl +AC_DEFUN([OCTAVE_FAST_INT_OPS],[ +AC_MSG_CHECKING([whether fast integer arithmetics is usable]) +AC_LANG_PUSH(C++) +AC_RUN_IFELSE([AC_LANG_PROGRAM([[ +#include +template +static bool +do_test (UT, ST) +{ + volatile ST s = std::numeric_limits::min () / 3; + volatile UT u = static_cast (s); + if (*(reinterpret_cast (&u)) != s) return true; + + u = 0; u = ~u; + if (*(reinterpret_cast (&u)) != -1) return true; + + ST sx, sy; + sx = std::numeric_limits::max () / 2 + 1; + sy = std::numeric_limits::max () / 2 + 2; + if (static_cast (static_cast (sx) + static_cast (sy)) + != std::numeric_limits::min () + 1) return true; + if (static_cast (static_cast (sx) - static_cast (sy)) + != -1) return true; + + if ((sx & sy) != (static_cast (sx) & static_cast (sy))) + return true; + if ((sx | sy) != (static_cast (sx) | static_cast (sy))) + return true; + if ((sx ^ sy) != (static_cast (sx) ^ static_cast (sy))) + return true; + if ((-1 >> 1) != -1) return true; + return false; +} + +#define DO_TEST(T) \ +if (do_test (static_cast (0), static_cast (0))) \ + return sizeof (T); +]],[[ + DO_TEST(char) + DO_TEST(short) + DO_TEST(int) + DO_TEST(long) +#if (defined(HAVE_LONG_LONG_INT) && defined(HAVE_UNSIGNED_LONG_LONG_INT)) + DO_TEST(long long) +#endif +]])], +[AC_MSG_RESULT([yes]) + AC_DEFINE(HAVE_FAST_INT_OPS,1,[Define if signed integers use two's complement])], +[AC_MSG_RESULT([no])]) +AC_LANG_POP(C++)]) + diff --git a/configure.in b/configure.in --- a/configure.in +++ b/configure.in @@ -1374,6 +1374,10 @@ OCTAVE_DYNAMIC_AUTO_ARRAYS +### See if we can use fast integer arithmetics + +OCTAVE_FAST_INT_OPS + ### Checks for header files. AC_HEADER_STDC diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2008-09-30 Jaroslav Hajek + + * oct-inttypes.h: Mostly rewrite. + * oct-inttypes.cc: Modstly rewrite. + 2008-09-29 Jaroslav Hajek * Array.cc (Array::maybe_delete_elements_2(idx_vector&)): Return on diff --git a/liboctave/oct-inttypes.cc b/liboctave/oct-inttypes.cc --- a/liboctave/oct-inttypes.cc +++ b/liboctave/oct-inttypes.cc @@ -24,71 +24,164 @@ #include #endif +#include "lo-error.h" + #include "oct-inttypes.h" -#define INSTANTIATE_INT_DOUBLE_BIN_OP(T, OP) \ - template OCTAVE_API octave_int operator OP (const octave_int&, double) +// define type names. +#define DECLARE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ + template <> \ + const char * \ + octave_int::type_name () { return TYPENAME; } -#define INSTANTIATE_INT_DOUBLE_BIN_OPS(T) \ - INSTANTIATE_INT_DOUBLE_BIN_OP (T, +); \ - INSTANTIATE_INT_DOUBLE_BIN_OP (T, -); \ - INSTANTIATE_INT_DOUBLE_BIN_OP (T, *); \ - INSTANTIATE_INT_DOUBLE_BIN_OP (T, /) +DECLARE_OCTAVE_INT_TYPENAME (int8_t, "int8") +DECLARE_OCTAVE_INT_TYPENAME (int16_t, "int16") +DECLARE_OCTAVE_INT_TYPENAME (int32_t, "int32") +DECLARE_OCTAVE_INT_TYPENAME (int64_t, "int64") +DECLARE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") +DECLARE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") +DECLARE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") +DECLARE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") + +static void +gripe_64bit_mul() +{ + (*current_liboctave_error_handler) + ("64-bit integer multiplication is not implemented"); +} -#define INSTANTIATE_DOUBLE_INT_BIN_OP(T, OP) \ - template OCTAVE_API octave_int operator OP (double, const octave_int&) +template <> +uint64_t +octave_int_arith_base::mul (uint64_t, uint64_t) +{ + gripe_64bit_mul (); + return static_cast (0); +} +template <> +int64_t +octave_int_arith_base::mul (int64_t, int64_t) +{ + gripe_64bit_mul (); + return static_cast (0); +} + +static void +gripe_64bit_mixed() +{ + (*current_liboctave_error_handler) + ("mixed double - 64-bit integer operations are not implemented"); +} -#define INSTANTIATE_DOUBLE_INT_BIN_OPS(T) \ - INSTANTIATE_DOUBLE_INT_BIN_OP (T, +); \ - INSTANTIATE_DOUBLE_INT_BIN_OP (T, -); \ - INSTANTIATE_DOUBLE_INT_BIN_OP (T, *); \ - INSTANTIATE_DOUBLE_INT_BIN_OP (T, /) +#define DEFINE_DOUBLE_INT64_OP0(OP,ARGT,RETT) \ + template <> \ + OCTAVE_API RETT \ + operator OP (const double&, const ARGT&) \ + { gripe_64bit_mixed (); return 0.0; } \ + template <> \ + OCTAVE_API RETT \ + operator OP (const ARGT&, const double&) \ + { gripe_64bit_mixed (); return 0.0; } \ -#define INSTANTIATE_INT_DOUBLE_CMP_OP(T, OP) \ - template OCTAVE_API bool operator OP (const octave_int&, const double&) +#define DEFINE_DOUBLE_INT64_BIN_OP(OP) \ + DEFINE_DOUBLE_INT64_OP0(OP,octave_int64,octave_int64) \ + DEFINE_DOUBLE_INT64_OP0(OP,octave_uint64,octave_uint64) + +DEFINE_DOUBLE_INT64_BIN_OP(+) +DEFINE_DOUBLE_INT64_BIN_OP(-) +DEFINE_DOUBLE_INT64_BIN_OP(*) +DEFINE_DOUBLE_INT64_BIN_OP(/) + +#define DEFINE_DOUBLE_INT64_CMP_OP(OP) \ + DEFINE_DOUBLE_INT64_OP0(OP,octave_int64,bool) \ + DEFINE_DOUBLE_INT64_OP0(OP,octave_uint64,bool) -#define INSTANTIATE_INT_DOUBLE_CMP_OPS(T) \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, <); \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, <=); \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, >=); \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, >); \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, ==); \ - INSTANTIATE_INT_DOUBLE_CMP_OP (T, !=) +DEFINE_DOUBLE_INT64_CMP_OP(<) +DEFINE_DOUBLE_INT64_CMP_OP(<=) +DEFINE_DOUBLE_INT64_CMP_OP(>) +DEFINE_DOUBLE_INT64_CMP_OP(>=) +DEFINE_DOUBLE_INT64_CMP_OP(==) +DEFINE_DOUBLE_INT64_CMP_OP(!=) + +//template +//bool +//xisnan (const octave_int&) +//{ +// return false; +//} -#define INSTANTIATE_DOUBLE_INT_CMP_OP(T, OP) \ - template OCTAVE_API bool operator OP (const double&, const octave_int&) +template +octave_int +pow (const octave_int& a, const octave_int& b) +{ + octave_int retval; + + octave_int zero = octave_int (0); + octave_int one = octave_int (1); + + if (b == zero || a == one) + retval = one; + else if (b < zero) + { + if (std::numeric_limits::is_signed && a.value () == -1) + retval = (b.value () % 2) ? a : one; + else + retval = zero; + } + else + { + octave_int a_val = a; + T b_val = b; // no need to do saturation on b -#define INSTANTIATE_DOUBLE_INT_CMP_OPS(T) \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, <); \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, <=); \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, >=); \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, >); \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, ==); \ - INSTANTIATE_DOUBLE_INT_CMP_OP (T, !=) + retval = a; + + b_val -= 1; + + while (b_val != 0) + { + if (b_val & 1) + retval = retval * a_val; + + b_val = b_val >> 1; + + if (b_val) + a_val = a_val * a_val; + } + } + + return retval; +} -#define INSTANTIATE_INT_BITCMP_OP(T, OP) \ - template OCTAVE_API octave_int \ - operator OP (const octave_int&, const octave_int&) +template +octave_int +pow (const double& a, const octave_int& b) +{ return octave_int (pow (a, b.double_value ())); } + +template +octave_int +pow (const octave_int& a, const double& b) +{ return octave_int (pow (a.double_value (), b)); } -#define INSTANTIATE_INT_BITCMP_OPS(T) \ - INSTANTIATE_INT_BITCMP_OP (T, &); \ - INSTANTIATE_INT_BITCMP_OP (T, |); \ - INSTANTIATE_INT_BITCMP_OP (T, ^) +template +octave_int +powf (const float& a, const octave_int& b) +{ return octave_int (pow (a, b.float_value ())); } + +template +octave_int +powf (const octave_int& a, const float& b) +{ return octave_int (pow (a.float_value (), b)); } #define INSTANTIATE_INTTYPE(T) \ template class OCTAVE_API octave_int; \ template OCTAVE_API octave_int pow (const octave_int&, const octave_int&); \ - template OCTAVE_API octave_int pow (double, const octave_int&); \ - template OCTAVE_API octave_int pow (const octave_int&, double b); \ + template OCTAVE_API octave_int pow (const double&, const octave_int&); \ + template OCTAVE_API octave_int pow (const octave_int&, const double&); \ + template OCTAVE_API octave_int powf (const float&, const octave_int&); \ + template OCTAVE_API octave_int powf (const octave_int&, const float&); \ template OCTAVE_API std::ostream& operator << (std::ostream&, const octave_int&); \ template OCTAVE_API std::istream& operator >> (std::istream&, octave_int&); \ template OCTAVE_API octave_int \ bitshift (const octave_int&, int, const octave_int&); \ - INSTANTIATE_INT_DOUBLE_BIN_OPS (T); \ - INSTANTIATE_DOUBLE_INT_BIN_OPS (T); \ - INSTANTIATE_INT_DOUBLE_CMP_OPS (T); \ - INSTANTIATE_DOUBLE_INT_CMP_OPS (T); \ - INSTANTIATE_INT_BITCMP_OPS (T) INSTANTIATE_INTTYPE (int8_t); INSTANTIATE_INTTYPE (int16_t); @@ -100,283 +193,6 @@ INSTANTIATE_INTTYPE (uint32_t); INSTANTIATE_INTTYPE (uint64_t); -#define INSTANTIATE_INTTYPE_BIN_OP(T1, T2, OP) \ - template OCTAVE_API octave_int::TR> \ - operator OP (const octave_int&, const octave_int&) - -#define INSTANTIATE_INTTYPE_BIN_OPS(T1, T2) \ - INSTANTIATE_INTTYPE_BIN_OP (T1, T2, +); \ - INSTANTIATE_INTTYPE_BIN_OP (T1, T2, -); \ - INSTANTIATE_INTTYPE_BIN_OP (T1, T2, *); \ - INSTANTIATE_INTTYPE_BIN_OP (T1, T2, /) - -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int8_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int16_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int32_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (int64_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint8_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint16_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint32_t, uint64_t); - -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, int8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, int16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, int32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, int64_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, uint8_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, uint16_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, uint32_t); -INSTANTIATE_INTTYPE_BIN_OPS (uint64_t, uint64_t); - -#define INSTANTIATE_INTTYPE_SHIFT_OP(T, OP) \ - template OCTAVE_API octave_int operator OP (const octave_int&, const int&) - -#define INSTANTIATE_INTTYPE_SHIFT_OPS(T) \ - INSTANTIATE_INTTYPE_SHIFT_OP (T, <<); \ - INSTANTIATE_INTTYPE_SHIFT_OP (T, >>) - -INSTANTIATE_INTTYPE_SHIFT_OPS (int8_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (int16_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (int32_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (int64_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (uint8_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (uint16_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (uint32_t); -INSTANTIATE_INTTYPE_SHIFT_OPS (uint64_t); - -#define INSTANTIATE_OCTAVE_INT_CMP_OP(OP, T1, T2) \ - template OCTAVE_API bool operator OP (const octave_int&, const octave_int&) - -#define INSTANTIATE_OCTAVE_INT_CMP_OPS(T1, T2) \ - INSTANTIATE_OCTAVE_INT_CMP_OP (<, T1, T2); \ - INSTANTIATE_OCTAVE_INT_CMP_OP (<=, T1, T2); \ - INSTANTIATE_OCTAVE_INT_CMP_OP (>=, T1, T2); \ - INSTANTIATE_OCTAVE_INT_CMP_OP (>, T1, T2); \ - INSTANTIATE_OCTAVE_INT_CMP_OP (==, T1, T2); \ - INSTANTIATE_OCTAVE_INT_CMP_OP (!=, T1, T2) - -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, uint16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, uint32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int8_t, uint64_t); - -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, uint16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, uint32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int16_t, uint64_t); - -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, uint16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, uint32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int32_t, uint64_t); - -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, uint16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, uint32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (int64_t, uint64_t); - -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, uint16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, uint32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint8_t, uint64_t); - -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, int8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, int16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, int32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, uint16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, uint32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint16_t, uint64_t); - -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, int8_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, int16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, int32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, uint16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, uint32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint32_t, uint64_t); - -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, int8_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, int16_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, int32_t); -// INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, int64_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, uint8_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, uint16_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, uint32_t); -INSTANTIATE_OCTAVE_INT_CMP_OPS (uint64_t, uint64_t); - -// The following apply if the unsigned type is at least as wide as the -// signed type (then we can cast postive signed values to the unsigned -// type and compare). - -#define OCTAVE_US_TYPE1_CMP_OP(OP, LTZ_VAL, UT, ST) \ - template <> \ - bool \ - operator OP (const octave_int& lhs, const octave_int& rhs) \ - { \ - return rhs.value () < 0 ? LTZ_VAL \ - : lhs.value () OP static_cast (rhs.value ()); \ - } - -#define OCTAVE_US_TYPE1_CMP_OPS(UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (<, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (<=, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (>=, true, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (>, true, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (==, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP (!=, true, UT, ST) - -#define OCTAVE_SU_TYPE1_CMP_OP(OP, LTZ_VAL, ST, UT) \ - template <> \ - bool \ - operator OP (const octave_int& lhs, const octave_int& rhs) \ - { \ - return lhs.value () < 0 ? LTZ_VAL \ - : static_cast (lhs.value ()) OP rhs.value (); \ - } - -#define OCTAVE_SU_TYPE1_CMP_OPS(ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (<, true, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (<=, true, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (>=, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (>, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (==, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP (!=, true, ST, UT) - -#define OCTAVE_TYPE1_CMP_OPS(UT, ST) \ - OCTAVE_US_TYPE1_CMP_OPS (UT, ST) \ - OCTAVE_SU_TYPE1_CMP_OPS (ST, UT) - -OCTAVE_TYPE1_CMP_OPS (uint32_t, int8_t) -OCTAVE_TYPE1_CMP_OPS (uint32_t, int16_t) -OCTAVE_TYPE1_CMP_OPS (uint32_t, int32_t) - -OCTAVE_TYPE1_CMP_OPS (uint64_t, int8_t) -OCTAVE_TYPE1_CMP_OPS (uint64_t, int16_t) -OCTAVE_TYPE1_CMP_OPS (uint64_t, int32_t) -OCTAVE_TYPE1_CMP_OPS (uint64_t, int64_t) - -// The following apply if the signed type is wider than the unsigned -// type (then we can cast unsigned values to the signed type and -// compare if the signed value is positive). - -#define OCTAVE_US_TYPE2_CMP_OP(OP, LTZ_VAL, UT, ST) \ - template <> \ - bool \ - operator OP (const octave_int& lhs, const octave_int& rhs) \ - { \ - return rhs.value () < 0 ? LTZ_VAL \ - : static_cast (lhs.value ()) OP rhs.value (); \ - } - -#define OCTAVE_US_TYPE2_CMP_OPS(ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (<, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (<=, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (>=, true, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (>, true, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (==, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP (!=, true, ST, UT) - -#define OCTAVE_SU_TYPE2_CMP_OP(OP, LTZ_VAL, ST, UT) \ - template <> \ - bool \ - operator OP (const octave_int& lhs, const octave_int& rhs) \ - { \ - return lhs.value () < 0 ? LTZ_VAL \ - : lhs.value () OP static_cast (rhs.value ()); \ - } - -#define OCTAVE_SU_TYPE2_CMP_OPS(ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (<, true, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (<=, true, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (>=, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (>, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (==, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP (!=, true, ST, UT) - -#define OCTAVE_TYPE2_CMP_OPS(UT, ST) \ - OCTAVE_US_TYPE2_CMP_OPS (UT, ST) \ - OCTAVE_SU_TYPE2_CMP_OPS (ST, UT) - -OCTAVE_TYPE2_CMP_OPS (uint32_t, int64_t) - - /* ;;; Local Variables: *** diff --git a/liboctave/oct-inttypes.h b/liboctave/oct-inttypes.h --- a/liboctave/oct-inttypes.h +++ b/liboctave/oct-inttypes.h @@ -25,6 +25,7 @@ #define octave_inttypes_h 1 #include +#include #include #include @@ -33,296 +34,661 @@ #include "lo-ieee.h" #include "lo-mappers.h" -template -class -octave_int_binop_traits +// Query for an integer type of certain sizeof, and signedness. +template +struct query_integer_type { public: - // The return type for a T1 by T2 binary operation. - typedef T1 TR; + static const bool registered = false; + typedef void type; // Void shall result in a compile-time error if we + // attempt to use it in computations. }; -#define OCTAVE_INT_BINOP_TRAIT(T1, T2, T3) \ - template <> \ - class octave_int_binop_traits \ - { \ - public: \ - typedef T3 TR; \ - } - -OCTAVE_INT_BINOP_TRAIT (int8_t, int8_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, int16_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, int32_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, int64_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, uint8_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, uint16_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, uint32_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (int8_t, uint64_t, int8_t); - -OCTAVE_INT_BINOP_TRAIT (int16_t, int8_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, int16_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, int32_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, int64_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, uint8_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, uint16_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, uint32_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (int16_t, uint64_t, int16_t); - -OCTAVE_INT_BINOP_TRAIT (int32_t, int8_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, int16_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, int32_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, int64_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, uint8_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, uint16_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, uint32_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (int32_t, uint64_t, int32_t); - -OCTAVE_INT_BINOP_TRAIT (int64_t, int8_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, int16_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, int32_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, int64_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, uint8_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, uint16_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, uint32_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (int64_t, uint64_t, int64_t); - -OCTAVE_INT_BINOP_TRAIT (uint8_t, int8_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, int16_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, int32_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, int64_t, int8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, uint8_t, uint8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, uint16_t, uint8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, uint32_t, uint8_t); -OCTAVE_INT_BINOP_TRAIT (uint8_t, uint64_t, uint8_t); - -OCTAVE_INT_BINOP_TRAIT (uint16_t, int8_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, int16_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, int32_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, int64_t, int16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, uint8_t, uint16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, uint16_t, uint16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, uint32_t, uint16_t); -OCTAVE_INT_BINOP_TRAIT (uint16_t, uint64_t, uint16_t); - -OCTAVE_INT_BINOP_TRAIT (uint32_t, int8_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, int16_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, int32_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, int64_t, int32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, uint8_t, uint32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, uint16_t, uint32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, uint32_t, uint32_t); -OCTAVE_INT_BINOP_TRAIT (uint32_t, uint64_t, uint32_t); - -OCTAVE_INT_BINOP_TRAIT (uint64_t, int8_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, int16_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, int32_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, int64_t, int64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, uint8_t, uint64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, uint16_t, uint64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, uint32_t, uint64_t); -OCTAVE_INT_BINOP_TRAIT (uint64_t, uint64_t, uint64_t); - -template -inline T2 -octave_int_fit_to_range (const T1& x, const T2& mn, const T2& mx, - int& conv_flag, int math_truncate) -{ - bool out_of_range_max = x > mx; - bool out_of_range_min = x < mn; - conv_flag |= (out_of_range_max || out_of_range_min ? math_truncate : 0); - return (out_of_range_max ? mx : (out_of_range_min ? mn : T2 (x))); -} - -template -inline T -octave_int_fit_to_range (const double& x, const T& mn, const T& mx, - int& conv_flag, int math_truncate) -{ - bool out_of_range_max = x > mx; - bool out_of_range_min = x < mn; - conv_flag |= (out_of_range_max || out_of_range_min ? math_truncate : 0); - return (__lo_ieee_isnan (x) - ? 0 : (out_of_range_max - ? mx : (out_of_range_min ? mn : static_cast (x)))); +#define REGISTER_INT_TYPE(TYPE) \ +template <> \ +class query_integer_type::is_signed> \ +{ \ +public: \ + static const bool registered = true; \ + typedef TYPE type; \ } -// If X is unsigned and the new type is signed, then we only have to -// check the upper limit, but we should cast the maximum value of the -// new type to an unsigned type before performing the comparison. -// This should always be OK because the maximum value should always be -// positive. +// No two registered integers can share sizeof and signedness. +REGISTER_INT_TYPE (int8_t); +REGISTER_INT_TYPE (uint8_t); +REGISTER_INT_TYPE (int16_t); +REGISTER_INT_TYPE (uint16_t); +REGISTER_INT_TYPE (int32_t); +REGISTER_INT_TYPE (uint32_t); +REGISTER_INT_TYPE (int64_t); +REGISTER_INT_TYPE (uint64_t); + +// Selects one of two types, according to a static bool. May be useful in +// general. + +template +class if_else_type +{ +public: + typedef FT type; +}; -#define OCTAVE_US_S_FTR(T1, T2, TC) \ - template <> \ - inline T2 \ - octave_int_fit_to_range (const T1& x, const T2&, const T2& mx, \ - int& conv_flag, int math_truncate) \ - { \ - bool out_of_range = x > static_cast (mx); \ - conv_flag |= (out_of_range ? math_truncate : 0); \ - return out_of_range ? mx : x; \ - } +template +class if_else_type +{ +public: + typedef TT type; +}; + +// Rationale: Comparators have a single static method, rel(), that returns the +// result of the binary relation. They also have two static boolean fields: +// ltval, gtval determine the value of x OP y if x < y, x > y, respectively. +#define REGISTER_OCTAVE_CMP_OP(NM,OP) \ + class NM \ + { \ + public: \ + static const bool ltval = (0 OP 1), gtval = (1 OP 0); \ + template \ + static bool op (T x, T y) { return x OP y; } \ + } + +// We also provide two special relations: ct, yielding always true, and cf, +// yielding always false. +#define REGISTER_OCTAVE_CONST_OP(NM,value) \ + class NM \ + { \ + public: \ + static const bool ltval = value, gtval = value; \ + template \ + static bool op (T, T) { return value; } \ + } -#define OCTAVE_US_S_FTR_FCNS(T) \ - OCTAVE_US_S_FTR (T, char, unsigned char) \ - OCTAVE_US_S_FTR (T, signed char, unsigned char) \ - OCTAVE_US_S_FTR (T, short, unsigned short) \ - OCTAVE_US_S_FTR (T, int, unsigned int) \ - OCTAVE_US_S_FTR (T, long, unsigned long) \ - OCTAVE_US_S_FTR (T, long long, unsigned long long) - -OCTAVE_US_S_FTR_FCNS (unsigned char) -OCTAVE_US_S_FTR_FCNS (unsigned short) -OCTAVE_US_S_FTR_FCNS (unsigned int) -OCTAVE_US_S_FTR_FCNS (unsigned long) -OCTAVE_US_S_FTR_FCNS (unsigned long long) +// Handles non-homogeneous integer comparisons. Avoids doing useless tests. +class octave_int_cmp_op +{ + // This determines a suitable promotion type for T1 when meeting T2 in a + // binary relation. If promotion to int or T2 is safe, it is used. Otherwise, + // the signedness of T1 is preserved and it is widened if T2 is wider. + // Notice that if this is applied to both types, they must end up with equal + // size. + template + class prom + { + // Promote to int? + static const bool pint = (sizeof (T1) < sizeof (int) + && sizeof (T2) < sizeof (int)); + static const bool t1sig = std::numeric_limits::is_signed; + static const bool t2sig = std::numeric_limits::is_signed; + static const bool psig = + (pint || (sizeof (T2) > sizeof (T1) && t2sig) || t1sig); + static const int psize = + (pint ? sizeof (int) : (sizeof (T2) > sizeof (T1) + ? sizeof (T2) : sizeof (T1))); + public: + typedef typename query_integer_type::type type; + }; -// If X is signed and the new type is unsigned, then we only have to -// check the lower limit (which will always be 0 for an unsigned -// type). The upper limit will be enforced correctly by converting to -// the new type, even if the type of X is wider than the new type. + // Implements comparisons between two types of equal size but + // possibly different signedness. + template + class uiop + { + typedef typename query_integer_type::type utype; + typedef typename query_integer_type::type stype; + public: + static bool op (utype x, utype y) + { return xop::op (x, y); } + static bool op (stype x, stype y) + { return xop::op (x, y); } + static bool op (stype x, utype y) + { return (x < 0) ? xop::ltval : xop::op (static_cast (x), y); } + static bool op (utype x, stype y) + { return (y < 0) ? xop::gtval : xop::op (x, static_cast (y)); } + }; -#define OCTAVE_S_US_FTR(T1, T2) \ - template <> \ - inline T2 \ - octave_int_fit_to_range (const T1& x, const T2&, const T2&, \ - int& conv_flag, int math_truncate) \ - { \ - bool out_of_range = x < 0; \ - conv_flag |= (out_of_range ? math_truncate : 0); \ - return x <= 0 ? 0 : x; \ - } +public: + REGISTER_OCTAVE_CMP_OP (lt, <); + REGISTER_OCTAVE_CMP_OP (le, <=); + REGISTER_OCTAVE_CMP_OP (gt, >); + REGISTER_OCTAVE_CMP_OP (ge, >=); + REGISTER_OCTAVE_CMP_OP (eq, ==); + REGISTER_OCTAVE_CMP_OP (ne, !=); + REGISTER_OCTAVE_CONST_OP (ct, true); + REGISTER_OCTAVE_CONST_OP (cf, false); + + // Universal comparison operation. + template + static bool + op (T1 x, T2 y) + { + typedef typename prom::type PT1; + typedef typename prom::type PT2; + return uiop::op (static_cast (x), + static_cast (y)); + } +}; + +// Base integer class. No data, just conversion methods and exception flags. +template +class octave_int_base +{ +protected: + + static T min_val () { return std::numeric_limits:: min (); } + static T max_val () { return std::numeric_limits:: max (); } -#define OCTAVE_S_US_FTR_FCNS(T) \ - OCTAVE_S_US_FTR (T, unsigned char) \ - OCTAVE_S_US_FTR (T, unsigned short) \ - OCTAVE_S_US_FTR (T, unsigned int) \ - OCTAVE_S_US_FTR (T, unsigned long) \ - OCTAVE_S_US_FTR (T, unsigned long long) +public: + + // Convert integer value. + template + static T + truncate_int (const S& value) + { + // An exhaustive test whether the max and/or min check can be omitted. + static const bool t_is_signed = std::numeric_limits::is_signed; + static const bool s_is_signed = std::numeric_limits::is_signed; + static const int t_size = sizeof (T), s_size = sizeof (S); + static const bool omit_chk_min = + (! s_is_signed || (t_is_signed && t_size >= s_size)); + static const bool omit_chk_max = + (t_size > s_size || (t_size == s_size + && (! t_is_signed || s_is_signed))); + // If the check can be omitted, substitute constant false relation. + typedef octave_int_cmp_op::cf cf; + typedef octave_int_cmp_op::lt lt; + typedef octave_int_cmp_op::gt gt; + typedef typename if_else_type::type chk_min; + typedef typename if_else_type::type chk_max; -OCTAVE_S_US_FTR_FCNS (char) -OCTAVE_S_US_FTR_FCNS (signed char) -OCTAVE_S_US_FTR_FCNS (short) -OCTAVE_S_US_FTR_FCNS (int) -OCTAVE_S_US_FTR_FCNS (long) -OCTAVE_S_US_FTR_FCNS (long long) + // Efficiency of the following depends on inlining and dead code + // elimination, but that should be a piece of cake for most compilers. + if (chk_min::op (value, static_cast (min_val ()))) + { + ftrunc = true; + return min_val (); + } + else if (chk_max::op (value, static_cast (max_val ()))) + { + ftrunc = true; + return max_val (); + } + else + return static_cast (value); + } -#define OCTAVE_INT_CONV_FIT_TO_RANGE(r, T) \ - octave_int_fit_to_range (r, \ - std::numeric_limits::min (), \ - std::numeric_limits::max (), \ - octave_int::conv_flag, \ - octave_int::int_truncate) + // This does not check for NaN and non-int. It can be useful when using real + // types for integer arithmetics, e.g., 64-bit multiply via long double. + template + static T + truncate_real (const S& value) + { + if (value < min_val ()) + { + ftrunc = true; + return min_val (); + } + else if (value > max_val ()) + { + ftrunc = true; + return max_val (); + } + else + return static_cast (value); + } -#define OCTAVE_INT_FIT_TO_RANGE(r, T) \ - octave_int_fit_to_range (r, \ - std::numeric_limits::min (), \ - std::numeric_limits::max (), \ - octave_int::conv_flag, \ - octave_int::math_truncate) - -#define OCTAVE_INT_MIN_VAL2(T1, T2) \ - std::numeric_limits::TR>::min () - -#define OCTAVE_INT_MAX_VAL2(T1, T2) \ - std::numeric_limits::TR>::max () + // Convert a real number (check NaN and non-int). + template + static T + convert_real (const S& value) + { + if (lo_ieee_isnan (value)) + { + fnan = true; + return static_cast (0); + } + else if (value < min_val ()) + { + octave_int_base::ftrunc = true; + return min_val (); + } + else if (value > max_val ()) + { + ftrunc = true; + return max_val (); + } + else + { + S rvalue = xround (value); + if (rvalue != value) fnon_int = true; + return static_cast (rvalue); + } + } -#define OCTAVE_INT_FIT_TO_RANGE2(r, T1, T2) \ - octave_int_fit_to_range (r, \ - OCTAVE_INT_MIN_VAL2 (T1, T2), \ - OCTAVE_INT_MAX_VAL2 (T1, T2), \ - octave_int::TR>::conv_flag, \ - octave_int::TR>::math_truncate) + // Exception flags rationale: + // There is little reason to distinguish math and conversion exceptions at + // octave_int level. Doing this would require special constructors for + // intermediate int results in math computations. + // + // Boolean flags are used rather than a single flag, because raising a boolean + // flag is faster than masking an int flag (single mov versus mov, or, mov). + // Also, it is atomic, and thus thread-safe (but there is *one* flag for all + // threads). -// We have all the machinery below (octave_int_helper) to avoid a few -// warnings from GCC about comparisons always false due to limited -// range of data types. Ugh. The cure may be worse than the disease. + static bool get_trunc_flag () { return ftrunc; } + static bool get_nan_flag () { return fnan; } + static bool get_non_int_flag () { return fnon_int; } + static void clear_conv_flags () + { + ftrunc = false; + fnan = false; + fnon_int = false; + } + // For compatibility. + static bool get_math_trunc_flag () { return ftrunc || fnan; } + static void clear_conv_flag () { clear_conv_flags (); } + +protected: -// FIXME -- it would be nice to nest the helper class inside the -// octave_int class, but I don't see the magic for that at the moment. + // Conversion flags. + static bool ftrunc; + static bool fnon_int; + static bool fnan; +}; -template class octave_int; +template bool octave_int_base::ftrunc = false; +template bool octave_int_base::fnon_int = false; +template bool octave_int_base::fnan = false; + +// Saturated (homogeneous) integer arithmetics. The signed and unsigned +// implementations are significantly different, so we implement another layer +// and completely specialize. Arithmetics inherits from octave_int_base so that +// it can use its exceptions and truncation functions. template -class octave_int_helper -{ -public: - static octave_int abs (const T& x); +class octave_int_arith_base +{ }; - static octave_int signum (const T& x); - - template static void rshift_eq (T& ival, const T2& x); -}; - +// Unsigned arithmetics. C++ standard requires it to be modular, so the +// overflows can be handled efficiently and reliably. template -class octave_int_helper +class octave_int_arith_base : octave_int_base { public: - static octave_int - abs (const T& x) { return x; } + + static T + abs (T x) { return x; } + + static T + signum (T x) { return x ? static_cast (1) : static_cast (0); } + + // Shifts do not overflow. + static T + rshift (T x, int n) { return x >> n; } + + static T + lshift (T x, int n) { return x << n; } + + static T + minus (T x) + { + if (x != 0) octave_int_base::ftrunc = true; + return static_cast (0); + } + + // the overflow behaviour for unsigned integers is guaranteed by C/C++, + // so the following should always work. + static T + add (T x, T y) + { + T u = x + y; + if (u < x) + { + u = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + return u; + } + + static T + sub (T x, T y) + { + T u = x - y; + if (u > x) + { + u = 0; + octave_int_base::ftrunc = true; + } + return u; + } - static octave_int - signum (const T& x) { return x > 0 ? 1 : 0; } + // Multiplication is done using promotion to wider type. If there is no + // suitable promotion type, this operation *MUST* be specialized. But note + // that a real type can be used as the promotion type (e.g., long double for + // int64_t). + static T + mul (T x, T y) + { + // Promotion type for multiplication (if exists). + typedef typename query_integer_type<2*sizeof (T), false>::type mptype; + return truncate_int (static_cast (x) + * static_cast (y)); + } - template - static void - rshift_eq (T& ival, const T2& x) { ival = ival >> x; } + // Division with rounding to nearest. Note that / and % are probably + // computed by a single instruction. + static T + div (T x, T y) + { + if (y != 0) + { + T z = x / y, w = x % y; + if (w >= y-w) z += 1; + return z; + } + else + { + octave_int_base::ftrunc = true; + return x ? octave_int_base::max_val () : 0; + } + } }; +// Special handler for 64-bit integer multiply. +template <> +OCTAVE_API uint64_t +octave_int_arith_base::mul (uint64_t, uint64_t); + +// Signed integer arithmetics. +// Rationale: If HAVE_FAST_INT_OPS is defined, the following conditions +// should hold: +// 1. Signed numbers are represented by twos complement +// (see ) +// 2. static_cast to unsigned int counterpart works like interpreting +// the signed bit pattern as unsigned (and is thus zero-cost). +// 3. Signed addition and subtraction yield the same bit results as unsigned. +// (We use casts to prevent optimization interference, so there is no +// need for things like -ftrapv). +// 4. Bit operations on signed integers work like on unsigned integers, +// except for the shifts. Shifts are arithmetic. +// +// The above conditions are satisfied by most modern platforms. If +// HAVE_FAST_INT_OPS is defined, bit tricks and wraparound arithmetics are used +// to avoid conditional jumps as much as possible, thus being friendly to modern +// pipeline processor architectures. +// Otherwise, we fall back to a bullet-proof code that only uses assumptions +// guaranteed by the standard. + template -class octave_int_helper +class octave_int_arith_base : octave_int_base { + // The corresponding unsigned type. + typedef typename query_integer_type::type UT; public: - static octave_int - abs (const T& x) { return x < 0 ? -x : x; } + + // Returns 1 for negative number, 0 otherwise. + static T + signbit (T x) + { +#ifdef HAVE_FAST_INT_OPS + return static_cast (x) >> std::numeric_limits::digits; +#else + return (x < 0) ? 1 : 0; +#endif + } - static octave_int - signum (const T& x) { return x < 0 ? -1 : (x > 0 ? 1 : 0); } + static T + abs (T x) + { +#ifdef HAVE_FAST_INT_OPS + // This is close to how GCC does std::abs, but we can't just use std::abs, + // because its behaviour for INT_MIN is undefined and the compiler could + // discard the following test. + T m = x >> std::numeric_limits::digits; + T y = (x ^ m) - m; + if (y < 0) + { + y = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + return y; +#else + // -INT_MAX is safe because C++ actually allows only three implementations + // of integers: sign & magnitude, ones complement and twos complement. + // The first test will, with modest optimizations, evaluate at compile + // time, and maybe eliminate the branch completely. + T y; + if (octave_int_base::min_val () < -octave_int_base::max_val () + && x == octave_int_base::min_val ()) + { + y = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + else + y = (x < 0) ? -x : x; + return y; +#endif + } + + static T + signum (T x) + { + // With modest optimizations, this will compile without a jump. + return ((x > 0) ? 1 : 0) - signbit (x); + } + + // TODO: We do not have an authority what signed shifts should exactly do, so + // we define them the easy way. Note that Matlab does not define signed + // shifts. + + static T + rshift (T x, int n) { return x >> n; } + + static T + lshift (T x, int n) { return x << n; } + + // Minus has problems similar to abs. + static T + minus (T x) + { +#ifdef HAVE_FAST_INT_OPS + T y = -x; + if (y == octave_int_base::min_val ()) + { + --y; + octave_int_base::ftrunc = false; + } + return y; +#else + T y; + if (octave_int_base::min_val () < -octave_int_base::max_val () + && x == octave_int_base::min_val ()) + { + y = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + else + y = -x; + return y; +#endif + } - template - static void - rshift_eq (T& ival, const T2& x) - { - if (ival < 0) - ival = - (((-ival) >> x) & std::numeric_limits::max()); - else - ival = ival >> x; - } + static T + add (T x, T y) + { +#ifdef HAVE_FAST_INT_OPS + // The typecasts do nothing, but they are here to prevent an optimizing + // compiler from interfering. Also, the signed operations on small types + // actually return int. + T u = static_cast (x) + static_cast (y); + T ux = u ^ x, uy = u ^ y; + if ((ux & uy) < 0) + { + u = octave_int_base::max_val () + signbit (~u); + octave_int_base::ftrunc = true; + } + return u; +#else + // We shall carefully avoid anything that may overflow. + T u; + if (y < 0) + { + if (x < octave_int_base::min_val () - y) + { + u = octave_int_base::min_val (); + octave_int_base::ftrunc = true; + } + else + u = x + y; + } + else + { + if (x > octave_int_base::max_val () - y) + { + u = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + else + u = x + y; + } + + return u; +#endif + } + + // This is very similar to addition. + static T + sub (T x, T y) + { +#ifdef HAVE_FAST_INT_OPS + // The typecasts do nothing, but they are here to prevent an optimizing + // compiler from interfering. Also, the signed operations on small types + // actually return int. + T u = static_cast (x) - static_cast (y); + T ux = u ^ x, uy = u ^ ~y; + if ((ux & uy) < 0) + { + u = octave_int_base::max_val () + signbit (~u); + octave_int_base::ftrunc = true; + } + return u; +#else + // We shall carefully avoid anything that may overflow. + T u; + if (y < 0) + { + if (x > octave_int_base::max_val () + y) + { + u = octave_int_base::max_val (); + octave_int_base::ftrunc = true; + } + else + u = x - y; + } + else + { + if (x < octave_int_base::min_val () + y) + { + u = octave_int_base::min_val (); + octave_int_base::ftrunc = true; + } + else + u = x - y; + } + + return u; +#endif + } + + // Multiplication is done using promotion to wider type. If there is no + // suitable promotion type, this operation *MUST* be specialized. But note + // that a real type can be used as the promotion type (e.g., long double for + // int64_t). + static T + mul (T x, T y) + { + // Promotion type for multiplication (if exists). + typedef typename query_integer_type<2*sizeof (T), true>::type mptype; + return truncate_int (static_cast (x) + * static_cast (y)); + } + + // Division. + static T + div (T x, T y) + { + T z; + if (y == 0) + { + octave_int_base::ftrunc = true; + if (x < 0) + z = octave_int_base::min_val (); + else if (x != 0) + z = octave_int_base::max_val (); + else + z = 0; + } + else if (y < 0) + { + // This is a special case that overflows as well. + if (y == -1 && x == octave_int_base::min_val ()) + { + octave_int_base::ftrunc = true; + z = octave_int_base::max_val (); + } + else + { + z = x / y; + T w = -std::abs (x % y); // Can't overflow, but std::abs (x) can! + if (w <= y - w) + z -= 1 - (signbit (x) << 1); + } + } + else + { + z = x / y; + T w = std::abs (x % y); // Can't overflow, but std::abs (x) can! + if (w >= y - w) + z += 1 - (signbit (x) << 1); + } + return z; + } + }; +// Special handler for 64-bit integer multiply. +template <> +OCTAVE_API int64_t +octave_int_arith_base::mul (int64_t, int64_t); + +// This class simply selects the proper arithmetics. +template +class octave_int_arith + : public octave_int_arith_base::is_signed> +{}; + template class -octave_int +octave_int : public octave_int_base { public: - enum conv_error_type - { - int_truncate = 1, - conv_nan = 2, - conv_non_int = 4, - math_truncate = 8 - }; - typedef T val_type; octave_int (void) : ival () { } - template - octave_int (U i) : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (i, T)) { } + octave_int (T i) : ival (i) { } - octave_int (double d) : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (xround (d), T)) - { - if (xisnan (d)) - conv_flag |= conv_nan; - else - conv_flag |= (d != xround (d) ? conv_non_int : 0); - } + octave_int (double d) : ival (octave_int_base::convert_real (d)) { } + + octave_int (float d) : ival (octave_int_base::convert_real (d)) { } octave_int (bool b) : ival (b) { } template + octave_int (const U& i) : ival(octave_int_base::truncate_int (i)) { } + + template octave_int (const octave_int& i) - : ival (OCTAVE_INT_CONV_FIT_TO_RANGE (i.value (), T)) { } + : ival (octave_int_base::truncate_int (i.value ())) { } octave_int (const octave_int& i) : ival (i.ival) { } @@ -332,8 +698,6 @@ return *this; } - ~octave_int (void) { } - T value (void) const { return ival; } const unsigned char * iptr (void) const @@ -341,23 +705,6 @@ bool operator ! (void) const { return ! ival; } - octave_int operator + (void) const { return *this; } - - octave_int operator - (void) const - { - // Can't just return -ival because signed types are not - // symmetric, which causes things like -intmin("int32") to be the - // same as intmin("int32") instead of intmax("int32") (which is - // what we should get with saturation semantics). - if (std::numeric_limits::is_signed) - return OCTAVE_INT_FIT_TO_RANGE (- static_cast (ival), T); - else - { - conv_flag |= math_truncate; - return 0; - } - } - bool bool_value (void) const { return static_cast (value ()); } char char_value (void) const { return static_cast (value ()); } @@ -374,188 +721,110 @@ operator float (void) const { return float_value (); } - octave_int& operator += (const octave_int& x) - { - double t = static_cast (value ()); - double tx = static_cast (x.value ()); - ival = OCTAVE_INT_FIT_TO_RANGE (t + tx, T); - return *this; - } + octave_int + operator + () const + { return *this; } + + // unary operators & mappers +#define OCTAVE_INT_UN_OP(OPNAME,NAME) \ + inline octave_int \ + OPNAME () const \ + { return octave_int_arith::NAME (ival); } - octave_int& operator -= (const octave_int& x) - { - double t = static_cast (value ()); - double tx = static_cast (x.value ()); - ival = OCTAVE_INT_FIT_TO_RANGE (t - tx, T); - return *this; - } + OCTAVE_INT_UN_OP(operator -, minus) + OCTAVE_INT_UN_OP(abs, abs) + OCTAVE_INT_UN_OP(signum, signum) + +# undef OCTAVE_INT_UN_OP - octave_int& operator *= (const octave_int& x) - { - double t = static_cast (value ()); - double tx = static_cast (x.value ()); - ival = OCTAVE_INT_FIT_TO_RANGE (t * tx, T); - return *this; +// Homogeneous binary integer operations. +#define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \ + inline octave_int \ + operator OP (const ARGT& y) const \ + { return octave_int_arith::NAME (ival, y); } \ + inline octave_int& \ + operator OP##= (const ARGT& y) \ + { \ + ival = octave_int_arith::NAME (ival, y); \ + return *this; \ } - octave_int& operator /= (const octave_int& x) - { - double t = static_cast (value ()); - double tx = static_cast (x.value ()); - double r = (t == 0 && tx == 0) ? 0 : xround (t / tx); - ival = OCTAVE_INT_FIT_TO_RANGE (r, T); - return *this; - } - - template - octave_int& operator <<= (const T2& x) - { - ival = ival << x; - return *this; - } + OCTAVE_INT_BIN_OP(+, add, octave_int) + OCTAVE_INT_BIN_OP(-, sub, octave_int) + OCTAVE_INT_BIN_OP(*, mul, octave_int) + OCTAVE_INT_BIN_OP(/, div, octave_int) + OCTAVE_INT_BIN_OP(<<, lshift, int) + OCTAVE_INT_BIN_OP(>>, rshift, int) - // Use helper functions in the operator >>=, abs, and signum - // functions to avoid "comparison of unsigned expression < 0 is - // always false" warnings from GCC when instantiating these funtions - // for unsigned types. - - template - octave_int& operator >>= (const T2& x) - { - octave_int_helper::is_signed>::rshift_eq (ival, x); - return *this; - } - - octave_int abs (void) const - { - return octave_int_helper::is_signed>::abs (value ()); - } - - octave_int signum (void) const - { - return octave_int_helper::is_signed>::signum (value ()); - } +# undef OCTAVE_INT_BIN_OP octave_int min (void) const { return std::numeric_limits::min (); } octave_int max (void) const { return std::numeric_limits::max (); } - static int nbits (void) { return sizeof (T) * CHAR_BIT; } + static int nbits (void) { return std::numeric_limits::digits; } static int byte_size (void) { return sizeof(T); } - static int get_conv_flag () { return conv_flag; } - static bool get_trunc_flag () { return (conv_flag & int_truncate); } - static bool get_nan_flag () { return (conv_flag & conv_nan); } - static bool get_non_int_flag () { return (conv_flag & conv_non_int); } - static bool get_math_trunc_flag () { return (conv_flag & math_truncate); } - static void clear_conv_flag () { conv_flag = 0; } - - static const char *type_name () { return "unknown type"; } + static const char *type_name (); // Unsafe. This function exists to support the MEX interface. // You should not use it anywhere else. void *mex_get_data (void) const { return const_cast (&ival); } - static int conv_flag; - private: T ival; }; -template int octave_int::conv_flag = 0; +// No mixed integer binary operations! + +template +inline bool +xisnan (const octave_int&) +{ return false; } + +// TODO: Can/should any of these be inline? template -bool -xisnan (const octave_int&) -{ - return false; -} +extern OCTAVE_API octave_int +pow (const octave_int&, const octave_int&); + +template +extern OCTAVE_API octave_int +pow (const double& a, const octave_int& b); + +template +extern OCTAVE_API octave_int +pow (const octave_int& a, const double& b); template -octave_int -pow (const octave_int& a, const octave_int& b) -{ - octave_int retval; - - octave_int zero = octave_int (0); - octave_int one = octave_int (1); - - if (b == zero || a == one) - retval = one; - else if (b < zero) - { - if (std::numeric_limits::is_signed && a.value () == -1) - retval = (b.value () % 2) ? a : one; - else - retval = zero; - } - else - { - octave_int a_val = a; - octave_int b_val = b; - - retval = a; - - b_val -= 1; - - while (b_val != zero) - { - if ((b_val & one) != zero) - retval = retval * a_val; - - b_val = b_val >> 1; - - if (b_val > zero) - a_val = a_val * a_val; - } - } - - return retval; -} +extern OCTAVE_API octave_int +powf (const float& a, const octave_int& b); template -octave_int -pow (double a, const octave_int& b) -{ - double tb = static_cast (b.value ()); - double r = pow (a, tb); - r = __lo_ieee_isnan (r) ? 0 : xround (r); - return OCTAVE_INT_FIT_TO_RANGE (r, T); -} +extern OCTAVE_API octave_int +powf (const octave_int& a, const float& b); + +// Binary relations -template -octave_int -pow (const octave_int& a, double b) -{ - double ta = static_cast (a.value ()); - double r = pow (ta, b); - r = __lo_ieee_isnan (r) ? 0 : xround (r); - return OCTAVE_INT_FIT_TO_RANGE (r, T); -} +#define OCTAVE_INT_CMP_OP(OP, NAME) \ + template \ + inline bool \ + operator OP (const octave_int& x, const octave_int& y) \ + { return octave_int_cmp_op::op \ + (x.value (), y.value ()); } + +OCTAVE_INT_CMP_OP (<, lt) +OCTAVE_INT_CMP_OP (<=, le) +OCTAVE_INT_CMP_OP (>, gt) +OCTAVE_INT_CMP_OP (>=, ge) +OCTAVE_INT_CMP_OP (==, eq) +OCTAVE_INT_CMP_OP (!=, ne) + +# undef OCTAVE_INT_CMP_OP template -octave_int -powf (float a, const octave_int& b) -{ - float tb = static_cast (b.value ()); - float r = powf (a, tb); - r = __lo_ieee_float_isnan (r) ? 0 : xround (r); - return OCTAVE_INT_FIT_TO_RANGE (r, T); -} - -template -octave_int -powf (const octave_int& a, float b) -{ - float ta = static_cast (a.value ()); - float r = pow (ta, b); - r = __lo_ieee_float_isnan (r) ? 0 : xround (r); - return OCTAVE_INT_FIT_TO_RANGE (r, T); -} - -template -std::ostream& +inline std::ostream& operator << (std::ostream& os, const octave_int& ival) { os << ival.value (); @@ -563,7 +832,7 @@ } template -std::istream& +inline std::istream& operator >> (std::istream& is, octave_int& ival) { T tmp = 0; @@ -572,237 +841,21 @@ return is; } -// specialize the widening conversions to make them faster -// gosh. the syntax is tricky! - -#define SPECIALIZE_WIDENING_CONVERSION(T1, T2) \ - template <> template <> \ - inline octave_int::octave_int (T1 i) \ - : ival (i) { } \ - \ - template <> template <> \ - inline octave_int::octave_int (const octave_int& i) \ - : ival (i.value ()) { } - -SPECIALIZE_WIDENING_CONVERSION (int8_t, int16_t) -SPECIALIZE_WIDENING_CONVERSION (int8_t, int32_t) -SPECIALIZE_WIDENING_CONVERSION (int8_t, int64_t) -SPECIALIZE_WIDENING_CONVERSION (int16_t, int32_t) -SPECIALIZE_WIDENING_CONVERSION (int16_t, int64_t) -SPECIALIZE_WIDENING_CONVERSION (int32_t, int64_t) -SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint16_t) -SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint32_t) -SPECIALIZE_WIDENING_CONVERSION (uint8_t, uint64_t) -SPECIALIZE_WIDENING_CONVERSION (uint16_t, uint32_t) -SPECIALIZE_WIDENING_CONVERSION (uint16_t, uint64_t) -SPECIALIZE_WIDENING_CONVERSION (uint32_t, uint64_t) - -// declare type names -#define DECLARE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ - template <> \ - inline const char * \ - octave_int::type_name () { return TYPENAME; } - -DECLARE_OCTAVE_INT_TYPENAME (int8_t, "int8") -DECLARE_OCTAVE_INT_TYPENAME (int16_t, "int16") -DECLARE_OCTAVE_INT_TYPENAME (int32_t, "int32") -DECLARE_OCTAVE_INT_TYPENAME (int64_t, "int64") -DECLARE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") -DECLARE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") -DECLARE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") -DECLARE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") - -typedef octave_int octave_int8; -typedef octave_int octave_int16; -typedef octave_int octave_int32; -typedef octave_int octave_int64; - -typedef octave_int octave_uint8; -typedef octave_int octave_uint16; -typedef octave_int octave_uint32; -typedef octave_int octave_uint64; - -#define OCTAVE_INT_BIN_OP(OP) \ - template \ - octave_int::TR> \ - operator OP (const octave_int& x, const octave_int& y) \ - { \ - double tx = static_cast (x.value ()); \ - double ty = static_cast (y.value ()); \ - double r = tx OP ty; \ - return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); \ - } - -OCTAVE_INT_BIN_OP (+) -OCTAVE_INT_BIN_OP (-) -OCTAVE_INT_BIN_OP (*) - -template -octave_int::TR> -operator / (const octave_int& x, const octave_int& y) -{ - double tx = static_cast (x.value ()); - double ty = static_cast (y.value ()); - double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty); - return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2); -} - -#define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ - template \ - octave_int \ - operator OP (const octave_int& x, double y) \ - { \ - double tx = static_cast (x.value ()); \ - double r = xround (tx OP y); \ - r = __lo_ieee_isnan (r) ? 0 : xround (r); \ - return OCTAVE_INT_FIT_TO_RANGE (r, T); \ - } - -OCTAVE_INT_DOUBLE_BIN_OP (+) -OCTAVE_INT_DOUBLE_BIN_OP (-) -OCTAVE_INT_DOUBLE_BIN_OP (*) -OCTAVE_INT_DOUBLE_BIN_OP (/) - -#define OCTAVE_DOUBLE_INT_BIN_OP(OP) \ - template \ - octave_int \ - operator OP (double x, const octave_int& y) \ - { \ - double ty = static_cast (y.value ()); \ - double r = x OP ty; \ - r = __lo_ieee_isnan (r) ? 0 : xround (r); \ - return OCTAVE_INT_FIT_TO_RANGE (r, T); \ - } - -OCTAVE_DOUBLE_INT_BIN_OP (+) -OCTAVE_DOUBLE_INT_BIN_OP (-) -OCTAVE_DOUBLE_INT_BIN_OP (*) -OCTAVE_DOUBLE_INT_BIN_OP (/) - -#define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ - template \ - bool \ - operator OP (const octave_int& x, const double& y) \ - { \ - double tx = static_cast (x.value ()); \ - return tx OP y; \ - } - -OCTAVE_INT_DOUBLE_CMP_OP (<) -OCTAVE_INT_DOUBLE_CMP_OP (<=) -OCTAVE_INT_DOUBLE_CMP_OP (>=) -OCTAVE_INT_DOUBLE_CMP_OP (>) -OCTAVE_INT_DOUBLE_CMP_OP (==) -OCTAVE_INT_DOUBLE_CMP_OP (!=) - -#define OCTAVE_DOUBLE_INT_CMP_OP(OP) \ - template \ - bool \ - operator OP (const double& x, const octave_int& y) \ - { \ - double ty = static_cast (y.value ()); \ - return x OP ty; \ - } - -OCTAVE_DOUBLE_INT_CMP_OP (<) -OCTAVE_DOUBLE_INT_CMP_OP (<=) -OCTAVE_DOUBLE_INT_CMP_OP (>=) -OCTAVE_DOUBLE_INT_CMP_OP (>) -OCTAVE_DOUBLE_INT_CMP_OP (==) -OCTAVE_DOUBLE_INT_CMP_OP (!=) - -#define OCTAVE_INT_FLOAT_BIN_OP(OP) \ - template \ - octave_int \ - operator OP (const octave_int& x, float y) \ - { \ - double tx = static_cast (x.value ()); \ - double r = xround (tx OP y); \ - r = __lo_ieee_isnan (r) ? 0 : xround (r); \ - return OCTAVE_INT_FIT_TO_RANGE (r, T); \ - } - -OCTAVE_INT_FLOAT_BIN_OP (+) -OCTAVE_INT_FLOAT_BIN_OP (-) -OCTAVE_INT_FLOAT_BIN_OP (*) -OCTAVE_INT_FLOAT_BIN_OP (/) - -#define OCTAVE_FLOAT_INT_BIN_OP(OP) \ - template \ - octave_int \ - operator OP (float x, const octave_int& y) \ - { \ - double ty = static_cast (y.value ()); \ - double r = x OP ty; \ - r = __lo_ieee_isnan (r) ? 0 : xround (r); \ - return OCTAVE_INT_FIT_TO_RANGE (r, T); \ - } - -OCTAVE_FLOAT_INT_BIN_OP (+) -OCTAVE_FLOAT_INT_BIN_OP (-) -OCTAVE_FLOAT_INT_BIN_OP (*) -OCTAVE_FLOAT_INT_BIN_OP (/) - -#define OCTAVE_INT_FLOAT_CMP_OP(OP) \ - template \ - bool \ - operator OP (const octave_int& x, const float& y) \ - { \ - double tx = static_cast (x.value ()); \ - return tx OP y; \ - } - -OCTAVE_INT_FLOAT_CMP_OP (<) -OCTAVE_INT_FLOAT_CMP_OP (<=) -OCTAVE_INT_FLOAT_CMP_OP (>=) -OCTAVE_INT_FLOAT_CMP_OP (>) -OCTAVE_INT_FLOAT_CMP_OP (==) -OCTAVE_INT_FLOAT_CMP_OP (!=) - -#define OCTAVE_FLOAT_INT_CMP_OP(OP) \ - template \ - bool \ - operator OP (const float& x, const octave_int& y) \ - { \ - double ty = static_cast (y.value ()); \ - return x OP ty; \ - } - -OCTAVE_FLOAT_INT_CMP_OP (<) -OCTAVE_FLOAT_INT_CMP_OP (<=) -OCTAVE_FLOAT_INT_CMP_OP (>=) -OCTAVE_FLOAT_INT_CMP_OP (>) -OCTAVE_FLOAT_INT_CMP_OP (==) -OCTAVE_FLOAT_INT_CMP_OP (!=) +// Bitwise operations #define OCTAVE_INT_BITCMP_OP(OP) \ template \ octave_int \ operator OP (const octave_int& x, const octave_int& y) \ - { \ - return x.value () OP y.value (); \ - } + { return x.value () OP y.value (); } OCTAVE_INT_BITCMP_OP (&) OCTAVE_INT_BITCMP_OP (|) OCTAVE_INT_BITCMP_OP (^) -template -octave_int -operator << (const octave_int& x, const T2& y) -{ - octave_int retval = x; - return retval <<= y; -} +# undef OCTAVE_INT_BITCMP_OP -template -octave_int -operator >> (const octave_int& x, const T2& y) -{ - octave_int retval = x; - return retval >>= y; -} - +// General bit shift. template octave_int bitshift (const octave_int& a, int n, @@ -816,127 +869,115 @@ return a; } -#define OCTAVE_INT_CMP_OP(OP) \ - template \ - bool \ - operator OP (const octave_int& x, const octave_int& y) \ - { \ - return x.value () OP y.value (); \ - } +typedef octave_int octave_int8; +typedef octave_int octave_int16; +typedef octave_int octave_int32; +typedef octave_int octave_int64; -OCTAVE_INT_CMP_OP (<) -OCTAVE_INT_CMP_OP (<=) -OCTAVE_INT_CMP_OP (>=) -OCTAVE_INT_CMP_OP (>) -OCTAVE_INT_CMP_OP (==) -OCTAVE_INT_CMP_OP (!=) - -// The following apply if the unsigned type is at least as wide as the -// signed type (then we can cast postive signed values to the unsigned -// type and compare). - -#define OCTAVE_US_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ - template <> \ - bool \ - OCTAVE_API operator OP (const octave_int& lhs, const octave_int& rhs); +typedef octave_int octave_uint8; +typedef octave_int octave_uint16; +typedef octave_int octave_uint32; +typedef octave_int octave_uint64; -#define OCTAVE_US_TYPE1_CMP_OP_DECLS(UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (<, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (<=, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (>=, true, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (>, true, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (==, false, UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECL (!=, true, UT, ST) - -#define OCTAVE_SU_TYPE1_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ +#define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ + template \ + inline octave_int \ + operator OP (const octave_int& x, const double& y) \ + { return octave_int (static_cast (x) OP y); } \ + template <> \ + OCTAVE_API octave_int64 \ + operator OP (const octave_int64&, const double&); \ template <> \ - bool \ - OCTAVE_API operator OP (const octave_int& lhs, const octave_int& rhs); + OCTAVE_API octave_uint64 \ + operator OP (const octave_uint64&, const double&); \ + template \ + inline octave_int \ + operator OP (const double& x, const octave_int& y) \ + { return octave_int (x OP static_cast (y)); } \ + template <> \ + OCTAVE_API octave_int64 \ + operator OP (const double&, const octave_int64&); \ + template <> \ + OCTAVE_API octave_uint64 \ + operator OP (const double&, const octave_uint64&); -#define OCTAVE_SU_TYPE1_CMP_OP_DECLS(ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (<, true, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (<=, true, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (>=, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (>, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (==, false, ST, UT) \ - OCTAVE_SU_TYPE1_CMP_OP_DECL (!=, true, ST, UT) -#define OCTAVE_TYPE1_CMP_OP_DECLS(UT, ST) \ - OCTAVE_US_TYPE1_CMP_OP_DECLS (UT, ST) \ - OCTAVE_SU_TYPE1_CMP_OP_DECLS (ST, UT) +OCTAVE_INT_DOUBLE_BIN_OP (+) +OCTAVE_INT_DOUBLE_BIN_OP (-) +OCTAVE_INT_DOUBLE_BIN_OP (*) +OCTAVE_INT_DOUBLE_BIN_OP (/) -OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int8_t) -OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int16_t) -OCTAVE_TYPE1_CMP_OP_DECLS (uint32_t, int32_t) - -OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int8_t) -OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int16_t) -OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int32_t) -OCTAVE_TYPE1_CMP_OP_DECLS (uint64_t, int64_t) +# undef OCTAVE_INT_DOUBLE_BIN_OP -// The following apply if the signed type is wider than the unsigned -// type (then we can cast unsigned values to the signed type and -// compare if the signed value is positive). - -#define OCTAVE_US_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, UT, ST) \ +#define OCTAVE_INT_DOUBLE_CMP_OP(OP) \ + template \ + inline bool \ + operator OP (const octave_int& x, const double& y) \ + { return static_cast (x.value ()) OP y; } \ + template <> \ + OCTAVE_API bool \ + operator OP (const octave_int64&, const double&); \ template <> \ - bool \ - OCTAVE_API operator OP (const octave_int& lhs, const octave_int& rhs); + OCTAVE_API bool \ + operator OP (const octave_uint64&, const double&); \ + template \ + inline bool \ + operator OP (const double& x, const octave_int& y) \ + { return x OP static_cast (y.value ()); } \ + template <> \ + OCTAVE_API bool \ + operator OP (const double&, const octave_int64&); \ + template <> \ + OCTAVE_API bool \ + operator OP (const double&, const octave_uint64&); -#define OCTAVE_US_TYPE2_CMP_OP_DECLS(ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (<, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (<=, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (>=, true, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (>, true, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ - OCTAVE_US_TYPE2_CMP_OP_DECL (!=, true, ST, UT) -#define OCTAVE_SU_TYPE2_CMP_OP_DECL(OP, LTZ_VAL, ST, UT) \ - template <> \ - bool \ - OCTAVE_API operator OP (const octave_int& lhs, const octave_int& rhs); +OCTAVE_INT_DOUBLE_CMP_OP (<) +OCTAVE_INT_DOUBLE_CMP_OP (<=) +OCTAVE_INT_DOUBLE_CMP_OP (>=) +OCTAVE_INT_DOUBLE_CMP_OP (>) +OCTAVE_INT_DOUBLE_CMP_OP (==) +OCTAVE_INT_DOUBLE_CMP_OP (!=) -#define OCTAVE_SU_TYPE2_CMP_OP_DECLS(ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (<, true, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (<=, true, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (>=, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (>, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (==, false, ST, UT) \ - OCTAVE_SU_TYPE2_CMP_OP_DECL (!=, true, ST, UT) +# undef OCTAVE_INT_DOUBLE_CMP_OP + +// Floats are handled by simply converting to doubles. -#define OCTAVE_TYPE2_CMP_OP_DECLS(UT, ST) \ - OCTAVE_US_TYPE2_CMP_OP_DECLS (UT, ST) \ - OCTAVE_SU_TYPE2_CMP_OP_DECLS (ST, UT) +#define OCTAVE_INT_FLOAT_BIN_OP(OP) \ + template \ + inline octave_int \ + operator OP (const octave_int& x, float y) \ + { return x OP static_cast (y); } \ + template \ + inline octave_int \ + operator OP (float x, const octave_int& y) \ + { return static_cast (x) OP y; } -OCTAVE_TYPE2_CMP_OP_DECLS (uint32_t, int64_t) +OCTAVE_INT_FLOAT_BIN_OP (+) +OCTAVE_INT_FLOAT_BIN_OP (-) +OCTAVE_INT_FLOAT_BIN_OP (*) +OCTAVE_INT_FLOAT_BIN_OP (/) + +# undef OCTAVE_INT_FLOAT_BIN_OP -#undef OCTAVE_INT_BINOP_TRAIT -#undef OCTAVE_US_S_FTR -#undef OCTAVE_US_S_FTR_FCNS -#undef OCTAVE_S_US_FTR -#undef OCTAVE_S_US_FTR_FCNS -#undef OCTAVE_INT_FIT_TO_RANGE -#undef OCTAVE_INT_MIN_VAL2 -#undef OCTAVE_INT_MAX_VAL2 -#undef OCTAVE_INT_FIT_TO_RANGE2 -#undef OCTAVE_INT_BIN_OP -#undef OCTAVE_INT_DOUBLE_BIN_OP -#undef OCTAVE_DOUBLE_INT_BIN_OP -#undef OCTAVE_INT_DOUBLE_CMP_OP -#undef OCTAVE_DOUBLE_INT_CMP_OP -#undef OCTAVE_INT_BITCMP_OP -#undef OCTAVE_INT_CMP_OP -#undef OCTAVE_US_TYPE1_CMP_OP_DECL -#undef OCTAVE_US_TYPE1_CMP_OP_DECLS -#undef OCTAVE_SU_TYPE1_CMP_OP_DECL -#undef OCTAVE_SU_TYPE1_CMP_OP_DECLS -#undef OCTAVE_TYPE1_CMP_OP_DECLS -#undef OCTAVE_US_TYPE2_CMP_OP_DECL -#undef OCTAVE_US_TYPE2_CMP_OP_DECLS -#undef OCTAVE_SU_TYPE2_CMP_OP_DECL -#undef OCTAVE_SU_TYPE2_CMP_OP_DECLS -#undef OCTAVE_TYPE2_CMP_OP_DECLS +#define OCTAVE_INT_FLOAT_CMP_OP(OP) \ + template \ + inline bool \ + operator OP (const octave_int& x, const float& y) \ + { return x OP static_cast (y); } \ + template \ + bool \ + operator OP (const float& x, const octave_int& y) \ + { return static_cast (x) OP y; } +OCTAVE_INT_FLOAT_CMP_OP (<) +OCTAVE_INT_FLOAT_CMP_OP (<=) +OCTAVE_INT_FLOAT_CMP_OP (>=) +OCTAVE_INT_FLOAT_CMP_OP (>) +OCTAVE_INT_FLOAT_CMP_OP (==) +OCTAVE_INT_FLOAT_CMP_OP (!=) + +# undef OCTAVE_INT_FLOAT_CMP_OP #endif /*