26#if ! defined (octave_oct_inttypes_h)
27#define octave_oct_inttypes_h 1
29#include "octave-config.h"
41#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
47 inline long double round (
long double x)
49 return std::roundl (
x);
52 inline long double isnan (
long double x)
54 return isnan (
static_cast<double> (
x));
69 return (
x >= 0 ?
x : -
x);
74template <
int qsize,
bool q
signed>
87#define OCTAVE_REGISTER_INT_TYPE(TYPE) \
89 class query_integer_type<sizeof (TYPE), \
90 std::numeric_limits<TYPE>::is_signed> \
94 static const bool registered = true; \
109#undef OCTAVE_REGISTER_INT_TYPE
122 template <
typename T1,
typename T2>
126 static const bool pint = (
sizeof (T1) <
sizeof (
int)
127 &&
sizeof (T2) <
sizeof (
int));
129 static const bool t1sig = std::numeric_limits<T1>::is_signed;
130 static const bool t2sig = std::numeric_limits<T2>::is_signed;
138 : (
sizeof (T2) >
sizeof (T1) ?
sizeof (T2) :
sizeof (T1)));
147 template <
typename xop,
int size>
157 return xop::op (
x, y);
162 return xop::op (
x, y);
167 return (
x < 0) ? xop::ltval : xop::op (
static_cast<utype> (
x), y);
172 return (y < 0) ? xop::gtval : xop::op (
x,
static_cast<utype> (y));
183#define OCTAVE_REGISTER_INT_CMP_OP(NM, OP) \
188 static const bool ltval = (0 OP 1); \
189 static const bool gtval = (1 OP 0); \
191 template <typename T> \
192 static bool op (T x, T y) { return x OP y; } \
202#undef OCTAVE_REGISTER_INT_CMP_OP
207#define OCTAVE_REGISTER_INT_CONST_OP(NM, VALUE) \
212 static const bool ltval = VALUE; \
213 static const bool gtval = VALUE; \
215 template <typename T> \
216 static bool op (T, T) { return VALUE; } \
222#undef OCTAVE_REGISTER_INT_CONST_OP
226 template <
typename xop,
typename T1,
typename T2>
233 return uiop<xop,
sizeof (PT1)>
::op (
static_cast<PT1
> (
x),
234 static_cast<PT2
> (y));
241 template <
typename xop,
typename T>
242 static bool mop (T
x,
double y)
244 return xop::op (
static_cast<double> (
x), y);
247 template <
typename xop,
typename T>
248 static bool mop (
double x, T y)
250 return xop::op (
x,
static_cast<double> (y));
253#if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
255# define OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_CMP_OPS(T) \
256 template <typename xop> \
257 static OCTAVE_API bool external_mop (double, T); \
259 template <typename xop> \
260 static OCTAVE_API bool external_mop (T, double)
262 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_CMP_OPS (int64_t);
263 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_CMP_OPS (uint64_t);
270#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
272# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
274# define OCTAVE_DEFINE_LONG_DOUBLE_INT_CMP_OP(T) \
275 template <typename xop> \
276 static bool mop (double x, T y) \
278 return external_mop<xop> (x, y); \
281 template <typename xop> \
282 static bool mop (T x, double y) \
284 return external_mop<xop> (x, y); \
289# define OCTAVE_DEFINE_LONG_DOUBLE_INT_CMP_OP(T) \
290 template <typename xop> \
291 static bool mop (double x, T y) \
293 return xop::op (static_cast<long double> (x), \
294 static_cast<long double> (y)); \
297 template <typename xop> \
298 static bool mop (T x, double y) \
300 return xop::op (static_cast<long double> (x), \
301 static_cast<long double> (y)); \
314# define OCTAVE_DEFINE_LONG_DOUBLE_INT_CMP_OP(T) \
315 template <typename xop> \
316 static OCTAVE_API bool emulate_mop (double, T); \
318 template <typename xop> \
319 static bool mop (double x, T y) \
321 return emulate_mop<xop> (x, y); \
324 template <typename xop> \
325 static OCTAVE_API bool emulate_mop (T, double); \
327 template <typename xop> \
328 static bool mop (T x, double y) \
330 return emulate_mop<xop> (x, y); \
338#undef OCTAVE_DEFINE_LONG_DOUBLE_INT_CMP_OP
354 template <
typename S>
360 static const bool t_is_signed = std::numeric_limits<T>::is_signed;
361 static const bool s_is_signed = std::numeric_limits<S>::is_signed;
363 static const int t_size =
sizeof (T);
364 static const int s_size =
sizeof (S);
366 static const bool omit_chk_min
367 = (! s_is_signed || (t_is_signed && t_size >= s_size));
369 static const bool omit_chk_max
371 || (t_size == s_size && (! t_is_signed || s_is_signed)));
386 if (chk_min::op (value,
static_cast<S
> (
min_val ())))
388 else if (chk_max::op (value,
static_cast<S
> (
max_val ())))
391 return static_cast<T
> (value);
398 template <
typename S>
409 val *= (
static_cast<S
> (1) - (std::numeric_limits<S>::epsilon () / 2));
418 template <
typename S>
428template <
typename T,
bool is_
signed>
442 static T
signum (T
x) {
return x ?
static_cast<T
> (1) :
static_cast<T
> (0); }
450 static T
minus (T) {
return static_cast<T
> (0); }
488 *
static_cast<mptype
> (y));
512 static T
rem (T
x, T y) {
return y != 0 ?
x % y : 0; }
516 static T
mod (T
x, T y) {
return y != 0 ?
x % y :
x; }
519#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
523# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
526octave_external_uint64_uint64_mul (uint64_t, uint64_t);
536 long double p =
static_cast<long double> (
x) *
static_cast<long double> (y);
541 retval =
static_cast<uint64_t
> (p);
550# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
551 return octave_external_uint64_uint64_mul (
x, y);
553 return mul_internal (
x, y);
590 : ((
x < 0) ? -
x :
x));
597 return ((
x > 0) ? 1 : 0) - __signbit (
x);
657 *
static_cast<mptype
> (y));
686 z -= 1 - (__signbit (
x) << 1);
699 z += 1 - (__signbit (
x) << 1);
708 return y != 0 ?
x % y : 0;
718 return (r == 0) ? 0 : (((r < 0) != (y < 0)) ? r + y : r);
725#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
729# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
732octave_external_int64_int64_mul (int64_t, int64_t);
742 long double p =
static_cast<long double> (
x) *
static_cast<long double> (y);
749 retval =
static_cast<int64_t
> (p);
758# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
759 return octave_external_int64_int64_mul (
x, y);
761 return mul_internal (
x, y);
792#if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
807#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
816 template <
typename U>
820 template <
typename U>
832 const unsigned char *
iptr (
void)
const
834 return reinterpret_cast<const unsigned char *
> (&
m_ival);
847 operator T (
void)
const {
return value (); }
852#define OCTAVE_INT_UN_OP(OPNAME, NAME) \
853 inline octave_int<T> \
856 return octave_int_arith<T>::NAME (m_ival); \
863#undef OCTAVE_INT_UN_OP
866#define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \
867 inline octave_int<T> \
868 operator OP (const ARGT& y) const \
870 return octave_int_arith<T>::NAME (m_ival, y); \
873 inline octave_int<T>& \
874 operator OP##= (const ARGT& y) \
876 m_ival = octave_int_arith<T>::NAME (m_ival, y); \
888#undef OCTAVE_INT_BIN_OP
893 static int nbits (
void) {
return std::numeric_limits<T>::digits; }
927 template <
typename T>
972#define OCTAVE_INT_CMP_OP(OP, NAME) \
973 template <typename T1, typename T2> \
975 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \
977 return octave_int_cmp_op::op<octave_int_cmp_op::NAME, T1, T2> (x.value (), y.value ()); \
987#undef OCTAVE_INT_CMP_OP
1017 os << static_cast<int> (ival.
value ());
1026 os << static_cast<unsigned int> (ival.
value ());
1037 ival =
static_cast<int8_t
> (tmp);
1046 unsigned int tmp = 0;
1048 ival =
static_cast<uint8_t
> (tmp);
1055#define OCTAVE_INT_BITCMP_OP(OP) \
1056 template <typename T> \
1058 operator OP (const octave_int<T>& x, const octave_int<T>& y) \
1060 return x.value () OP y.value (); \
1067#undef OCTAVE_INT_BITCMP_OP
1070template <
typename T>
1076 return (a << n) & mask;
1078 return (a >> -n) & mask;
1083#if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
1085# define OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP(T, OP) \
1086 extern OCTAVE_API T \
1087 external_double_ ## T ## _ ## OP (double x, T y); \
1089 extern OCTAVE_API T \
1090 external_ ## T ## _double_ ## OP (T x, double y)
1092# define OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS(T) \
1093 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, add); \
1094 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, sub); \
1095 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, mul); \
1096 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, div)
1098 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS (
octave_int64);
1099 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS (
octave_uint64);
1103#define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1104 template <typename T> \
1105 inline octave_int<T> \
1106 operator OP (const octave_int<T>& x, const double& y) \
1108 return octave_int<T> (static_cast<double> (x) OP y); \
1111 template <typename T> \
1112 inline octave_int<T> \
1113 operator OP (const double& x, const octave_int<T>& y) \
1115 return octave_int<T> (x OP static_cast<double> (y)); \
1118#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
1122# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
1124# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1125 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1128 inline octave_int64 \
1129 operator OP (const double& x, const octave_int64& y) \
1131 return external_double_octave_int64_ ## NAME (x, y); \
1135 inline octave_uint64 \
1136 operator OP (const double& x, const octave_uint64& y) \
1138 return external_double_octave_uint64_ ## NAME (x, y); \
1142 inline octave_int64 \
1143 operator OP (const octave_int64& x, const double& y) \
1145 return external_octave_int64_double_ ## NAME (x, y); \
1149 inline octave_uint64 \
1150 operator OP (const octave_uint64& x, const double& y) \
1152 return external_octave_uint64_double_ ## NAME (x, y); \
1157# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1158 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1161 inline octave_int64 \
1162 operator OP (const double& x, const octave_int64& y) \
1164 return octave_int64 (x OP static_cast<long double> (y.value ())); \
1168 inline octave_uint64 \
1169 operator OP (const double& x, const octave_uint64& y) \
1171 return octave_uint64 (x OP static_cast<long double> (y.value ())); \
1175 inline octave_int64 \
1176 operator OP (const octave_int64& x, const double& y) \
1178 return octave_int64 (static_cast<long double> (x.value ()) OP y); \
1182 inline octave_uint64 \
1183 operator OP (const octave_uint64& x, const double& y) \
1185 return octave_uint64 (static_cast<long double> (x.value ()) OP y); \
1194# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1195 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1198 OCTAVE_API octave_int64 \
1199 operator OP (const double&, const octave_int64&); \
1202 OCTAVE_API octave_uint64 \
1203 operator OP (const double&, const octave_uint64&); \
1206 OCTAVE_API octave_int64 \
1207 operator OP (const octave_int64&, const double&); \
1210 OCTAVE_API octave_uint64 \
1211 operator OP (const octave_uint64&, const double&);
1220#undef OCTAVE_INT_DOUBLE_BIN_OP0
1221#undef OCTAVE_INT_DOUBLE_BIN_OP
1222#undef OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP
1223#undef OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS
1225#define OCTAVE_INT_DOUBLE_CMP_OP(OP, NAME) \
1226 template <typename T> \
1228 operator OP (const octave_int<T>& x, const double& y) \
1230 return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x.value (), y); \
1233 template <typename T> \
1235 operator OP (const double& x, const octave_int<T>& y) \
1237 return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x, y.value ()); \
1247#undef OCTAVE_INT_DOUBLE_CMP_OP
1251#define OCTAVE_INT_FLOAT_BIN_OP(OP) \
1252 template <typename T> \
1253 inline octave_int<T> \
1254 operator OP (const octave_int<T>& x, float y) \
1256 return x OP static_cast<double> (y); \
1259 template <typename T> \
1260 inline octave_int<T> \
1261 operator OP (float x, const octave_int<T>& y) \
1263 return static_cast<double> (x) OP y; \
1271#undef OCTAVE_INT_FLOAT_BIN_OP
1273#define OCTAVE_INT_FLOAT_CMP_OP(OP) \
1274 template <typename T> \
1276 operator OP (const octave_int<T>& x, const float& y) \
1278 return x OP static_cast<double> (y); \
1281 template <typename T> \
1283 operator OP (const float& x, const octave_int<T>& y) \
1285 return static_cast<double> (x) OP y; \
1295#undef OCTAVE_INT_FLOAT_CMP_OP
1297template <
typename T>
1301 const T xv =
x.value ();
1302 const T yv = y.
value ();
1307template <
typename T>
1311 const T xv =
x.value ();
1312 const T yv = y.
value ();
1319#define OCTAVE_INT_IDX_TYPE_BIN_OP(OP) \
1320 template <typename T> \
1321 inline octave_int<T> \
1322 operator OP (const octave_int<T>& x, octave_idx_type y) \
1324 return x OP octave_int<T> (y); \
1327 template <typename T> \
1328 inline octave_int<T> \
1329 operator OP (octave_idx_type x, const octave_int<T>& y) \
1331 return octave_int<T> (x) OP y; \
1339#undef OCTAVE_INT_IDX_TYPE_BIN_OP
charNDArray max(char d, const charNDArray &m)
charNDArray min(char d, const charNDArray &m)
static T lshift(T x, int n)
static T mul_internal(T x, T y)
static T rshift(T x, int n)
static T rshift(T x, int n)
static T mul_internal(T x, T y)
query_integer_type< sizeof(T), false >::type UT
static T lshift(T x, int n)
uint64_t mul_internal(uint64_t x, uint64_t y)
static T truncate_int(const S &value)
static S compute_threshold(S val, T orig_val)
static OCTAVE_API T convert_real(const S &value)
query_integer_type< psize, psig >::type type
static bool op(stype x, stype y)
static bool op(stype x, utype y)
static bool op(utype x, stype y)
query_integer_type< size, true >::type stype
query_integer_type< size, false >::type utype
static bool op(utype x, utype y)
static bool op(T1 x, T2 y)
static bool mop(T x, double y)
static bool mop(double x, T y)
static int byte_size(void)
~octave_int(void)=default
bool operator!(void) const
octave_int< T > abs() const
const unsigned char * iptr(void) const
static octave_int< T > max(void)
bool bool_value(void) const
static octave_int< T > min(void)
octave_int< T > signum() const
static const octave_int s_zero
char char_value(void) const
static const octave_int s_one
double double_value(void) const
float float_value(void) const
octave_int & operator=(const octave_int< T > &)=default
octave_int(const octave_int< U > &i)
octave_int(const octave_int< T > &)=default
static const OCTAVE_API char * type_name()
octave_int< T > operator+() const
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
std::complex< double > w(std::complex< double > z, double relerr=0)
std::ostream & operator<<(std::ostream &os, const octave_int< T > &ival)
#define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME)
octave_int< T > rem(const octave_int< T > &x, const octave_int< T > &y)
#define OCTAVE_INT_UN_OP(OPNAME, NAME)
#define OCTAVE_REGISTER_INT_CMP_OP(NM, OP)
std::istream & operator>>(std::istream &is, octave_int< T > &ival)
#define OCTAVE_INT_BIN_OP(OP, NAME, ARGT)
#define OCTAVE_REGISTER_INT_TYPE(TYPE)
#define OCTAVE_INT_BITCMP_OP(OP)
#define OCTAVE_INT_IDX_TYPE_BIN_OP(OP)
#define OCTAVE_INT_DOUBLE_CMP_OP(OP, NAME)
#define OCTAVE_INT_FLOAT_BIN_OP(OP)
OCTAVE_API octave_int< T > pow(const octave_int< T > &, const octave_int< T > &)
#define OCTAVE_REGISTER_INT_CONST_OP(NM, VALUE)
octave_int< T > bitshift(const octave_int< T > &a, int n, const octave_int< T > &mask=std::numeric_limits< T >::max())
#define OCTAVE_INT_CMP_OP(OP, NAME)
octave_int< T > xmin(const octave_int< T > &x, const octave_int< T > &y)
OCTAVE_API octave_int< T > powf(const float &a, const octave_int< T > &b)
#define OCTAVE_DEFINE_LONG_DOUBLE_INT_CMP_OP(T)
#define OCTAVE_INT_FLOAT_CMP_OP(OP)
octave_int< T > mod(const octave_int< T > &x, const octave_int< T > &y)
octave_int< T > xmax(const octave_int< T > &x, const octave_int< T > &y)
static const bool registered