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)
47inline long double round (
long double x)
49 return std::roundl (
x);
52inline long double isnan (
long double x)
54 return isnan (
static_cast<double> (
x));
57OCTAVE_END_NAMESPACE(math)
58OCTAVE_END_NAMESPACE(octave)
70 return (
x >= 0 ?
x : -
x);
75template <
int qsize,
bool q
signed>
88#define OCTAVE_REGISTER_INT_TYPE(TYPE) \
90 class query_integer_type<sizeof (TYPE), \
91 std::numeric_limits<TYPE>::is_signed> \
95 static const bool registered = true; \
110#undef OCTAVE_REGISTER_INT_TYPE
122 template <
typename T1,
typename T2>
126 static constexpr bool s_pint = (
sizeof (T1) <
sizeof (
int)
127 &&
sizeof (T2) <
sizeof (
int));
129 static constexpr bool s_t1sig = std::numeric_limits<T1>::is_signed;
130 static constexpr bool s_t2sig = std::numeric_limits<T2>::is_signed;
132 static constexpr bool s_psig
133 = (s_pint || (
sizeof (T2) >
sizeof (T1) && s_t2sig) || s_t1sig);
135 static constexpr int s_psize
138 : (
sizeof (T2) >
sizeof (T1) ?
sizeof (T2) :
sizeof (T1)));
147 template <
typename xop,
int size>
155 static bool op (utype
x, utype y)
157 return xop::op (
x, y);
160 static bool op (stype
x, stype y)
162 return xop::op (
x, y);
165 static bool op (stype
x, utype y)
167 return (
x < 0) ? xop::ltval : xop::op (
static_cast<utype
> (
x), y);
170 static bool op (utype
x, stype 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 constexpr bool ltval = (0 OP 1); \
189 static constexpr 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 constexpr bool ltval = VALUE; \
213 static constexpr 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>
230 typedef typename prom<T1, T2>::type PT1;
231 typedef typename prom<T2, T1>::type PT2;
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
349 static T
min_val () {
return std::numeric_limits<T>::min (); }
350 static T
max_val () {
return std::numeric_limits<T>::max (); }
354 template <
typename S>
360 static constexpr bool t_is_signed = std::numeric_limits<T>::is_signed;
361 static constexpr bool s_is_signed = std::numeric_limits<S>::is_signed;
363 static constexpr int t_size =
sizeof (T);
364 static constexpr int s_size =
sizeof (S);
366 static constexpr bool omit_chk_min
367 = (! s_is_signed || (t_is_signed && t_size >= s_size));
369 static constexpr 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>
399 static S compute_threshold (S val, T orig_val)
403 val = octave::math::round (val);
407 if (orig_val % 2 && val / 2 == octave::math::round (val / 2))
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);
639 : (x < octave_int_base<T>::min_val () + y
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)
796 : m_ival (
octave_int_base<T>::truncate_int (static_cast<unsigned char> (c)))
807#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
816 template <
typename U>
820 template <
typename U>
832 const unsigned char *
iptr ()
const
834 return reinterpret_cast<const unsigned char *
> (& m_ival);
839 bool bool_value ()
const {
return static_cast<bool> (value ()); }
841 char char_value ()
const {
return static_cast<char> (value ()); }
843 double double_value ()
const {
return static_cast<double> (value ()); }
845 float float_value ()
const {
return static_cast<float> (value ()); }
847 operator T ()
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
872#define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \
873 inline octave_int<T> \
874 operator OP (const ARGT& y) const \
876 return octave_int_arith<T>::NAME (m_ival, y); \
879 inline octave_int<T>& \
880 operator OP##= (const ARGT& y) \
882 m_ival = octave_int_arith<T>::NAME (m_ival, y); \
894#undef OCTAVE_INT_BIN_OP
899 static int nbits () {
return std::numeric_limits<T>::digits; }
940OCTAVE_END_NAMESPACE(math)
941OCTAVE_END_NAMESPACE(octave)
979#define OCTAVE_INT_CMP_OP(OP, NAME) \
980 template <typename T1, typename T2> \
982 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \
984 return octave_int_cmp_op::op<octave_int_cmp_op::NAME, T1, T2> (x.value (), y.value ()); \
994#undef OCTAVE_INT_CMP_OP
1000 os << ival.
value ();
1004template <
typename T>
1024 os << static_cast<int> (ival.
value ());
1033 os << static_cast<unsigned int> (ival.
value ());
1044 ival =
static_cast<int8_t
> (tmp);
1053 unsigned int tmp = 0;
1055 ival =
static_cast<uint8_t
> (tmp);
1062#define OCTAVE_INT_BITCMP_OP(OP) \
1063 template <typename T> \
1065 operator OP (const octave_int<T>& x, const octave_int<T>& y) \
1067 return x.value () OP y.value (); \
1074#undef OCTAVE_INT_BITCMP_OP
1077template <
typename T>
1083 return (a << n) & mask;
1085 return (a >> -n) & mask;
1090#if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
1092# define OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP(T, OP) \
1093 extern OCTAVE_API T \
1094 external_double_ ## T ## _ ## OP (double x, T y); \
1096 extern OCTAVE_API T \
1097 external_ ## T ## _double_ ## OP (T x, double y)
1099# define OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS(T) \
1100 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, add); \
1101 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, sub); \
1102 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, mul); \
1103 OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP (T, div)
1105OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS (
octave_int64);
1110#define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1111 template <typename T> \
1112 inline octave_int<T> \
1113 operator OP (const octave_int<T>& x, const double& y) \
1115 return octave_int<T> (static_cast<double> (x) OP y); \
1118 template <typename T> \
1119 inline octave_int<T> \
1120 operator OP (const double& x, const octave_int<T>& y) \
1122 return octave_int<T> (x OP static_cast<double> (y)); \
1125#if defined (OCTAVE_INT_USE_LONG_DOUBLE)
1129# if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
1131# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1132 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1135 inline octave_int64 \
1136 operator OP (const double& x, const octave_int64& y) \
1138 return external_double_octave_int64_ ## NAME (x, y); \
1142 inline octave_uint64 \
1143 operator OP (const double& x, const octave_uint64& y) \
1145 return external_double_octave_uint64_ ## NAME (x, y); \
1149 inline octave_int64 \
1150 operator OP (const octave_int64& x, const double& y) \
1152 return external_octave_int64_double_ ## NAME (x, y); \
1156 inline octave_uint64 \
1157 operator OP (const octave_uint64& x, const double& y) \
1159 return external_octave_uint64_double_ ## NAME (x, y); \
1164# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1165 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1168 inline octave_int64 \
1169 operator OP (const double& x, const octave_int64& y) \
1171 return octave_int64 (x OP static_cast<long double> (y.value ())); \
1175 inline octave_uint64 \
1176 operator OP (const double& x, const octave_uint64& y) \
1178 return octave_uint64 (x OP static_cast<long double> (y.value ())); \
1182 inline octave_int64 \
1183 operator OP (const octave_int64& x, const double& y) \
1185 return octave_int64 (static_cast<long double> (x.value ()) OP y); \
1189 inline octave_uint64 \
1190 operator OP (const octave_uint64& x, const double& y) \
1192 return octave_uint64 (static_cast<long double> (x.value ()) OP y); \
1201# define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
1202 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
1205 OCTAVE_API octave_int64 \
1206 operator OP (const double&, const octave_int64&); \
1209 OCTAVE_API octave_uint64 \
1210 operator OP (const double&, const octave_uint64&); \
1213 OCTAVE_API octave_int64 \
1214 operator OP (const octave_int64&, const double&); \
1217 OCTAVE_API octave_uint64 \
1218 operator OP (const octave_uint64&, const double&);
1227#undef OCTAVE_INT_DOUBLE_BIN_OP0
1228#undef OCTAVE_INT_DOUBLE_BIN_OP
1229#undef OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OP
1230#undef OCTAVE_DECLARE_EXTERNAL_LONG_DOUBLE_INT_OPS
1232#define OCTAVE_INT_DOUBLE_CMP_OP(OP, NAME) \
1233 template <typename T> \
1235 operator OP (const octave_int<T>& x, const double& y) \
1237 return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x.value (), y); \
1240 template <typename T> \
1242 operator OP (const double& x, const octave_int<T>& y) \
1244 return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x, y.value ()); \
1254#undef OCTAVE_INT_DOUBLE_CMP_OP
1258#define OCTAVE_INT_FLOAT_BIN_OP(OP) \
1259 template <typename T> \
1260 inline octave_int<T> \
1261 operator OP (const octave_int<T>& x, float y) \
1263 return x OP static_cast<double> (y); \
1266 template <typename T> \
1267 inline octave_int<T> \
1268 operator OP (float x, const octave_int<T>& y) \
1270 return static_cast<double> (x) OP y; \
1278#undef OCTAVE_INT_FLOAT_BIN_OP
1280#define OCTAVE_INT_FLOAT_CMP_OP(OP) \
1281 template <typename T> \
1283 operator OP (const octave_int<T>& x, const float& y) \
1285 return x OP static_cast<double> (y); \
1288 template <typename T> \
1290 operator OP (const float& x, const octave_int<T>& y) \
1292 return static_cast<double> (x) OP y; \
1302#undef OCTAVE_INT_FLOAT_CMP_OP
1304template <
typename T>
1309 const T yv = y.
value ();
1314template <
typename T>
1319 const T yv = y.
value ();
1326#define OCTAVE_INT_IDX_TYPE_BIN_OP(OP) \
1327 template <typename T> \
1328 inline octave_int<T> \
1329 operator OP (const octave_int<T>& x, octave_idx_type y) \
1331 return x OP octave_int<T> (y); \
1334 template <typename T> \
1335 inline octave_int<T> \
1336 operator OP (octave_idx_type x, const octave_int<T>& y) \
1338 return octave_int<T> (x) OP y; \
1346#undef OCTAVE_INT_IDX_TYPE_BIN_OP
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)
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 T convert_real(const S &value)
static bool op(T1 x, T2 y)
static bool mop(T x, double y)
static bool mop(double x, T y)
const unsigned char * iptr() const
double double_value() const
static const octave_int s_one
static const char * type_name()
octave_int(const octave_int< U > &i)
static octave_int< T > max()
octave_int(const octave_int< T > &)=default
float float_value() const
static octave_int< T > min()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
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)
bool isnan(const octave_int< T > &)
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)
#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 > operator+(const octave_int< T > &x, const double &y)
octave_int< T > xmax(const octave_int< T > &x, const octave_int< T > &y)
octave_int< T > powf(const float &a, const octave_int< T > &b)
octave_value operator!(const octave_value &a)
static const bool registered