GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-inttypes.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2004-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "fpucw-wrappers.h"
31 #include "lo-error.h"
32 #include "oct-inttypes.h"
33 
34 template <typename T>
35 const octave_int<T> octave_int<T>::s_zero (static_cast<T> (0));
36 
37 template <typename T>
38 const octave_int<T> octave_int<T>::s_one (static_cast<T> (1));
39 
40 // Define type names.
41 
42 #define DEFINE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \
43  template <> \
44  OCTAVE_API const char * \
45  octave_int<TYPE>::type_name () { return TYPENAME; }
46 
48 DEFINE_OCTAVE_INT_TYPENAME (int16_t, "int16")
49 DEFINE_OCTAVE_INT_TYPENAME (int32_t, "int32")
50 DEFINE_OCTAVE_INT_TYPENAME (int64_t, "int64")
51 DEFINE_OCTAVE_INT_TYPENAME (uint8_t, "uint8")
52 DEFINE_OCTAVE_INT_TYPENAME (uint16_t, "uint16")
53 DEFINE_OCTAVE_INT_TYPENAME (uint32_t, "uint32")
54 DEFINE_OCTAVE_INT_TYPENAME (uint64_t, "uint64")
55 
56 template <class T>
57 template <class S>
58 T
59 octave_int_base<T>::convert_real (const S& value)
60 {
61  // Compute proper thresholds.
62  static const S thmin = compute_threshold (static_cast<S> (min_val ()),
63  min_val ());
64  static const S thmax = compute_threshold (static_cast<S> (max_val ()),
65  max_val ());
66  if (octave::math::isnan (value))
67  return static_cast<T> (0);
68  else if (value < thmin)
69  return min_val ();
70  else if (value > thmax)
71  return max_val ();
72  else
73  {
74  S rvalue = octave::math::round (value);
75  return static_cast<T> (rvalue);
76  }
77 }
78 
79 #define INSTANTIATE_CONVERT_REAL_1(T, S) \
80  template \
81  OCTAVE_API \
82  T \
83  octave_int_base<T>::convert_real (const S&)
84 
85 #define INSTANTIATE_CONVERT_REAL(S) \
86  INSTANTIATE_CONVERT_REAL_1 (int8_t, S); \
87  INSTANTIATE_CONVERT_REAL_1 (uint8_t, S); \
88  INSTANTIATE_CONVERT_REAL_1 (int16_t, S); \
89  INSTANTIATE_CONVERT_REAL_1 (uint16_t, S); \
90  INSTANTIATE_CONVERT_REAL_1 (int32_t, S); \
91  INSTANTIATE_CONVERT_REAL_1 (uint32_t, S); \
92  INSTANTIATE_CONVERT_REAL_1 (int64_t, S); \
93  INSTANTIATE_CONVERT_REAL_1 (uint64_t, S)
94 
97 #if defined (OCTAVE_INT_USE_LONG_DOUBLE)
98 INSTANTIATE_CONVERT_REAL (long double);
99 #endif
100 
101 #if defined (OCTAVE_INT_USE_LONG_DOUBLE)
102 
103 # if defined (OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED)
104 
105 # define DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES(T) \
106  template <typename xop> \
107  bool \
108  octave_int_cmp_op::external_mop (double x, T y) \
109  { \
110  unsigned int oldcw = octave_begin_long_double_rounding (); \
111  \
112  bool retval = xop::op (static_cast<long double> (x), \
113  static_cast<long double> (y)); \
114  \
115  octave_end_long_double_rounding (oldcw); \
116  \
117  return retval; \
118  } \
119  \
120  template <typename xop> \
121  bool \
122  octave_int_cmp_op::external_mop (T x, double y) \
123  { \
124  unsigned int oldcw = octave_begin_long_double_rounding (); \
125  \
126  bool retval = xop::op (static_cast<long double> (x), \
127  static_cast<long double> (y)); \
128  \
129  octave_end_long_double_rounding (oldcw); \
130  \
131  return retval; \
132  }
133 
134 DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (int64_t)
135 DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (uint64_t)
136 
137 # define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP(OP, T) \
138  template OCTAVE_API bool \
139  octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (double, T); \
140  \
141  template OCTAVE_API bool \
142  octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (T, double)
143 
144 # define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS(T) \
145  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (lt, T); \
146  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (le, T); \
147  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (gt, T); \
148  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ge, T); \
149  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (eq, T); \
150  INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ne, T)
151 
152 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (int64_t);
153 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (uint64_t);
154 
155 uint64_t
156 octave_external_uint64_uint64_mul (uint64_t x, uint64_t y)
157 {
158  unsigned int oldcw = octave_begin_long_double_rounding ();
159 
161 
163 
164  return retval;
165 }
166 
167 int64_t
168 octave_external_int64_int64_mul (int64_t x, int64_t y)
169 {
170  unsigned int oldcw = octave_begin_long_double_rounding ();
171 
173 
175 
176  return retval;
177 }
178 
179 // Note that if we return long double it is apparently possible for
180 // truncation to happen at the point of storing the result in retval,
181 // which can happen after we end long double rounding. Attempt to avoid
182 // that problem by storing the full precision temporary value in the
183 // integer value before we end the long double rounding mode.
184 // Similarly, the conversion from the 64-bit integer type to long double
185 // must also occur in long double rounding mode.
186 
187 # define DEFINE_OCTAVE_LONG_DOUBLE_OP(T, OP, NAME) \
188  T \
189  external_double_ ## T ## _ ## NAME (double x, T y) \
190  { \
191  unsigned int oldcw = octave_begin_long_double_rounding (); \
192  \
193  T retval = T (x OP static_cast<long double> (y.value ())); \
194  \
195  octave_end_long_double_rounding (oldcw); \
196  \
197  return retval; \
198  } \
199  \
200  T \
201  external_ ## T ## _double_ ## NAME (T x, double y) \
202  { \
203  unsigned int oldcw = octave_begin_long_double_rounding (); \
204  \
205  T retval = T (static_cast<long double> (x.value ()) OP y); \
206  \
207  octave_end_long_double_rounding (oldcw); \
208  \
209  return retval; \
210  }
211 
212 # define DEFINE_OCTAVE_LONG_DOUBLE_OPS(T) \
213  DEFINE_OCTAVE_LONG_DOUBLE_OP (T, +, add); \
214  DEFINE_OCTAVE_LONG_DOUBLE_OP (T, -, sub); \
215  DEFINE_OCTAVE_LONG_DOUBLE_OP (T, *, mul); \
216  DEFINE_OCTAVE_LONG_DOUBLE_OP (T, /, div)
217 
218 DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_int64);
219 DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_uint64);
220 
221 # endif
222 
223 #else
224 
225 // Define comparison operators
226 
227 template <typename xop>
228 bool
229 octave_int_cmp_op::emulate_mop (uint64_t x, double y)
230 {
231  // The following cast changes the value to 2^64 (which is outside the range
232  // of `uint64_t`). Take care to handle this correctly (e.g., don't cast back
233  // to `uint64_t`)!
234  static const double xxup
235  = static_cast<double> (std::numeric_limits<uint64_t>::max ());
236  // This converts to the nearest double. Unless there's an equality, the
237  // result is clear.
238  double xx = x;
239  if (xx != y)
240  return xop::op (xx, y);
241  else
242  {
243  // If equality occurred we compare as integers.
244  if (xx == xxup)
245  return xop::gtval;
246  else
247  return xop::op (x, static_cast<uint64_t> (xx));
248  }
249 }
250 
251 template <typename xop>
252 bool
254 {
255  // The following cast changes the value to 2^63 (which is outside the range
256  // of `int64_t`). Take care to handle this correctly (e.g., don't cast back
257  // to `int64_t`)! The same applies to the lower limit on systems using one's
258  // complement.
259  static const double xxup
260  = static_cast<double> (std::numeric_limits<int64_t>::max ());
261  static const double xxlo
262  = static_cast<double> (std::numeric_limits<int64_t>::min ());
263  // This converts to the nearest double. Unless there's an equality, the
264  // result is clear.
265  double xx = x;
266  if (xx != y)
267  return xop::op (xx, y);
268  else
269  {
270  // If equality occurred we compare as integers.
271  if (xx == xxup)
272  return xop::gtval;
273  else if (xx == xxlo)
274  return xop::ltval;
275  else
276  return xop::op (x, static_cast<int64_t> (xx));
277  }
278 
279 }
280 
281 // We define double-int operations by reverting the operator
282 
283 // A trait class reverting the operator
284 template <typename xop>
285 class rev_op
286 {
287 public:
288  typedef xop op;
289 };
290 
291 #define DEFINE_REVERTED_OPERATOR(OP1, OP2) \
292  template <> \
293  class rev_op<octave_int_cmp_op::OP1> \
294  { \
295  public: \
296  typedef octave_int_cmp_op::OP2 op; \
297  }
298 
299 DEFINE_REVERTED_OPERATOR (lt, gt);
300 DEFINE_REVERTED_OPERATOR (gt, lt);
301 DEFINE_REVERTED_OPERATOR (le, ge);
302 DEFINE_REVERTED_OPERATOR (ge, le);
303 
304 template <typename xop>
305 bool
306 octave_int_cmp_op::emulate_mop (double x, uint64_t y)
307 {
308  typedef typename rev_op<xop>::op rop;
309  return mop<rop> (y, x);
310 }
311 
312 template <typename xop>
313 bool
315 {
316  typedef typename rev_op<xop>::op rop;
317  return mop<rop> (y, x);
318 }
319 
320 // Define handlers for (u)int64 multiplication.
321 
322 template <>
323 uint64_t
325 {
326  // Get upper words
327  uint64_t ux = x >> 32;
328  uint64_t uy = y >> 32;
329  uint64_t res;
330  if (ux)
331  {
332  if (uy)
333  goto overflow;
334  else
335  {
336  uint64_t ly = static_cast<uint32_t> (y);
337  uint64_t uxly = ux*ly;
338  if (uxly >> 32)
339  goto overflow;
340  uxly <<= 32; // never overflows
341  uint64_t lx = static_cast<uint32_t> (x);
342  uint64_t lxly = lx*ly;
343  res = add (uxly, lxly);
344  }
345  }
346  else if (uy)
347  {
348  uint64_t lx = static_cast<uint32_t> (x);
349  uint64_t uylx = uy*lx;
350  if (uylx >> 32)
351  goto overflow;
352  uylx <<= 32; // never overflows
353  uint64_t ly = static_cast<uint32_t> (y);
354  uint64_t lylx = ly*lx;
355  res = add (uylx, lylx);
356  }
357  else
358  {
359  uint64_t lx = static_cast<uint32_t> (x);
360  uint64_t ly = static_cast<uint32_t> (y);
361  res = lx*ly;
362  }
363 
364  return res;
365 
366 overflow:
367  return max_val ();
368 }
369 
370 template <>
371 int64_t
373 {
374  // The signed case is far worse. The problem is that even if neither
375  // integer fits into signed 32-bit range, the result may still be OK.
376  // Uh oh.
377 
378  // Essentially, what we do is compute sign, multiply absolute values
379  // (as above) and impose the sign.
380 
381  // But first, avoid overflow in computation of abs (min_val ()).
382 
383  if (x == min_val ())
384  return y == 0 ? 0 : (y < 0 ? max_val () : min_val ());
385 
386  if (y == min_val ())
387  return x == 0 ? 0 : (x < 0 ? max_val () : min_val ());
388 
389  uint64_t usx = octave_int_abs (x);
390  uint64_t usy = octave_int_abs (y);
391  bool positive = (x < 0) == (y < 0);
392 
393  // Get upper words
394  uint64_t ux = usx >> 32;
395  uint64_t uy = usy >> 32;
396  uint64_t res;
397  if (ux)
398  {
399  if (uy)
400  goto overflow;
401  else
402  {
403  uint64_t ly = static_cast<uint32_t> (usy);
404  uint64_t uxly = ux*ly;
405  if (uxly >> 32)
406  goto overflow;
407  uxly <<= 32; // never overflows
408  uint64_t lx = static_cast<uint32_t> (usx);
409  uint64_t lxly = lx*ly;
410  res = uxly + lxly;
411  if (res < uxly)
412  goto overflow;
413  }
414  }
415  else if (uy)
416  {
417  uint64_t lx = static_cast<uint32_t> (usx);
418  uint64_t uylx = uy*lx;
419  if (uylx >> 32)
420  goto overflow;
421  uylx <<= 32; // never overflows
422  uint64_t ly = static_cast<uint32_t> (usy);
423  uint64_t lylx = ly*lx;
424  res = uylx + lylx;
425  if (res < uylx)
426  goto overflow;
427  }
428  else
429  {
430  uint64_t lx = static_cast<uint32_t> (usx);
431  uint64_t ly = static_cast<uint32_t> (usy);
432  res = lx*ly;
433  }
434 
435  if (positive)
436  {
437  if (res > static_cast<uint64_t> (max_val ()))
438  return max_val ();
439  else
440  return static_cast<int64_t> (res);
441  }
442  else
443  {
444  if (res > static_cast<uint64_t> (min_val ()))
445  return min_val ();
446  else
447  return -static_cast<int64_t> (res);
448  }
449 
450 overflow:
451  return positive ? max_val () : min_val ();
452 
453 }
454 
455 template <>
457 operator + (const octave_uint64& x, const double& y)
458 {
459  return (y < 0) ? x - octave_uint64 (-y) : x + octave_uint64 (y);
460 }
461 
462 template <>
464 operator + (const double& x, const octave_uint64& y)
465 {
466  return y + x;
467 }
468 
469 template <>
471 operator + (const octave_int64& x, const double& y)
472 {
473  // The following cast changes the value to 2^63 (which is outside the range
474  // of `int64_t`).
475  if (fabs (y) < static_cast<double> (octave_int64::max ()))
476  return x + octave_int64 (y);
477  else
478  {
479  // If the number is within the int64 range (the most common case,
480  // probably), the above will work as expected. If not, it's more
481  // complicated - as long as y is within _twice_ the signed range, the
482  // result may still be an integer. An instance of such an operation is
483  // 3*2^62 + (1+intmin ('int64')) that should yield int64 (2^62) + 1.
484  // So what we do is to try to convert y/2 and add it twice. Note that
485  // if y/2 overflows, the result must overflow as well, and that y/2
486  // cannot be a fractional number.
487  octave_int64 y2 (y / 2);
488  return (x + y2) + y2;
489  }
490 }
491 
492 template <>
494 operator + (const double& x, const octave_int64& y)
495 {
496  return y + x;
497 }
498 
499 template <>
501 operator - (const octave_uint64& x, const double& y)
502 {
503  return x + (-y);
504 }
505 
506 template <>
508 operator - (const double& x, const octave_uint64& y)
509 {
510  // The following cast changes the value to 2^64 (which is outside the range
511  // of `uint64_t`).
512  if (x < static_cast<double> (octave_uint64::max ()))
513  return octave_uint64 (x) - y;
514  else
515  {
516  // Again a trick to get the corner cases right. Things like
517  // 3^2^63 - intmax ('uint64') should produce the correct result, i.e.
518  // int64 (2^63) + 1.
519  const double p2_64 = std::pow (2.0, 64);
520  if (y.bool_value ())
521  {
522  const uint64_t p2_64my = (~y.value ()) + 1; // Equals 2^64 - y
523  return octave_uint64 (x - p2_64) + octave_uint64 (p2_64my);
524  }
525  else
526  return octave_uint64 (p2_64);
527  }
528 }
529 
530 template <>
532 operator - (const octave_int64& x, const double& y)
533 {
534  return x + (-y);
535 }
536 
537 template <>
539 operator - (const double& x, const octave_int64& y)
540 {
541  static const bool twosc = (std::numeric_limits<int64_t>::min ()
543  // In case of symmetric integers (not two's complement), this will probably
544  // be eliminated at compile time.
545  if (twosc && y.value () == std::numeric_limits<int64_t>::min ())
546  return octave_int64 (x + std::pow (2.0, 63));
547  else
548  return x + (-y);
549 }
550 
551 // NOTE:
552 // Emulated mixed multiplications are tricky due to possible precision loss.
553 // Here, after sorting out common cases for speed, we follow the strategy
554 // of converting the double number into the form sign * 64-bit integer *
555 // 2^exponent, multiply the 64-bit integers to get a 128-bit number, split that
556 // number into 32-bit words and form 4 double-valued summands (none of which
557 // loses precision), then convert these into integers and sum them. Though it
558 // is not immediately obvious, this should work even w.r.t. rounding (none of
559 // the summands lose precision).
560 
561 // Multiplies two unsigned 64-bit ints to get a 128-bit number represented
562 // as four 32-bit words.
563 static void
564 umul128 (uint64_t x, uint64_t y, uint32_t w[4])
565 {
566  uint64_t lx = static_cast<uint32_t> (x);
567  uint64_t ux = x >> 32;
568  uint64_t ly = static_cast<uint32_t> (y);
569  uint64_t uy = y >> 32;
570  uint64_t a = lx * ly;
571  w[0] = a; a >>= 32;
572  uint64_t uxly = ux*ly;
573  uint64_t uylx = uy*lx;
574  a += static_cast<uint32_t> (uxly); uxly >>= 32;
575  a += static_cast<uint32_t> (uylx); uylx >>= 32;
576  w[1] = a; a >>= 32;
577  uint64_t uxuy = ux * uy;
578  a += uxly; a += uylx; a += uxuy;
579  w[2] = a; a >>= 32;
580  w[3] = a;
581 }
582 
583 // Splits a double into bool sign, unsigned 64-bit mantissa and int exponent
584 static void
585 dblesplit (double x, bool& sign, uint64_t& mtis, int& exp)
586 {
587  sign = x < 0; x = fabs (x);
588  x = octave::math::frexp (x, &exp);
589  exp -= 52;
590  mtis = static_cast<uint64_t> (ldexp (x, 52));
591 }
592 
593 // Gets a double number from a
594 // 32-bit unsigned integer mantissa, exponent, and sign.
595 static double
596 dbleget (bool sign, uint32_t mtis, int exp)
597 {
598  double x = ldexp (static_cast<double> (mtis), exp);
599  return sign ? -x : x;
600 }
601 
602 template <>
604 operator * (const octave_uint64& x, const double& y)
605 {
606  // The following cast changes the value to 2^64 (which is outside the range
607  // of `uint64_t`).
608  if (y >= 0 && y < static_cast<double> (octave_uint64::max ())
609  && y == octave::math::fix (y))
610  return x * octave_uint64 (static_cast<uint64_t> (y));
611  else if (y == 0.5)
612  return x / octave_uint64 (static_cast<uint64_t> (2));
613  else if (y < 0 || octave::math::isnan (y) || octave::math::isinf (y))
614  return octave_uint64 (x.value () * y);
615  else
616  {
617  bool sign;
618  uint64_t my;
619  int e;
620  dblesplit (y, sign, my, e);
621  uint32_t w[4];
622  umul128 (x.value (), my, w);
624  for (short i = 0; i < 4; i++)
625  {
626  res += octave_uint64 (dbleget (sign, w[i], e));
627  e += 32;
628  }
629  return res;
630  }
631 }
632 
633 template <>
635 operator * (const double& x, const octave_uint64& y)
636 {
637  return y * x;
638 }
639 
640 template <>
642 operator * (const octave_int64& x, const double& y)
643 {
644  // The following cast changes the value to 2^63 (which is outside the range
645  // of `int64_t`).
646  if (fabs (y) < static_cast<double> (octave_int64::max ())
647  && y == octave::math::fix (y))
648  return x * octave_int64 (static_cast<int64_t> (y));
649  else if (fabs (y) == 0.5)
650  return x / octave_int64 (static_cast<uint64_t> (4*y));
651  else if (octave::math::isnan (y) || octave::math::isinf (y))
652  return octave_int64 (x.value () * y);
653  else
654  {
655  bool sign;
656  uint64_t my;
657  int e;
658  dblesplit (y, sign, my, e);
659  uint32_t w[4];
660  sign = (sign != (x.value () < 0));
661  umul128 (octave_int_abs (x.value ()), my, w);
663  for (short i = 0; i < 4; i++)
664  {
665  res += octave_int64 (dbleget (sign, w[i], e));
666  e += 32;
667  }
668  return res;
669  }
670 }
671 
672 template <>
674 operator * (const double& x, const octave_int64& y)
675 {
676  return y * x;
677 }
678 
679 template <>
681 operator / (const double& x, const octave_uint64& y)
682 {
683  return octave_uint64 (x / static_cast<double> (y));
684 }
685 
686 template <>
688 operator / (const double& x, const octave_int64& y)
689 {
690  return octave_int64 (x / static_cast<double> (y));
691 }
692 
693 template <>
695 operator / (const octave_uint64& x, const double& y)
696 {
697  // The following cast changes the value to 2^64 (which is outside the range
698  // of `uint64_t`).
699  if (y >= 0 && y < static_cast<double> (octave_uint64::max ())
700  && y == octave::math::fix (y))
701  return x / octave_uint64 (y);
702  else
703  return x * (1.0/y);
704 }
705 
706 template <>
708 operator / (const octave_int64& x, const double& y)
709 {
710  // The following cast changes the value to 2^63 (which is outside the range
711  // of `int64_t`).
712  if (fabs (y) < static_cast<double> (octave_int64::max ())
713  && y == octave::math::fix (y))
714  return x / octave_int64 (y);
715  else
716  return x * (1.0/y);
717 }
718 
719 #define INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, T1, T2) \
720  template OCTAVE_API bool \
721  octave_int_cmp_op::emulate_mop<octave_int_cmp_op::OP> (T1 x, T2 y)
722 
723 #define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP) \
724  INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, int64_t); \
725  INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, uint64_t); \
726  INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, int64_t, double); \
727  INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, uint64_t, double)
728 
735 
736 #endif
737 
738 template <typename T>
740 pow (const octave_int<T>& a, const octave_int<T>& b)
741 {
742  octave_int<T> retval;
743 
746 
747  if (b == zero || a == one)
748  retval = one;
749  else if (b < zero)
750  {
751  if (a == -one)
752  retval = (b.value () % 2) ? a : one;
753  else
754  retval = zero;
755  }
756  else
757  {
758  octave_int<T> a_val = a;
759  T b_val = b; // no need to do saturation on b
760 
761  retval = a;
762 
763  b_val -= 1;
764 
765  while (b_val != 0)
766  {
767  if (b_val & 1)
768  retval = retval * a_val;
769 
770  b_val = b_val >> 1;
771 
772  if (b_val)
773  a_val = a_val * a_val;
774  }
775  }
776 
777  return retval;
778 }
779 
780 template <typename T>
782 pow (const double& a, const octave_int<T>& b)
783 { return octave_int<T> (std::pow (a, b.double_value ())); }
784 
785 template <typename T>
787 pow (const octave_int<T>& a, const double& b)
788 {
789  return ((b >= 0 && b < std::numeric_limits<T>::digits
790  && b == octave::math::fix (b))
791  ? pow (a, octave_int<T> (static_cast<T> (b)))
792  : octave_int<T> (std::pow (a.double_value (), b)));
793 }
794 
795 template <typename T>
797 pow (const float& a, const octave_int<T>& b)
798 { return octave_int<T> (std::pow (a, b.float_value ())); }
799 
800 template <typename T>
802 pow (const octave_int<T>& a, const float& b)
803 {
804  return ((b >= 0 && b < std::numeric_limits<T>::digits
805  && b == octave::math::fix (b))
806  ? pow (a, octave_int<T> (static_cast<T> (b)))
808  static_cast<double> (b))));
809 }
810 
811 // FIXME: Do we really need a differently named single-precision function
812 // integer power function here instead of an overloaded one?
813 template <typename T>
815 powf (const float& a, const octave_int<T>& b)
816 { return octave_int<T> (pow (a, b.float_value ())); }
817 
818 template <typename T>
820 powf (const octave_int<T>& a, const float& b)
821 {
822  return ((b >= 0 && b < std::numeric_limits<T>::digits
823  && b == octave::math::fix (b))
824  ? pow (a, octave_int<T> (static_cast<T> (b)))
826  static_cast<double> (b))));
827 }
828 
829 #define INSTANTIATE_INTTYPE(T) \
830  template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API octave_int<T>; \
831  \
832  template OCTAVE_API octave_int<T> \
833  pow (const octave_int<T>&, const octave_int<T>&); \
834  \
835  template OCTAVE_API octave_int<T> \
836  pow (const double&, const octave_int<T>&); \
837  \
838  template OCTAVE_API octave_int<T> \
839  pow (const octave_int<T>&, const double&); \
840  \
841  template OCTAVE_API octave_int<T> \
842  pow (const float&, const octave_int<T>&); \
843  \
844  template OCTAVE_API octave_int<T> \
845  pow (const octave_int<T>&, const float&); \
846  \
847  template OCTAVE_API octave_int<T> \
848  powf (const float&, const octave_int<T>&); \
849  \
850  template OCTAVE_API octave_int<T> \
851  powf (const octave_int<T>&, const float&); \
852  \
853  template OCTAVE_API octave_int<T> \
854  bitshift (const octave_int<T>&, int, const octave_int<T>&);
855 
860 
865 
866 /*
867 
868 %!assert (intmax ("int64") / intmin ("int64"), int64 (-1))
869 %!assert (intmin ("int64") / int64 (-1), intmax ("int64"))
870 %!assert (int64 (2^63), intmax ("int64"))
871 %!assert (uint64 (2^64), intmax ("uint64"))
872 %!test
873 %! a = 1.9*2^61; b = uint64 (a); b++; assert (b > a);
874 %!test
875 %! a = -1.9*2^61; b = int64 (a); b++; assert (b > a);
876 %!test
877 %! a = int64 (-2^60) + 2; assert (1.25*a == (5*a)/4);
878 %!test
879 %! a = uint64 (2^61) + 2; assert (1.25*a == (5*a)/4);
880 %!assert (int32 (2^31+0.5), intmax ("int32"))
881 %!assert (int32 (-2^31-0.5), intmin ("int32"))
882 %!assert ((int64 (2^62)+1)^1, int64 (2^62)+1)
883 %!assert ((int64 (2^30)+1)^2, int64 (2^60+2^31) + 1)
884 
885 %!assert <54382> (uint8 (char (128)), uint8 (128))
886 %!assert <54382> (uint8 (char (255)), uint8 (255))
887 %!assert <54382> (int8 (char (128)), int8 (128))
888 %!assert <54382> (int8 (char (255)), int8 (255))
889 
890 %!assert <54382> (uint16 (char (128)), uint16 (128))
891 %!assert <54382> (uint16 (char (255)), uint16 (255))
892 %!assert <54382> (int16 (char (128)), int16 (128))
893 %!assert <54382> (int16 (char (255)), int16 (255))
894 
895 %!assert <54382> (uint32 (char (128)), uint32 (128))
896 %!assert <54382> (uint32 (char (255)), uint32 (255))
897 %!assert <54382> (int32 (char (128)), int32 (128))
898 %!assert <54382> (int32 (char (255)), int32 (255))
899 
900 %!assert <54382> (uint64 (char (128)), uint64 (128))
901 %!assert <54382> (uint64 (char (255)), uint64 (255))
902 %!assert <54382> (int64 (char (128)), int64 (128))
903 %!assert <54382> (int64 (char (255)), int64 (255))
904 */
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
uint64_t mul_internal(uint64_t x, uint64_t y)
static bool emulate_mop(double, int64_t)
T value() const
Definition: oct-inttypes.h:832
double double_value() const
Definition: oct-inttypes.h:845
bool bool_value() const
Definition: oct-inttypes.h:841
static octave_int< T > max()
Definition: oct-inttypes.h:899
static const octave_int s_zero
Definition: oct-inttypes.h:908
static const octave_int s_one
Definition: oct-inttypes.h:908
float float_value() const
Definition: oct-inttypes.h:847
unsigned int octave_begin_long_double_rounding(void)
void octave_end_long_double_rounding(unsigned int oldcw)
double frexp(double x, int *expptr)
Definition: lo-mappers.cc:129
bool isinf(double x)
Definition: lo-mappers.h:203
double round(double x)
Definition: lo-mappers.h:136
bool isnan(bool)
Definition: lo-mappers.h:178
double fix(double x)
Definition: lo-mappers.h:118
F77_RET_T const F77_DBLE * x
#define OCTAVE_API
Definition: main.cc:55
std::complex< double > w(std::complex< double > z, double relerr=0)
octave_int< int64_t > octave_int64
octave_int< uint64_t > octave_uint64
octave_uint64 operator*(const octave_uint64 &x, const double &y)
#define INSTANTIATE_INTTYPE(T)
octave_uint64 operator/(const double &x, const octave_uint64 &y)
#define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP)
octave_uint64 operator+(const octave_uint64 &x, const double &y)
octave_uint64 operator-(const octave_uint64 &x, const double &y)
octave_int< T > powf(const float &a, const octave_int< T > &b)
#define DEFINE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME)
Definition: oct-inttypes.cc:42
#define DEFINE_REVERTED_OPERATOR(OP1, OP2)
#define INSTANTIATE_CONVERT_REAL(S)
Definition: oct-inttypes.cc:85
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
T octave_int_abs(T x)
Definition: oct-inttypes.h:68