GNU Octave 7.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-2022 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
34template <typename T>
35const octave_int<T> octave_int<T>::s_zero (static_cast<T> (0));
36
37template <typename T>
38const 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 (void) { return TYPENAME; }
46
52DEFINE_OCTAVE_INT_TYPENAME (uint16_t, "uint16")
53DEFINE_OCTAVE_INT_TYPENAME (uint32_t, "uint32")
54DEFINE_OCTAVE_INT_TYPENAME (uint64_t, "uint64")
55
56template <class T>
57template <class S>
58T
59octave_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)
98INSTANTIATE_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
134DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (int64_t)
135DEFINE_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
152INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (int64_t);
153INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (uint64_t);
154
155uint64_t
156octave_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
167int64_t
168octave_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
218DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_int64);
219DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_uint64);
220
221# endif
222
223#else
224
225// Define comparison operators
226
227template <typename xop>
228bool
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
251template <typename xop>
252bool
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
284template <typename xop>
286{
287public:
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
303
304template <typename xop>
305bool
307{
308 typedef typename rev_op<xop>::op rop;
309 return mop<rop> (y, x);
310}
311
312template <typename xop>
313bool
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
322template <>
323uint64_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
366overflow:
367 return max_val ();
368}
369
370template <>
371int64_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 uint64_t usx = octave_int_abs (x);
382 uint64_t usy = octave_int_abs (y);
383 bool positive = (x < 0) == (y < 0);
384
385 // Get upper words
386 uint64_t ux = usx >> 32;
387 uint64_t uy = usy >> 32;
388 uint64_t res;
389 if (ux)
390 {
391 if (uy)
392 goto overflow;
393 else
394 {
395 uint64_t ly = static_cast<uint32_t> (usy);
396 uint64_t uxly = ux*ly;
397 if (uxly >> 32)
398 goto overflow;
399 uxly <<= 32; // never overflows
400 uint64_t lx = static_cast<uint32_t> (usx);
401 uint64_t lxly = lx*ly;
402 res = uxly + lxly;
403 if (res < uxly)
404 goto overflow;
405 }
406 }
407 else if (uy)
408 {
409 uint64_t lx = static_cast<uint32_t> (usx);
410 uint64_t uylx = uy*lx;
411 if (uylx >> 32)
412 goto overflow;
413 uylx <<= 32; // never overflows
414 uint64_t ly = static_cast<uint32_t> (usy);
415 uint64_t lylx = ly*lx;
416 res = uylx + lylx;
417 if (res < uylx)
418 goto overflow;
419 }
420 else
421 {
422 uint64_t lx = static_cast<uint32_t> (usx);
423 uint64_t ly = static_cast<uint32_t> (usy);
424 res = lx*ly;
425 }
426
427 if (positive)
428 {
429 if (res > static_cast<uint64_t> (max_val ()))
430 return max_val ();
431 else
432 return static_cast<int64_t> (res);
433 }
434 else
435 {
436 if (res > static_cast<uint64_t> (min_val ()))
437 return min_val ();
438 else
439 return -static_cast<int64_t> (res);
440 }
441
442overflow:
443 return positive ? max_val () : min_val ();
444
445}
446
447template <>
449operator + (const octave_uint64& x, const double& y)
450{
451 return (y < 0) ? x - octave_uint64 (-y) : x + octave_uint64 (y);
452}
453
454template <>
456operator + (const double& x, const octave_uint64& y)
457{
458 return y + x;
459}
460
461template <>
463operator + (const octave_int64& x, const double& y)
464{
465 // The following cast changes the value to 2^63 (which is outside the range
466 // of `int64_t`).
467 if (fabs (y) < static_cast<double> (octave_int64::max ()))
468 return x + octave_int64 (y);
469 else
470 {
471 // If the number is within the int64 range (the most common case,
472 // probably), the above will work as expected. If not, it's more
473 // complicated - as long as y is within _twice_ the signed range, the
474 // result may still be an integer. An instance of such an operation is
475 // 3*2^62 + (1+intmin ('int64')) that should yield int64 (2^62) + 1.
476 // So what we do is to try to convert y/2 and add it twice. Note that
477 // if y/2 overflows, the result must overflow as well, and that y/2
478 // cannot be a fractional number.
479 octave_int64 y2 (y / 2);
480 return (x + y2) + y2;
481 }
482}
483
484template <>
486operator + (const double& x, const octave_int64& y)
487{
488 return y + x;
489}
490
491template <>
493operator - (const octave_uint64& x, const double& y)
494{
495 return x + (-y);
496}
497
498template <>
500operator - (const double& x, const octave_uint64& y)
501{
502 // The following cast changes the value to 2^64 (which is outside the range
503 // of `uint64_t`).
504 if (x < static_cast<double> (octave_uint64::max ()))
505 return octave_uint64 (x) - y;
506 else
507 {
508 // Again a trick to get the corner cases right. Things like
509 // 3^2^63 - intmax ('uint64') should produce the correct result, i.e.
510 // int64 (2^63) + 1.
511 const double p2_64 = std::pow (2.0, 64);
512 if (y.bool_value ())
513 {
514 const uint64_t p2_64my = (~y.value ()) + 1; // Equals 2^64 - y
515 return octave_uint64 (x - p2_64) + octave_uint64 (p2_64my);
516 }
517 else
518 return octave_uint64 (p2_64);
519 }
520}
521
522template <>
524operator - (const octave_int64& x, const double& y)
525{
526 return x + (-y);
527}
528
529template <>
531operator - (const double& x, const octave_int64& y)
532{
533 static const bool twosc = (std::numeric_limits<int64_t>::min ()
535 // In case of symmetric integers (not two's complement), this will probably
536 // be eliminated at compile time.
537 if (twosc && y.value () == std::numeric_limits<int64_t>::min ())
538 return octave_int64 (x + std::pow (2.0, 63));
539 else
540 return x + (-y);
541}
542
543// NOTE:
544// Emulated mixed multiplications are tricky due to possible precision loss.
545// Here, after sorting out common cases for speed, we follow the strategy
546// of converting the double number into the form sign * 64-bit integer *
547// 2^exponent, multiply the 64-bit integers to get a 128-bit number, split that
548// number into 32-bit words and form 4 double-valued summands (none of which
549// loses precision), then convert these into integers and sum them. Though it
550// is not immediately obvious, this should work even w.r.t. rounding (none of
551// the summands lose precision).
552
553// Multiplies two unsigned 64-bit ints to get a 128-bit number represented
554// as four 32-bit words.
555static void
556umul128 (uint64_t x, uint64_t y, uint32_t w[4])
557{
558 uint64_t lx = static_cast<uint32_t> (x);
559 uint64_t ux = x >> 32;
560 uint64_t ly = static_cast<uint32_t> (y);
561 uint64_t uy = y >> 32;
562 uint64_t a = lx * ly;
563 w[0] = a; a >>= 32;
564 uint64_t uxly = ux*ly;
565 uint64_t uylx = uy*lx;
566 a += static_cast<uint32_t> (uxly); uxly >>= 32;
567 a += static_cast<uint32_t> (uylx); uylx >>= 32;
568 w[1] = a; a >>= 32;
569 uint64_t uxuy = ux * uy;
570 a += uxly; a += uylx; a += uxuy;
571 w[2] = a; a >>= 32;
572 w[3] = a;
573}
574
575// Splits a double into bool sign, unsigned 64-bit mantissa and int exponent
576static void
577dblesplit (double x, bool& sign, uint64_t& mtis, int& exp)
578{
579 sign = x < 0; x = fabs (x);
580 x = octave::math::frexp (x, &exp);
581 exp -= 52;
582 mtis = static_cast<uint64_t> (ldexp (x, 52));
583}
584
585// Gets a double number from a
586// 32-bit unsigned integer mantissa, exponent, and sign.
587static double
588dbleget (bool sign, uint32_t mtis, int exp)
589{
590 double x = ldexp (static_cast<double> (mtis), exp);
591 return sign ? -x : x;
592}
593
594template <>
596operator * (const octave_uint64& x, const double& y)
597{
598 // The following cast changes the value to 2^64 (which is outside the range
599 // of `uint64_t`).
600 if (y >= 0 && y < static_cast<double> (octave_uint64::max ())
601 && y == octave::math::fix (y))
602 return x * octave_uint64 (static_cast<uint64_t> (y));
603 else if (y == 0.5)
604 return x / octave_uint64 (static_cast<uint64_t> (2));
605 else if (y < 0 || octave::math::isnan (y) || octave::math::isinf (y))
606 return octave_uint64 (x.value () * y);
607 else
608 {
609 bool sign;
610 uint64_t my;
611 int e;
612 dblesplit (y, sign, my, e);
613 uint32_t w[4];
614 umul128 (x.value (), my, w);
616 for (short i = 0; i < 4; i++)
617 {
618 res += octave_uint64 (dbleget (sign, w[i], e));
619 e += 32;
620 }
621 return res;
622 }
623}
624
625template <>
627operator * (const double& x, const octave_uint64& y)
628{
629 return y * x;
630}
631
632template <>
634operator * (const octave_int64& x, const double& y)
635{
636 // The following cast changes the value to 2^63 (which is outside the range
637 // of `int64_t`).
638 if (fabs (y) < static_cast<double> (octave_int64::max ())
639 && y == octave::math::fix (y))
640 return x * octave_int64 (static_cast<int64_t> (y));
641 else if (fabs (y) == 0.5)
642 return x / octave_int64 (static_cast<uint64_t> (4*y));
643 else if (octave::math::isnan (y) || octave::math::isinf (y))
644 return octave_int64 (x.value () * y);
645 else
646 {
647 bool sign;
648 uint64_t my;
649 int e;
650 dblesplit (y, sign, my, e);
651 uint32_t w[4];
652 sign = (sign != (x.value () < 0));
653 umul128 (octave_int_abs (x.value ()), my, w);
655 for (short i = 0; i < 4; i++)
656 {
657 res += octave_int64 (dbleget (sign, w[i], e));
658 e += 32;
659 }
660 return res;
661 }
662}
663
664template <>
666operator * (const double& x, const octave_int64& y)
667{
668 return y * x;
669}
670
671template <>
673operator / (const double& x, const octave_uint64& y)
674{
675 return octave_uint64 (x / static_cast<double> (y));
676}
677
678template <>
680operator / (const double& x, const octave_int64& y)
681{
682 return octave_int64 (x / static_cast<double> (y));
683}
684
685template <>
687operator / (const octave_uint64& x, const double& y)
688{
689 // The following cast changes the value to 2^64 (which is outside the range
690 // of `uint64_t`).
691 if (y >= 0 && y < static_cast<double> (octave_uint64::max ())
692 && y == octave::math::fix (y))
693 return x / octave_uint64 (y);
694 else
695 return x * (1.0/y);
696}
697
698template <>
700operator / (const octave_int64& x, const double& y)
701{
702 // The following cast changes the value to 2^63 (which is outside the range
703 // of `int64_t`).
704 if (fabs (y) < static_cast<double> (octave_int64::max ())
705 && y == octave::math::fix (y))
706 return x / octave_int64 (y);
707 else
708 return x * (1.0/y);
709}
710
711#define INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, T1, T2) \
712 template OCTAVE_API bool \
713 octave_int_cmp_op::emulate_mop<octave_int_cmp_op::OP> (T1 x, T2 y)
714
715#define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP) \
716 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, int64_t); \
717 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, uint64_t); \
718 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, int64_t, double); \
719 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, uint64_t, double)
720
727
728#endif
729
730template <typename T>
732pow (const octave_int<T>& a, const octave_int<T>& b)
733{
734 octave_int<T> retval;
735
738
739 if (b == zero || a == one)
740 retval = one;
741 else if (b < zero)
742 {
743 if (a == -one)
744 retval = (b.value () % 2) ? a : one;
745 else
746 retval = zero;
747 }
748 else
749 {
750 octave_int<T> a_val = a;
751 T b_val = b; // no need to do saturation on b
752
753 retval = a;
754
755 b_val -= 1;
756
757 while (b_val != 0)
758 {
759 if (b_val & 1)
760 retval = retval * a_val;
761
762 b_val = b_val >> 1;
763
764 if (b_val)
765 a_val = a_val * a_val;
766 }
767 }
768
769 return retval;
770}
771
772template <typename T>
774pow (const double& a, const octave_int<T>& b)
775{ return octave_int<T> (std::pow (a, b.double_value ())); }
776
777template <typename T>
779pow (const octave_int<T>& a, const double& b)
780{
781 return ((b >= 0 && b < std::numeric_limits<T>::digits
782 && b == octave::math::fix (b))
783 ? pow (a, octave_int<T> (static_cast<T> (b)))
784 : octave_int<T> (std::pow (a.double_value (), b)));
785}
786
787template <typename T>
789pow (const float& a, const octave_int<T>& b)
790{ return octave_int<T> (std::pow (a, b.float_value ())); }
791
792template <typename T>
794pow (const octave_int<T>& a, const float& b)
795{
796 return ((b >= 0 && b < std::numeric_limits<T>::digits
797 && b == octave::math::fix (b))
798 ? pow (a, octave_int<T> (static_cast<T> (b)))
800 static_cast<double> (b))));
801}
802
803// FIXME: Do we really need a differently named single-precision function
804// integer power function here instead of an overloaded one?
805template <typename T>
807powf (const float& a, const octave_int<T>& b)
808{ return octave_int<T> (pow (a, b.float_value ())); }
809
810template <typename T>
812powf (const octave_int<T>& a, const float& b)
813{
814 return ((b >= 0 && b < std::numeric_limits<T>::digits
815 && b == octave::math::fix (b))
816 ? pow (a, octave_int<T> (static_cast<T> (b)))
818 static_cast<double> (b))));
819}
820
821#define INSTANTIATE_INTTYPE(T) \
822 template class octave_int<T>; \
823 \
824 template OCTAVE_API octave_int<T> \
825 pow (const octave_int<T>&, const octave_int<T>&); \
826 \
827 template OCTAVE_API octave_int<T> \
828 pow (const double&, const octave_int<T>&); \
829 \
830 template OCTAVE_API octave_int<T> \
831 pow (const octave_int<T>&, const double&); \
832 \
833 template OCTAVE_API octave_int<T> \
834 pow (const float&, const octave_int<T>&); \
835 \
836 template OCTAVE_API octave_int<T> \
837 pow (const octave_int<T>&, const float&); \
838 \
839 template OCTAVE_API octave_int<T> \
840 powf (const float&, const octave_int<T>&); \
841 \
842 template OCTAVE_API octave_int<T> \
843 powf (const octave_int<T>&, const float&); \
844 \
845 template OCTAVE_API octave_int<T> \
846 bitshift (const octave_int<T>&, int, const octave_int<T>&);
847
852
857
858/*
859
860%!assert (intmax ("int64") / intmin ("int64"), int64 (-1))
861%!assert (intmin ("int64") / int64 (-1), intmax ("int64"))
862%!assert (int64 (2^63), intmax ("int64"))
863%!assert (uint64 (2^64), intmax ("uint64"))
864%!test
865%! a = 1.9*2^61; b = uint64 (a); b++; assert (b > a);
866%!test
867%! a = -1.9*2^61; b = int64 (a); b++; assert (b > a);
868%!test
869%! a = int64 (-2^60) + 2; assert (1.25*a == (5*a)/4);
870%!test
871%! a = uint64 (2^61) + 2; assert (1.25*a == (5*a)/4);
872%!assert (int32 (2^31+0.5), intmax ("int32"))
873%!assert (int32 (-2^31-0.5), intmin ("int32"))
874%!assert ((int64 (2^62)+1)^1, int64 (2^62)+1)
875%!assert ((int64 (2^30)+1)^2, int64 (2^60+2^31) + 1)
876
877%!assert <54382> (uint8 (char (128)), uint8 (128))
878%!assert <54382> (uint8 (char (255)), uint8 (255))
879%!assert <54382> (int8 (char (128)), int8 (128))
880%!assert <54382> (int8 (char (255)), int8 (255))
881
882%!assert <54382> (uint16 (char (128)), uint16 (128))
883%!assert <54382> (uint16 (char (255)), uint16 (255))
884%!assert <54382> (int16 (char (128)), int16 (128))
885%!assert <54382> (int16 (char (255)), int16 (255))
886
887%!assert <54382> (uint32 (char (128)), uint32 (128))
888%!assert <54382> (uint32 (char (255)), uint32 (255))
889%!assert <54382> (int32 (char (128)), int32 (128))
890%!assert <54382> (int32 (char (255)), int32 (255))
891
892%!assert <54382> (uint64 (char (128)), uint64 (128))
893%!assert <54382> (uint64 (char (255)), uint64 (255))
894%!assert <54382> (int64 (char (128)), int64 (128))
895%!assert <54382> (int64 (char (255)), int64 (255))
896*/
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 OCTAVE_API bool emulate_mop(double, int64_t)
static octave_int< T > max(void)
Definition: oct-inttypes.h:891
bool bool_value(void) const
Definition: oct-inttypes.h:839
static const octave_int s_zero
Definition: oct-inttypes.h:900
static const octave_int s_one
Definition: oct-inttypes.h:900
double double_value(void) const
Definition: oct-inttypes.h:843
float float_value(void) const
Definition: oct-inttypes.h:845
T value(void) const
Definition: oct-inttypes.h:830
unsigned int octave_begin_long_double_rounding(void)
void octave_end_long_double_rounding(unsigned int oldcw)
F77_RET_T const F77_DBLE * x
#define OCTAVE_API
Definition: main.in.cc:55
std::complex< double > w(std::complex< double > z, double relerr=0)
double fix(double x)
Definition: lo-mappers.h:118
double frexp(double x, int *expptr)
Definition: lo-mappers.cc:128
bool isnan(bool)
Definition: lo-mappers.h:178
bool isinf(double x)
Definition: lo-mappers.h:203
double round(double x)
Definition: lo-mappers.h:136
octave_int< int64_t > octave_int64
octave_int< uint64_t > octave_uint64
static void umul128(uint64_t x, uint64_t y, uint32_t w[4])
#define INSTANTIATE_INTTYPE(T)
octave_int< T > pow(const octave_int< T > &a, const octave_int< T > &b)
OCTAVE_API octave_uint64 operator-(const octave_uint64 &x, const double &y)
OCTAVE_API octave_uint64 operator+(const octave_uint64 &x, const double &y)
static double dbleget(bool sign, uint32_t mtis, int exp)
OCTAVE_API octave_uint64 operator*(const octave_uint64 &x, const double &y)
#define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP)
static void dblesplit(double x, bool &sign, uint64_t &mtis, int &exp)
OCTAVE_API octave_uint64 operator/(const double &x, const octave_uint64 &y)
#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 > powf(const float &a, const octave_int< T > &b)
T octave_int_abs(T x)
Definition: oct-inttypes.h:67