annotate liboctave/util/oct-inttypes.h @ 18627:37c300acfcfd stable

don't truncate when casting char to uintN values (bug #42054) * oct-inttypes.h (octave_int<T>::octave_int (char)): New special case constructor. * oct-inttypes.cc: New tests.
author John W. Eaton <jwe@octave.org>
date Sat, 05 Apr 2014 15:09:09 -0400
parents 284e5c87f27b
children 5b6901b06106
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1 /*
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
2
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
3 Copyright (C) 2004-2013 John W. Eaton
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10436
diff changeset
4 Copyright (C) 2008-2009 Jaroslav Hajek
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
5
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
7
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6764
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6764
diff changeset
11 option) any later version.
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
12
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
16 for more details.
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
17
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6764
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6764
diff changeset
20 <http://www.gnu.org/licenses/>.
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
21
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
22 */
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
23
17822
ebb3ef964372 maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents: 17769
diff changeset
24 #if !defined (octave_oct_inttypes_h)
ebb3ef964372 maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents: 17769
diff changeset
25 #define octave_oct_inttypes_h 1
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
26
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
27 #include <cstdlib>
6482
7e958a1532c6 [project @ 2007-04-04 02:37:07 by jwe]
jwe
parents: 6402
diff changeset
28
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
29 #include <limits>
8950
d865363208d6 include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents: 8920
diff changeset
30 #include <iosfwd>
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
31
8731
5abe5ae55465 use lo-traits in oct-inttypes
Jaroslav Hajek <highegg@gmail.com>
parents: 8333
diff changeset
32 #include "lo-traits.h"
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
33 #include "lo-math.h"
4969
52f808101130 [project @ 2004-09-08 02:44:04 by jwe]
jwe
parents: 4968
diff changeset
34 #include "lo-mappers.h"
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
35
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
36 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
37 inline long double xround (long double x) { return roundl (x); }
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
38 inline long double xisnan (long double x)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
39 { return xisnan (static_cast<double> (x)); }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
40 #endif
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
41
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
42 // FIXME: we define this by our own because some compilers, such as
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8319
diff changeset
43 // MSVC, do not provide std::abs (int64_t) and std::abs (uint64_t). In
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8319
diff changeset
44 // the future, it should go away in favor of std::abs.
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
45 template <class T>
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
46 inline T octave_int_abs (T x) { return x >= 0 ? x : -x; }
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
47
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
48 // Query for an integer type of certain sizeof, and signedness.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
49 template<int qsize, bool qsigned>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
50 struct query_integer_type
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
51 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
52 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
53 static const bool registered = false;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
54 typedef void type; // Void shall result in a compile-time error if we
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
55 // attempt to use it in computations.
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
56 };
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
57
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
58 #define REGISTER_INT_TYPE(TYPE) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
59 template <> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
60 class query_integer_type<sizeof (TYPE), std::numeric_limits<TYPE>::is_signed> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
61 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
62 public: \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
63 static const bool registered = true; \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
64 typedef TYPE type; \
7598
a89b3fa632ee partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents: 7596
diff changeset
65 }
7596
6929e40fc597 compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents: 7534
diff changeset
66
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
67 // No two registered integers can share sizeof and signedness.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
68 REGISTER_INT_TYPE (int8_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
69 REGISTER_INT_TYPE (uint8_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
70 REGISTER_INT_TYPE (int16_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
71 REGISTER_INT_TYPE (uint16_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
72 REGISTER_INT_TYPE (int32_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
73 REGISTER_INT_TYPE (uint32_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
74 REGISTER_INT_TYPE (int64_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
75 REGISTER_INT_TYPE (uint64_t);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
76
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
77 // Rationale: Comparators have a single static method, rel(), that returns the
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
78 // result of the binary relation. They also have two static boolean fields:
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
79 // ltval, gtval determine the value of x OP y if x < y, x > y, respectively.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
80 #define REGISTER_OCTAVE_CMP_OP(NM,OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
81 class NM \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
82 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
83 public: \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
84 static const bool ltval = (0 OP 1), gtval = (1 OP 0); \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
85 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
86 static bool op (T x, T y) { return x OP y; } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
87 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
88
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
89 // We also provide two special relations: ct, yielding always true, and cf,
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
90 // yielding always false.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
91 #define REGISTER_OCTAVE_CONST_OP(NM,value) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
92 class NM \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
93 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
94 public: \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
95 static const bool ltval = value, gtval = value; \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
96 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
97 static bool op (T, T) { return value; } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
98 }
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
99
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
100 // Handles non-homogeneous integer comparisons. Avoids doing useless tests.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
101 class octave_int_cmp_op
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
102 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
103 // This determines a suitable promotion type for T1 when meeting T2 in a
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
104 // binary relation. If promotion to int or T2 is safe, it is used. Otherwise,
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
105 // the signedness of T1 is preserved and it is widened if T2 is wider.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
106 // Notice that if this is applied to both types, they must end up with equal
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
107 // size.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
108 template <class T1, class T2>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
109 class prom
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
110 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
111 // Promote to int?
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
112 static const bool pint = (sizeof (T1) < sizeof (int)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
113 && sizeof (T2) < sizeof (int));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
114 static const bool t1sig = std::numeric_limits<T1>::is_signed;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
115 static const bool t2sig = std::numeric_limits<T2>::is_signed;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
116 static const bool psig =
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
117 (pint || (sizeof (T2) > sizeof (T1) && t2sig) || t1sig);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
118 static const int psize =
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
119 (pint ? sizeof (int) : (sizeof (T2) > sizeof (T1)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
120 ? sizeof (T2) : sizeof (T1)));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
121 public:
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
122 typedef typename query_integer_type<psize, psig>::type type;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
123 };
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
124
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
125 // Implements comparisons between two types of equal size but
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
126 // possibly different signedness.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
127 template<class xop, int size>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
128 class uiop
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
129 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
130 typedef typename query_integer_type<size, false>::type utype;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
131 typedef typename query_integer_type<size, true>::type stype;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
132 public:
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
133 static bool op (utype x, utype y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
134 { return xop::op (x, y); }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
135 static bool op (stype x, stype y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
136 { return xop::op (x, y); }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
137 static bool op (stype x, utype y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
138 { return (x < 0) ? xop::ltval : xop::op (static_cast<utype> (x), y); }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
139 static bool op (utype x, stype y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
140 { return (y < 0) ? xop::gtval : xop::op (x, static_cast<utype> (y)); }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
141 };
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
142
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
143 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
144 REGISTER_OCTAVE_CMP_OP (lt, <);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
145 REGISTER_OCTAVE_CMP_OP (le, <=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
146 REGISTER_OCTAVE_CMP_OP (gt, >);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
147 REGISTER_OCTAVE_CMP_OP (ge, >=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
148 REGISTER_OCTAVE_CMP_OP (eq, ==);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
149 REGISTER_OCTAVE_CMP_OP (ne, !=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
150 REGISTER_OCTAVE_CONST_OP (ct, true);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
151 REGISTER_OCTAVE_CONST_OP (cf, false);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
152
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
153 // Universal comparison operation.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
154 template<class xop, class T1, class T2>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
155 static bool
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
156 op (T1 x, T2 y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
157 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
158 typedef typename prom<T1, T2>::type PT1;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
159 typedef typename prom<T2, T1>::type PT2;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
160 return uiop<xop, sizeof (PT1)>::op (static_cast<PT1> (x),
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
161 static_cast<PT2> (y));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
162 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
163
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
164 public:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
165
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
166 // Mixed comparisons
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
167 template <class xop, class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
168 static bool
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
169 mop (T x, double y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
170 { return xop::op (static_cast<double> (x), y); }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
171
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
172 template <class xop, class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
173 static bool
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
174 mop (double x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
175 { return xop::op (x, static_cast<double> (y)); }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
176
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
177 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
178 #define DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
179 template <class xop> static OCTAVE_API bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
180 external_mop (double, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
181 template <class xop> static OCTAVE_API bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
182 external_mop (T, double)
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
183
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
184 DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS (int64_t);
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
185 DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS (uint64_t);
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
186 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
187
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
188 // Typecasting to doubles won't work properly for 64-bit integers --
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
189 // they lose precision.
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
190 // If we have long doubles, use them...
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
191 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
192 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
193 #define DEFINE_LONG_DOUBLE_CMP_OP(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
194 template <class xop> \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
195 static bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
196 mop (double x, T y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
197 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
198 return external_mop<xop> (x, y); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
199 } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
200 template <class xop> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
201 static bool \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
202 mop (T x, double y) \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
203 { \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
204 return external_mop<xop> (x, y); \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
205 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
206 #else
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
207 #define DEFINE_LONG_DOUBLE_CMP_OP(T) \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
208 template <class xop> \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
209 static bool \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
210 mop (double x, T y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
211 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
212 return xop::op (static_cast<long double> (x), \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
213 static_cast<long double> (y)); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
214 } \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
215 template <class xop> \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
216 static bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
217 mop (T x, double y) \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
218 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
219 return xop::op (static_cast<long double> (x), \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
220 static_cast<long double> (y)); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
221 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
222 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
223 #else
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
224 // ... otherwise, use external handlers
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
225
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
226 // FIXME: We could declare directly the mop methods as external,
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
227 // but we can't do this because bugs in gcc (<= 4.3) prevent
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
228 // explicit instantiations later in that case.
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
229 #define DEFINE_LONG_DOUBLE_CMP_OP(T) \
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
230 template <class xop> static OCTAVE_API bool \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
231 emulate_mop (double, T); \
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
232 template <class xop> \
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
233 static bool \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
234 mop (double x, T y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
235 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
236 return emulate_mop<xop> (x, y); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
237 } \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
238 template <class xop> static OCTAVE_API bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
239 emulate_mop (T, double); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
240 template <class xop> \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
241 static bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
242 mop (T x, double y) \
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
243 { \
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
244 return emulate_mop<xop> (x, y); \
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
245 }
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
246 #endif
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
247
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
248 DEFINE_LONG_DOUBLE_CMP_OP(int64_t)
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
249 DEFINE_LONG_DOUBLE_CMP_OP(uint64_t)
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8731
diff changeset
250
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
251 #undef DEFINE_LONG_DOUBLE_CMP_OP
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
252 };
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
253
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
254 // Base integer class. No data, just conversion methods and exception flags.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
255 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
256 class octave_int_base
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
257 {
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
258 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
259
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
260 static T min_val () { return std::numeric_limits<T>:: min (); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
261 static T max_val () { return std::numeric_limits<T>:: max (); }
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
262
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
263 // Convert integer value.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
264 template <class S>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
265 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
266 truncate_int (const S& value)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
267 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
268 // An exhaustive test whether the max and/or min check can be omitted.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
269 static const bool t_is_signed = std::numeric_limits<T>::is_signed;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
270 static const bool s_is_signed = std::numeric_limits<S>::is_signed;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
271 static const int t_size = sizeof (T), s_size = sizeof (S);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
272 static const bool omit_chk_min =
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
273 (! s_is_signed || (t_is_signed && t_size >= s_size));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
274 static const bool omit_chk_max =
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
275 (t_size > s_size || (t_size == s_size
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
276 && (! t_is_signed || s_is_signed)));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
277 // If the check can be omitted, substitute constant false relation.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
278 typedef octave_int_cmp_op::cf cf;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
279 typedef octave_int_cmp_op::lt lt;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
280 typedef octave_int_cmp_op::gt gt;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
281 typedef typename if_then_else<omit_chk_min, cf, lt>::result chk_min;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
282 typedef typename if_then_else<omit_chk_max, cf, gt>::result chk_max;
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
283
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
284 // Efficiency of the following depends on inlining and dead code
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
285 // elimination, but that should be a piece of cake for most compilers.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
286 if (chk_min::op (value, static_cast<S> (min_val ())))
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
287 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
288 return min_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
289 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
290 else if (chk_max::op (value, static_cast<S> (max_val ())))
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
291 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
292 return max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
293 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
294 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
295 return static_cast<T> (value);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
296 }
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
297
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
298 private:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
299
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
300 // Computes a real-valued threshold for a max/min check.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
301 template <class S>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
302 static S
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
303 compute_threshold (S val, T orig_val)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
304 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
305 val = xround (val); // Fool optimizations (maybe redundant)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
306 // If val is even, but orig_val is odd, we're one unit off.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
307 if (orig_val % 2 && val / 2 == xround (val / 2))
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
308 // FIXME: is this always correct?
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
309 val *= (static_cast<S>(1) - (std::numeric_limits<S>::epsilon () / 2));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
310 return val;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
311 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
312
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
313 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
314 // Convert a real number (check NaN and non-int).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
315 template <class S>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
316 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
317 convert_real (const S& value)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
318 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
319 // Compute proper thresholds.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
320 static const S thmin = compute_threshold (static_cast<S> (min_val ()),
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
321 min_val ());
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
322 static const S thmax = compute_threshold (static_cast<S> (max_val ()),
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
323 max_val ());
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
324 if (xisnan (value))
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
325 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
326 return static_cast<T> (0);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
327 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
328 else if (value < thmin)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
329 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
330 return min_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
331 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
332 else if (value > thmax)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
333 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
334 return max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
335 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
336 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
337 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
338 S rvalue = xround (value);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
339 return static_cast<T> (rvalue);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
340 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
341 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
342 };
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
343
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
344 // Saturated (homogeneous) integer arithmetics. The signed and unsigned
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
345 // implementations are significantly different, so we implement another layer
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
346 // and completely specialize. Arithmetics inherits from octave_int_base so that
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
347 // it can use its exceptions and truncation functions.
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
348
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
349 template <class T, bool is_signed>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
350 class octave_int_arith_base
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
351 { };
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
352
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
353 // Unsigned arithmetics. C++ standard requires it to be modular, so the
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
354 // overflows can be handled efficiently and reliably.
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
355 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
356 class octave_int_arith_base<T, false> : octave_int_base<T>
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
357 {
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
358 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
359
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
360 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
361 abs (T x) { return x; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
362
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
363 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
364 signum (T x) { return x ? static_cast<T> (1) : static_cast<T> (0); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
365
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
366 // Shifts do not overflow.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
367 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
368 rshift (T x, int n) { return x >> n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
369
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
370 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
371 lshift (T x, int n) { return x << n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
372
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
373 static T
10405
cc69a17ec801 remove integer math warnings
Jaroslav Hajek <highegg@gmail.com>
parents: 10346
diff changeset
374 minus (T)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
375 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
376 return static_cast<T> (0);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
377 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
378
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
379 // the overflow behaviour for unsigned integers is guaranteed by C/C++,
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
380 // so the following should always work.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
381 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
382 add (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
383 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
384 T u = x + y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
385 if (u < x)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
386 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
387 u = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
388 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
389 return u;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
390 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
391
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
392 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
393 sub (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
394 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
395 T u = x - y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
396 if (u > x)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
397 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
398 u = 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
399 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
400 return u;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
401 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
402
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
403 // Multiplication is done using promotion to wider integer type. If there is
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
404 // no suitable promotion type, this operation *MUST* be specialized.
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
405 static T mul (T x, T y) { return mul_internal (x, y); }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
406
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
407 static T
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
408 mul_internal (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
409 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
410 // Promotion type for multiplication (if exists).
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
411 typedef typename query_integer_type<2*sizeof (T), false>::type mptype;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
412 return octave_int_base<T>::truncate_int (static_cast<mptype> (x)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
413 * static_cast<mptype> (y));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
414 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
415
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
416 // Division with rounding to nearest. Note that / and % are probably
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
417 // computed by a single instruction.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
418 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
419 div (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
420 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
421 if (y != 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
422 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
423 T z = x / y, w = x % y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
424 if (w >= y-w) z += 1;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
425 return z;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
426 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
427 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
428 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
429 return x ? octave_int_base<T>::max_val () : 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
430 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
431 }
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
432
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
433 // Remainder.
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
434 static T
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
435 rem (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
436 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
437 return y != 0 ? x % y : 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
438 }
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
439
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
440 // Modulus. Note the weird y = 0 case for Matlab compatibility.
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
441 static T
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
442 mod (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
443 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
444 return y != 0 ? x % y : x;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
445 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
446 };
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
447
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
448 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
449
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
450 // Handle 64-bit multiply using long double
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
451
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
452 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
453
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
454 extern OCTAVE_API uint64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
455 octave_external_uint64_uint64_mul (uint64_t, uint64_t);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
456
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
457 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
458
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
459 template <>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
460 inline uint64_t
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
461 octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t x, uint64_t y)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
462 {
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
463 uint64_t retval;
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
464
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
465 long double p = static_cast<long double> (x) * static_cast<long double> (y);
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
466
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
467 if (p > static_cast<long double> (octave_int_base<uint64_t>::max_val ()))
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
468 retval = octave_int_base<uint64_t>::max_val ();
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
469 else
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
470 retval = static_cast<uint64_t> (p);
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
471
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
472 return retval;
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
473 }
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
474
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
475 template <>
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
476 inline uint64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
477 octave_int_arith_base<uint64_t, false>::mul (uint64_t x, uint64_t y)
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
478 {
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
479 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
480 return octave_external_uint64_uint64_mul (x, y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
481 #else
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
482 return mul_internal (x, y);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
483 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
484 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
485
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
486 #else
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
487
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
488 // Special handler for 64-bit integer multiply.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
489 template <>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
490 OCTAVE_API uint64_t
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
491 octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t, uint64_t);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
492
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
493 #endif
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
494
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
495 // Signed integer arithmetics.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
496 // Rationale: If HAVE_FAST_INT_OPS is defined, the following conditions
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
497 // should hold:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
498 // 1. Signed numbers are represented by twos complement
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
499 // (see <http://en.wikipedia.org/wiki/Two%27s_complement>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
500 // 2. static_cast to unsigned int counterpart works like interpreting
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
501 // the signed bit pattern as unsigned (and is thus zero-cost).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
502 // 3. Signed addition and subtraction yield the same bit results as unsigned.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
503 // (We use casts to prevent optimization interference, so there is no
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
504 // need for things like -ftrapv).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
505 // 4. Bit operations on signed integers work like on unsigned integers,
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
506 // except for the shifts. Shifts are arithmetic.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
507 //
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
508 // The above conditions are satisfied by most modern platforms. If
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
509 // HAVE_FAST_INT_OPS is defined, bit tricks and wraparound arithmetics are used
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
510 // to avoid conditional jumps as much as possible, thus being friendly to
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
511 // modern pipeline processor architectures.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
512 // Otherwise, we fall back to a bullet-proof code that only uses assumptions
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
513 // guaranteed by the standard.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
514
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
515 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
516 class octave_int_arith_base<T, true> : octave_int_base<T>
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
517 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
518 // The corresponding unsigned type.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
519 typedef typename query_integer_type<sizeof (T), false>::type UT;
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
520 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
521
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
522 // Returns 1 for negative number, 0 otherwise.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
523 static T
13734
28ddb3150943 Rename signbit to __signbit, until the problem is fixed in gnulib.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13012
diff changeset
524 __signbit (T x)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
525 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
526 #ifdef HAVE_FAST_INT_OPS
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
527 return static_cast<UT> (x) >> std::numeric_limits<T>::digits;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
528 #else
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
529 return (x < 0) ? 1 : 0;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
530 #endif
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
531 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
532
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
533 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
534 abs (T x)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
535 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
536 #ifdef HAVE_FAST_INT_OPS
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
537 // This is close to how GCC does std::abs, but we can't just use std::abs,
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
538 // because its behaviour for INT_MIN is undefined and the compiler could
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
539 // discard the following test.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
540 T m = x >> std::numeric_limits<T>::digits;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
541 T y = (x ^ m) - m;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
542 if (y < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
543 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
544 y = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
545 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
546 return y;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
547 #else
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
548 // -INT_MAX is safe because C++ actually allows only three implementations
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
549 // of integers: sign & magnitude, ones complement and twos complement.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
550 // The first test will, with modest optimizations, evaluate at compile
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
551 // time, and maybe eliminate the branch completely.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
552 T y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
553 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val ()
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
554 && x == octave_int_base<T>::min_val ())
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
555 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
556 y = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
557 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
558 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
559 y = (x < 0) ? -x : x;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
560 return y;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
561 #endif
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
562 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
563
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
564 static T
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
565 signum (T x)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
566 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
567 // With modest optimizations, this will compile without a jump.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
568 return ((x > 0) ? 1 : 0) - __signbit (x);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
569 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
570
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
571 // FIXME: we do not have an authority what signed shifts should
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8319
diff changeset
572 // exactly do, so we define them the easy way. Note that Matlab does
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8319
diff changeset
573 // not define signed shifts.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
574
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
575 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
576 rshift (T x, int n) { return x >> n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
577
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
578 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
579 lshift (T x, int n) { return x << n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
580
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
581 // Minus has problems similar to abs.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
582 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
583 minus (T x)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
584 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
585 #ifdef HAVE_FAST_INT_OPS
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
586 T y = -x;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
587 if (y == octave_int_base<T>::min_val ())
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
588 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
589 --y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
590 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
591 return y;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
592 #else
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
593 T y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
594 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val ()
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
595 && x == octave_int_base<T>::min_val ())
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
596 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
597 y = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
598 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
599 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
600 y = -x;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
601 return y;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
602 #endif
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
603 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
604
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
605 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
606 add (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
607 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
608 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
609 // The typecasts do nothing, but they are here to prevent an optimizing
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
610 // compiler from interfering. Also, the signed operations on small types
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
611 // actually return int.
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
612 T u = static_cast<UT> (x) + static_cast<UT> (y);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
613 T ux = u ^ x, uy = u ^ y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
614 if ((ux & uy) < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
615 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
616 u = octave_int_base<T>::max_val () + __signbit (~u);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
617 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
618 return u;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
619 #else
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
620 // We shall carefully avoid anything that may overflow.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
621 T u;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
622 if (y < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
623 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
624 if (x < octave_int_base<T>::min_val () - y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
625 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
626 u = octave_int_base<T>::min_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
627 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
628 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
629 u = x + y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
630 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
631 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
632 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
633 if (x > octave_int_base<T>::max_val () - y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
634 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
635 u = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
636 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
637 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
638 u = x + y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
639 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
640
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
641 return u;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
642 #endif
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
643 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
644
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
645 // This is very similar to addition.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
646 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
647 sub (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
648 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
649 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
650 // The typecasts do nothing, but they are here to prevent an optimizing
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
651 // compiler from interfering. Also, the signed operations on small types
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
652 // actually return int.
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
653 T u = static_cast<UT> (x) - static_cast<UT> (y);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
654 T ux = u ^ x, uy = u ^ ~y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
655 if ((ux & uy) < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
656 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
657 u = octave_int_base<T>::max_val () + __signbit (~u);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
658 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
659 return u;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
660 #else
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
661 // We shall carefully avoid anything that may overflow.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
662 T u;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
663 if (y < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
664 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
665 if (x > octave_int_base<T>::max_val () + y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
666 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
667 u = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
668 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
669 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
670 u = x - y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
671 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
672 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
673 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
674 if (x < octave_int_base<T>::min_val () + y)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
675 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
676 u = octave_int_base<T>::min_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
677 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
678 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
679 u = x - y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
680 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
681
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
682 return u;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
683 #endif
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
684 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
685
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
686 // Multiplication is done using promotion to wider integer type. If there is
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
687 // no suitable promotion type, this operation *MUST* be specialized.
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
688 static T mul (T x, T y) { return mul_internal (x, y); }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
689
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
690 static T
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
691 mul_internal (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
692 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
693 // Promotion type for multiplication (if exists).
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
694 typedef typename query_integer_type<2*sizeof (T), true>::type mptype;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
695 return octave_int_base<T>::truncate_int (static_cast<mptype> (x)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
696 * static_cast<mptype> (y));
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
697 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
698
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
699 // Division.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
700 static T
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
701 div (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
702 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
703 T z;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
704 if (y == 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
705 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
706 if (x < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
707 z = octave_int_base<T>::min_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
708 else if (x != 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
709 z = octave_int_base<T>::max_val ();
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
710 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
711 z = 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
712 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
713 else if (y < 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
714 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
715 // This is a special case that overflows as well.
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
716 if (y == -1 && x == octave_int_base<T>::min_val ())
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
717 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
718 z = octave_int_base<T>::max_val ();
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
719 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
720 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
721 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
722 z = x / y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
723 // Can't overflow, but std::abs (x) can!
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
724 T w = -octave_int_abs (x % y);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
725 if (w <= y - w)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
726 z -= 1 - (__signbit (x) << 1);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
727 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
728 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
729 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
730 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
731 z = x / y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
732 // FIXME: this is a workaround due to MSVC's absence of
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
733 // std::abs (int64_t). The call to octave_int_abs can't
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
734 // overflow, but std::abs (x) can!
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
735 T w = octave_int_abs (x % y);
8333
9238637cb81c style fixes
John W. Eaton <jwe@octave.org>
parents: 8319
diff changeset
736
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
737 if (w >= y - w)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
738 z += 1 - (__signbit (x) << 1);
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
739 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
740 return z;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
741 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
742
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
743 // Remainder.
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
744 static T
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
745 rem (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
746 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
747 return y != 0 ? x % y : 0;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
748 }
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
749
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
750 // Modulus. Note the weird y = 0 case for Matlab compatibility.
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
751 static T
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
752 mod (T x, T y)
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
753 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
754 if (y != 0)
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
755 {
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
756 T r = x % y;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
757 return ((r < 0) != (y < 0)) ? r + y : r;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
758 }
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
759 else
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
760 return x;
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
761 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
762 };
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
763
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
764 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
765
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
766 // Handle 64-bit multiply using long double
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
767
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
768 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
769
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
770 extern OCTAVE_API int64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
771 octave_external_int64_int64_mul (int64_t, int64_t);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
772
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
773 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
774
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
775 template <>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
776 inline int64_t
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
777 octave_int_arith_base<int64_t, true>::mul_internal (int64_t x, int64_t y)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
778 {
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
779 int64_t retval;
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
780
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
781 long double p = static_cast<long double> (x) * static_cast<long double> (y);
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
782
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
783 // NOTE: We could maybe do it with a single branch if HAVE_FAST_INT_OPS,
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
784 // but it would require one more runtime conversion, so the question is
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
785 // whether it would really be faster.
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
786 if (p > static_cast<long double> (octave_int_base<int64_t>::max_val ()))
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
787 retval = octave_int_base<int64_t>::max_val ();
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
788 else if (p < static_cast<long double> (octave_int_base<int64_t>::min_val ()))
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
789 retval = octave_int_base<int64_t>::min_val ();
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
790 else
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
791 retval = static_cast<int64_t> (p);
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
792
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
793 return retval;
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
794 }
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
795
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
796 template <>
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
797 inline int64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
798 octave_int_arith_base<int64_t, true>::mul (int64_t x, int64_t y)
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
799 {
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
800 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
801 return octave_external_int64_int64_mul (x, y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
802 #else
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
803 return mul_internal (x, y);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
804 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
805 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
806
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
807 #else
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
808
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
809 // Special handler for 64-bit integer multiply.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
810 template <>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
811 OCTAVE_API int64_t
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
812 octave_int_arith_base<int64_t, true>::mul_internal (int64_t, int64_t);
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
813
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
814 #endif
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
815
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
816 // This class simply selects the proper arithmetics.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
817 template<class T>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
818 class octave_int_arith
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
819 : public octave_int_arith_base<T, std::numeric_limits<T>::is_signed>
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
820 { };
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
821
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
822 template <class T>
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
823 class
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
824 octave_int : public octave_int_base<T>
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
825 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
826 public:
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
827 typedef T val_type;
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
828
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
829 octave_int (void) : ival () { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
830
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
831 octave_int (T i) : ival (i) { }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
832
18627
37c300acfcfd don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents: 18252
diff changeset
833 // Always treat characters as unsigned.
37c300acfcfd don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents: 18252
diff changeset
834 octave_int (char c)
37c300acfcfd don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents: 18252
diff changeset
835 : ival (octave_int_base<T>::truncate_int (static_cast<unsigned char> (c)))
37c300acfcfd don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents: 18252
diff changeset
836 { }
37c300acfcfd don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents: 18252
diff changeset
837
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
838 octave_int (double d) : ival (octave_int_base<T>::convert_real (d)) { }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
839
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
840 octave_int (float d) : ival (octave_int_base<T>::convert_real (d)) { }
6402
fe9817a6ee98 [project @ 2007-03-13 02:25:31 by jwe]
jwe
parents: 6108
diff changeset
841
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
842 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
843 octave_int (long double d) : ival (octave_int_base<T>::convert_real (d)) { }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
844 #endif
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
845
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
846 octave_int (bool b) : ival (b) { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
847
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
848 template <class U>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
849 octave_int (const U& i) : ival(octave_int_base<T>::truncate_int (i)) { }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
850
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
851 template <class U>
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
852 octave_int (const octave_int<U>& i)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
853 : ival (octave_int_base<T>::truncate_int (i.value ())) { }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
854
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
855 octave_int (const octave_int<T>& i) : ival (i.ival) { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
856
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
857 octave_int& operator = (const octave_int<T>& i)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
858 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
859 ival = i.ival;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
860 return *this;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
861 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
862
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
863 T value (void) const { return ival; }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
864
4949
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
865 const unsigned char * iptr (void) const
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
866 { return reinterpret_cast<const unsigned char *> (& ival); }
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
867
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
868 bool operator ! (void) const { return ! ival; }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
869
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
870 bool bool_value (void) const { return static_cast<bool> (value ()); }
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
871
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
872 char char_value (void) const { return static_cast<char> (value ()); }
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
873
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
874 double double_value (void) const { return static_cast<double> (value ()); }
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
875
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
876 float float_value (void) const { return static_cast<float> (value ()); }
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
877
7177
57d2546ad8d5 [project @ 2007-11-14 22:21:35 by jwe]
jwe
parents: 7017
diff changeset
878 operator T (void) const { return value (); }
57d2546ad8d5 [project @ 2007-11-14 22:21:35 by jwe]
jwe
parents: 7017
diff changeset
879
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
880 // char and bool operators intentionally omitted.
5533
667ad2becb63 [project @ 2005-11-10 21:40:48 by jwe]
jwe
parents: 5420
diff changeset
881
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
882 operator double (void) const { return double_value (); }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
883
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
884 operator float (void) const { return float_value (); }
5030
160365410ad4 [project @ 2004-09-24 03:22:23 by jwe]
jwe
parents: 5029
diff changeset
885
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
886 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
887 operator + () const
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
888 { return *this; }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
889
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
890 // unary operators & mappers
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
891 #define OCTAVE_INT_UN_OP(OPNAME,NAME) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
892 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
893 OPNAME () const \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
894 { return octave_int_arith<T>::NAME (ival); }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
895
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
896 OCTAVE_INT_UN_OP(operator -, minus)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
897 OCTAVE_INT_UN_OP(abs, abs)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
898 OCTAVE_INT_UN_OP(signum, signum)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
899
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
900 #undef OCTAVE_INT_UN_OP
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
901
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
902 // Homogeneous binary integer operations.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
903 #define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
904 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
905 operator OP (const ARGT& y) const \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
906 { return octave_int_arith<T>::NAME (ival, y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
907 inline octave_int<T>& \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
908 operator OP##= (const ARGT& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
909 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
910 ival = octave_int_arith<T>::NAME (ival, y); \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
911 return *this; \
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
912 }
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
913
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
914 OCTAVE_INT_BIN_OP(+, add, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
915 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
916 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
917 OCTAVE_INT_BIN_OP(/, div, octave_int<T>)
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
918 OCTAVE_INT_BIN_OP(%, rem, octave_int<T>)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
919 OCTAVE_INT_BIN_OP(<<, lshift, int)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
920 OCTAVE_INT_BIN_OP(>>, rshift, int)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
921
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
922 #undef OCTAVE_INT_BIN_OP
7503
8c32f95c2639 convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents: 7198
diff changeset
923
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
924 static octave_int<T> min (void) { return std::numeric_limits<T>::min (); }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
925 static octave_int<T> max (void) { return std::numeric_limits<T>::max (); }
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
926
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
927 static int nbits (void) { return std::numeric_limits<T>::digits; }
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
928
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14168
diff changeset
929 static int byte_size (void) { return sizeof (T); }
4949
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
930
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
931 static const char *type_name ();
7997
2b8952e133c9 implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents: 7922
diff changeset
932
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
933 // The following are provided for convenience.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
934 static const octave_int zero, one;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
935
5900
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
936 // Unsafe. This function exists to support the MEX interface.
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
937 // You should not use it anywhere else.
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
938 void *mex_get_data (void) const { return const_cast<T *> (&ival); }
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
939
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
940 private:
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
941
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
942 T ival;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
943 };
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
944
10436
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
945 template <class T>
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
946 inline octave_int<T>
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
947 rem (const octave_int<T>& x, const octave_int<T>& y)
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
948 { return octave_int_arith<T>::rem (x.value (), y.value ()); }
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
949
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
950 template <class T>
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
951 inline octave_int<T>
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
952 mod (const octave_int<T>& x, const octave_int<T>& y)
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
953 { return octave_int_arith<T>::mod (x.value (), y.value ()); }
00219bdd2d17 implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents: 10405
diff changeset
954
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
955 // No mixed integer binary operations!
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
956
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
957 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
958 inline bool
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
959 xisnan (const octave_int<T>&)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
960 { return false; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
961
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
962 // FIXME: can/should any of these be inline?
7997
2b8952e133c9 implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents: 7922
diff changeset
963
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
964 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
965 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
966 pow (const octave_int<T>&, const octave_int<T>&);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
967
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
968 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
969 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
970 pow (const double& a, const octave_int<T>& b);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
971
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
972 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
973 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
974 pow (const octave_int<T>& a, const double& b);
7922
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7789
diff changeset
975
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7789
diff changeset
976 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
977 extern OCTAVE_API octave_int<T>
13012
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
978 pow (const float& a, const octave_int<T>& b);
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
979
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
980 template <class T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
981 extern OCTAVE_API octave_int<T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
982 pow (const octave_int<T>& a, const float& b);
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
983
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
984 // FIXME: Do we really need a differently named single-precision
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
985 // function integer power function here instead of an overloaded
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
986 // one?
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
987 template <class T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
988 extern OCTAVE_API octave_int<T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
989 powf (const float& a, const octave_int<T>& b);
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
990
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
991 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
992 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
993 powf (const octave_int<T>& a, const float& b);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
994
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
995 // Binary relations
4953
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
996
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
997 #define OCTAVE_INT_CMP_OP(OP, NAME) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
998 template<class T1, class T2> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
999 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1000 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1001 { return octave_int_cmp_op::op<octave_int_cmp_op::NAME, T1, T2> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1002 (x.value (), y.value ()); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1003
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1004 OCTAVE_INT_CMP_OP (<, lt)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1005 OCTAVE_INT_CMP_OP (<=, le)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1006 OCTAVE_INT_CMP_OP (>, gt)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1007 OCTAVE_INT_CMP_OP (>=, ge)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1008 OCTAVE_INT_CMP_OP (==, eq)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1009 OCTAVE_INT_CMP_OP (!=, ne)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1010
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1011 #undef OCTAVE_INT_CMP_OP
4953
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
1012
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
1013 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1014 inline std::ostream&
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1015 operator << (std::ostream& os, const octave_int<T>& ival)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1016 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1017 os << ival.value ();
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1018 return os;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1019 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1020
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1021 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1022 inline std::istream&
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1023 operator >> (std::istream& is, octave_int<T>& ival)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1024 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1025 T tmp = 0;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1026 is >> tmp;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1027 ival = tmp;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1028 return is;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1029 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1030
18252
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1031 // We need to specialise for char and unsigned char because
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1032 // std::operator<< and std::operator>> are overloaded to input and
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1033 // output the ASCII character values instead of a representation of
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1034 // their numerical value (e.g. os << char(10) outputs a space instead
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1035 // of outputting the characters '1' and '0')
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1036
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1037 template <>
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1038 inline std::ostream&
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1039 operator << (std::ostream& os, const octave_int<int8_t>& ival)
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1040 {
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1041 os << static_cast<int> (ival.value ());
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1042 return os;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1043 }
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1044
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1045 template <>
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1046 inline std::ostream&
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1047 operator << (std::ostream& os, const octave_int<uint8_t>& ival)
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1048 {
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1049 os << static_cast<unsigned int> (ival.value ());
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1050 return os;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1051 }
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1052
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1053
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1054 template <>
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1055 inline std::istream&
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1056 operator >> (std::istream& is, octave_int<int8_t>& ival)
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1057 {
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1058 int tmp = 0;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1059 is >> tmp;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1060 ival = static_cast<int8_t> (tmp);
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1061 return is;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1062 }
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1063
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1064 template <>
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1065 inline std::istream&
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1066 operator >> (std::istream& is, octave_int<uint8_t>& ival)
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1067 {
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1068 unsigned int tmp = 0;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1069 is >> tmp;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1070 ival = static_cast<uint8_t> (tmp);
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1071 return is;
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1072 }
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1073
284e5c87f27b Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 18017
diff changeset
1074
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1075 // Bitwise operations
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7598
diff changeset
1076
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1077 #define OCTAVE_INT_BITCMP_OP(OP) \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1078 template <class T> \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1079 octave_int<T> \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1080 operator OP (const octave_int<T>& x, const octave_int<T>& y) \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1081 { return x.value () OP y.value (); }
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1082
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1083 OCTAVE_INT_BITCMP_OP (&)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1084 OCTAVE_INT_BITCMP_OP (|)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1085 OCTAVE_INT_BITCMP_OP (^)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1086
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1087 #undef OCTAVE_INT_BITCMP_OP
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
1088
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1089 // General bit shift.
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1090 template <class T>
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1091 octave_int<T>
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4919
diff changeset
1092 bitshift (const octave_int<T>& a, int n,
10312
cbc402e64d83 untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
1093 const octave_int<T>& mask = std::numeric_limits<T>::max ())
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1094 {
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1095 if (n > 0)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
1096 return (a << n) & mask;
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1097 else if (n < 0)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
1098 return (a >> -n) & mask;
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1099 else
8963
d1eab3ddb02d oct-inttypes.h (bitshift): apply mask even if not shifting
John W. Eaton <jwe@octave.org>
parents: 8950
diff changeset
1100 return a & mask;
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1101 }
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
1102
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1103 typedef octave_int<int8_t> octave_int8;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1104 typedef octave_int<int16_t> octave_int16;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1105 typedef octave_int<int32_t> octave_int32;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1106 typedef octave_int<int64_t> octave_int64;
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1107
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1108 typedef octave_int<uint8_t> octave_uint8;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1109 typedef octave_int<uint16_t> octave_uint16;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1110 typedef octave_int<uint32_t> octave_uint32;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1111 typedef octave_int<uint64_t> octave_uint64;
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1112
18005
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1113 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1114
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1115 #define DECLARE_EXTERNAL_LONG_DOUBLE_OP(T, OP) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1116 extern OCTAVE_API T \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1117 external_double_ ## T ## _ ## OP (double x, T y); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1118 extern OCTAVE_API T \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1119 external_ ## T ## _double_ ## OP (T x, double y)
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1120
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1121 #define DECLARE_EXTERNAL_LONG_DOUBLE_OPS(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1122 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, add); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1123 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, sub); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1124 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, mul); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1125 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, div)
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1126
18005
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1127 DECLARE_EXTERNAL_LONG_DOUBLE_OPS (octave_int64);
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1128 DECLARE_EXTERNAL_LONG_DOUBLE_OPS (octave_uint64);
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1129
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1130 #endif
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1131
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1132 #define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1133 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1134 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1135 operator OP (const octave_int<T>& x, const double& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1136 { return octave_int<T> (static_cast<double> (x) OP y); } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1137 template <class T> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1138 inline octave_int<T> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1139 operator OP (const double& x, const octave_int<T>& y) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1140 { return octave_int<T> (x OP static_cast<double> (y)); } \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1141
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1142 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1143 // Handle mixed op using long double
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1144 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1145 #define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1146 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1147 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1148 inline octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1149 operator OP (const double& x, const octave_int64& y) \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1150 { \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1151 return external_double_octave_int64_ ## NAME (x, y); \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1152 } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1153 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1154 inline octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1155 operator OP (const double& x, const octave_uint64& y) \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1156 { \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1157 return external_double_octave_uint64_ ## NAME (x, y); \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1158 } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1159 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1160 inline octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1161 operator OP (const octave_int64& x, const double& y) \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1162 { \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1163 return external_octave_int64_double_ ## NAME (x, y); \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1164 } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1165 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1166 inline octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1167 operator OP (const octave_uint64& x, const double& y) \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1168 { \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
1169 return external_octave_uint64_double_ ## NAME (x, y); \
17980
824c05a6d3ec avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17822
diff changeset
1170 }
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1171 #else
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1172 #define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1173 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1174 template <> \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1175 inline octave_int64 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1176 operator OP (const double& x, const octave_int64& y) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1177 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1178 return octave_int64 (x OP static_cast<long double> (y.value ())); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1179 } \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1180 template <> \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1181 inline octave_uint64 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1182 operator OP (const double& x, const octave_uint64& y) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1183 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1184 return octave_uint64 (x OP static_cast<long double> (y.value ())); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1185 } \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1186 template <> \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1187 inline octave_int64 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1188 operator OP (const octave_int64& x, const double& y) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1189 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1190 return octave_int64 (static_cast<long double> (x.value ()) OP y); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1191 } \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1192 template <> \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1193 inline octave_uint64 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1194 operator OP (const octave_uint64& x, const double& y) \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1195 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1196 return octave_uint64 (static_cast<long double> (x.value ()) OP y); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1197 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1198 #endif
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1199 #else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1200 // external handlers
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1201 #define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1202 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1203 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1204 OCTAVE_API octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1205 operator OP (const double&, const octave_int64&); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1206 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1207 OCTAVE_API octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1208 operator OP (const double&, const octave_uint64&); \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1209 template <> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1210 OCTAVE_API octave_int64 \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1211 operator OP (const octave_int64&, const double&); \
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
1212 template <> \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1213 OCTAVE_API octave_uint64 \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1214 operator OP (const octave_uint64&, const double&);
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1215
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1216 #endif
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1217
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1218 OCTAVE_INT_DOUBLE_BIN_OP (+, add)
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1219 OCTAVE_INT_DOUBLE_BIN_OP (-, sub)
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1220 OCTAVE_INT_DOUBLE_BIN_OP (*, mul)
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17980
diff changeset
1221 OCTAVE_INT_DOUBLE_BIN_OP (/, div)
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1222
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1223 #undef OCTAVE_INT_DOUBLE_BIN_OP0
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1224 #undef OCTAVE_INT_DOUBLE_BIN_OP
18005
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1225 #undef DECLARE_EXTERNAL_LONG_DOUBLE_OP
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
1226 #undef DECLARE_EXTERNAL_LONG_DOUBLE_OPS
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1227
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1228 #define OCTAVE_INT_DOUBLE_CMP_OP(OP,NAME) \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1229 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1230 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1231 operator OP (const octave_int<T>& x, const double& y) \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1232 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x.value (), y); } \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1233 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1234 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1235 operator OP (const double& x, const octave_int<T>& y) \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1236 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x, y.value ()); }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1237
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1238 OCTAVE_INT_DOUBLE_CMP_OP (<, lt)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1239 OCTAVE_INT_DOUBLE_CMP_OP (<=, le)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1240 OCTAVE_INT_DOUBLE_CMP_OP (>=, ge)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1241 OCTAVE_INT_DOUBLE_CMP_OP (>, gt)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1242 OCTAVE_INT_DOUBLE_CMP_OP (==, eq)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1243 OCTAVE_INT_DOUBLE_CMP_OP (!=, ne)
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1244
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1245 #undef OCTAVE_INT_DOUBLE_CMP_OP
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1246
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1247 // Floats are handled by simply converting to doubles.
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1248
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1249 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1250 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1251 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1252 operator OP (const octave_int<T>& x, float y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1253 { return x OP static_cast<double> (y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1254 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1255 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1256 operator OP (float x, const octave_int<T>& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1257 { return static_cast<double> (x) OP y; }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1258
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1259 OCTAVE_INT_FLOAT_BIN_OP (+)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1260 OCTAVE_INT_FLOAT_BIN_OP (-)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1261 OCTAVE_INT_FLOAT_BIN_OP (*)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1262 OCTAVE_INT_FLOAT_BIN_OP (/)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1263
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1264 #undef OCTAVE_INT_FLOAT_BIN_OP
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1265
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1266 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1267 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1268 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1269 operator OP (const octave_int<T>& x, const float& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1270 { return x OP static_cast<double> (y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1271 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1272 bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1273 operator OP (const float& x, const octave_int<T>& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1274 { return static_cast<double> (x) OP y; }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1275
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1276 OCTAVE_INT_FLOAT_CMP_OP (<)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1277 OCTAVE_INT_FLOAT_CMP_OP (<=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1278 OCTAVE_INT_FLOAT_CMP_OP (>=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1279 OCTAVE_INT_FLOAT_CMP_OP (>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1280 OCTAVE_INT_FLOAT_CMP_OP (==)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1281 OCTAVE_INT_FLOAT_CMP_OP (!=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1282
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1283 #undef OCTAVE_INT_FLOAT_CMP_OP
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1284
9743
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1285 template <class T>
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1286 octave_int<T>
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1287 xmax (const octave_int<T>& x, const octave_int<T>& y)
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1288 {
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1289 const T xv = x.value (), yv = y.value ();
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1290 return octave_int<T> (xv >= yv ? xv : yv);
9743
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1291 }
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1292
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1293 template <class T>
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1294 octave_int<T>
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1295 xmin (const octave_int<T>& x, const octave_int<T>& y)
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1296 {
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1297 const T xv = x.value (), yv = y.value ();
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1298 return octave_int<T> (xv <= yv ? xv : yv);
9743
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1299 }
26abff55f6fe optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents: 9648
diff changeset
1300
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1301 #endif