annotate liboctave/oct-inttypes.h @ 8293:ad5bb02d267a

workaround missing std::abs(int64_t) in MSVC
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 30 Oct 2008 20:12:28 +0100
parents cf59d542f33e
children c374691576f6
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
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
3 Copyright (C) 2004, 2005, 2006, 2007 John W. Eaton
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
4 Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com>
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
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
24 #if !defined (octave_inttypes_h)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
25 #define octave_inttypes_h 1
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
26
6482
7e958a1532c6 [project @ 2007-04-04 02:37:07 by jwe]
jwe
parents: 6402
diff changeset
27 #include <climits>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
28 #include <cstdlib>
6482
7e958a1532c6 [project @ 2007-04-04 02:37:07 by jwe]
jwe
parents: 6402
diff changeset
29
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
30 #include <limits>
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
31 #include <iostream>
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
32
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
33 #include "lo-math.h"
5828
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5533
diff changeset
34 #include "oct-types.h"
4953
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
35 #include "lo-ieee.h"
4969
52f808101130 [project @ 2004-09-08 02:44:04 by jwe]
jwe
parents: 4968
diff changeset
36 #include "lo-mappers.h"
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
37
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
38 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
39 inline long double xround (long double x) { return roundl (x); }
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
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
42 // FIXME: we define this by our own because some compilers, such as MSVC,
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
43 // do not provide std::abs (int64_t) and std::abs (uint64_t). In the future,
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
44 // it should go away in favor of std::abs.
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;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
54 typedef void type; // Void shall result in a compile-time error if we
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 // Selects one of two types, according to a static bool. May be useful in
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
78 // general.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
79
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
80 template<bool cond, class TT, class FT>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
81 class if_else_type
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 typedef FT type;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
85 };
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
86
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
87 template<class TT, class FT>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
88 class if_else_type<true, TT, FT>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
89 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
90 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
91 typedef TT type;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
92 };
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 // 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
95 // result of the binary relation. They also have two static boolean fields:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
96 // ltval, gtval determine the value of x OP y if x < y, x > y, respectively.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
97 #define REGISTER_OCTAVE_CMP_OP(NM,OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
98 class NM \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
99 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
100 public: \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
101 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
102 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
103 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
104 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
105
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
106 // 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
107 // yielding always false.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
108 #define REGISTER_OCTAVE_CONST_OP(NM,value) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
109 class NM \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
110 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
111 public: \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
112 static const bool ltval = value, gtval = value; \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
113 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
114 static bool op (T, T) { return value; } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
115 }
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
116
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
117 // Handles non-homogeneous integer comparisons. Avoids doing useless tests.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
118 class octave_int_cmp_op
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
119 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
120 // 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
121 // 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
122 // 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
123 // 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
124 // size.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
125 template <class T1, class T2>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
126 class prom
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
127 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
128 // Promote to int?
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
129 static const bool pint = (sizeof (T1) < sizeof (int)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
130 && sizeof (T2) < sizeof (int));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
131 static const bool t1sig = std::numeric_limits<T1>::is_signed;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
132 static const bool t2sig = std::numeric_limits<T2>::is_signed;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
133 static const bool psig =
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
134 (pint || (sizeof (T2) > sizeof (T1) && t2sig) || t1sig);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
135 static const int psize =
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
136 (pint ? sizeof (int) : (sizeof (T2) > sizeof (T1)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
137 ? sizeof (T2) : sizeof (T1)));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
138 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
139 typedef typename query_integer_type<psize, psig>::type type;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
140 };
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
141
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
142 // Implements comparisons between two types of equal size but
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
143 // possibly different signedness.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
144 template<class xop, int size>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
145 class uiop
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
146 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
147 typedef typename query_integer_type<size, false>::type utype;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
148 typedef typename query_integer_type<size, true>::type stype;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
149 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
150 static bool op (utype x, utype y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
151 { return xop::op (x, y); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
152 static bool op (stype x, stype y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
153 { return xop::op (x, y); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
154 static bool op (stype x, utype y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
155 { return (x < 0) ? xop::ltval : xop::op (static_cast<utype> (x), y); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
156 static bool op (utype x, stype y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
157 { return (y < 0) ? xop::gtval : xop::op (x, static_cast<utype> (y)); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
158 };
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
159
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
160 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
161 REGISTER_OCTAVE_CMP_OP (lt, <);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
162 REGISTER_OCTAVE_CMP_OP (le, <=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
163 REGISTER_OCTAVE_CMP_OP (gt, >);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
164 REGISTER_OCTAVE_CMP_OP (ge, >=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
165 REGISTER_OCTAVE_CMP_OP (eq, ==);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
166 REGISTER_OCTAVE_CMP_OP (ne, !=);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
167 REGISTER_OCTAVE_CONST_OP (ct, true);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
168 REGISTER_OCTAVE_CONST_OP (cf, false);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
170 // Universal comparison operation.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
171 template<class xop, class T1, class T2>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
172 static bool
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
173 op (T1 x, T2 y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
174 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
175 typedef typename prom<T1, T2>::type PT1;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
176 typedef typename prom<T2, T1>::type PT2;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
177 return uiop<xop, sizeof (PT1)>::op (static_cast<PT1> (x),
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
178 static_cast<PT2> (y));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
179 }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
180
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
181 public:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
182
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
183 // Mixed comparisons
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
184 template <class xop, class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
185 static bool
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
186 mop (T x, double y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
187 { return xop::op (static_cast<double> (x), y); }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
188
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
189 template <class xop, class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
190 static bool
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
191 mop (double x, T y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
192 { return xop::op (x, static_cast<double> (y)); }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
193
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
194 // Typecasting to doubles won't work properly for 64-bit integers - they lose precision.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
195 // If we have long doubles, use them...
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
196 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
197 #define DEFINE_LONG_DOUBLE_CMP_OP(T1, T2) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
198 template <class xop> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
199 static bool \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
200 mop (T1 x, T2 y) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
201 { \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
202 return xop::op (static_cast<long double> (x), \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
203 static_cast<long double> (y)); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
204 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
205 DEFINE_LONG_DOUBLE_CMP_OP(double, uint64_t)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
206 DEFINE_LONG_DOUBLE_CMP_OP(double, int64_t)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
207 DEFINE_LONG_DOUBLE_CMP_OP(int64_t, double)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
208 DEFINE_LONG_DOUBLE_CMP_OP(uint64_t, double)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
209 #undef DEFINE_LONG_DOUBLE_CMP_OP
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
210
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
211 #else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
212 // ... otherwise, use external handlers
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
213 template <class xop> static OCTAVE_API bool mop (uint64_t, double);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
214 template <class xop> static OCTAVE_API bool mop (int64_t, double);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
215 template <class xop> static OCTAVE_API bool mop (double, uint64_t);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
216 template <class xop> static OCTAVE_API bool mop (double, int64_t);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
217 #endif
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
218
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
219 };
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
220
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
221 // Base integer class. No data, just conversion methods and exception flags.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
222 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
223 class octave_int_base
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
224 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
225 protected:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
226
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
227 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
228 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
229
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
230 public:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
231
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
232 // Convert integer value.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
233 template <class S>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
234 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
235 truncate_int (const S& value)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
236 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
237 // An exhaustive test whether the max and/or min check can be omitted.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
238 static const bool t_is_signed = std::numeric_limits<T>::is_signed;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
239 static const bool s_is_signed = std::numeric_limits<S>::is_signed;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
240 static const int t_size = sizeof (T), s_size = sizeof (S);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
241 static const bool omit_chk_min =
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
242 (! s_is_signed || (t_is_signed && t_size >= s_size));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
243 static const bool omit_chk_max =
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
244 (t_size > s_size || (t_size == s_size
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
245 && (! t_is_signed || s_is_signed)));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
246 // If the check can be omitted, substitute constant false relation.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
247 typedef octave_int_cmp_op::cf cf;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
248 typedef octave_int_cmp_op::lt lt;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
249 typedef octave_int_cmp_op::gt gt;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
250 typedef typename if_else_type<omit_chk_min, cf, lt>::type chk_min;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
251 typedef typename if_else_type<omit_chk_max, cf, gt>::type chk_max;
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
252
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
253 // Efficiency of the following depends on inlining and dead code
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
254 // elimination, but that should be a piece of cake for most compilers.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
255 if (chk_min::op (value, static_cast<S> (min_val ())))
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
256 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
257 ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
258 return min_val ();
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 else if (chk_max::op (value, static_cast<S> (max_val ())))
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
261 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
262 ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
263 return max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
264 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
265 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
266 return static_cast<T> (value);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
267 }
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
268
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
269 private:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
270
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
271 // 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
272 template <class S>
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
273 static S
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
274 compute_threshold (S val, T orig_val)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
275 {
8193
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
276 val = xround (val); // Fool optimizations (maybe redundant)
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
277 // If val is even, but orig_val is odd, we're one unit off.
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
278 if (orig_val % 2 && val / 2 == xround (val / 2))
8202
cf59d542f33e replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents: 8193
diff changeset
279 // FIXME: is this always correct?
8193
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
280 val *= (static_cast<S>(1) - (std::numeric_limits<S>::epsilon () / 2));
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
281 return val;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
282 }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
283
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
284 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
285 // Convert a real number (check NaN and non-int).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
286 template <class S>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
287 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
288 convert_real (const S& value)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
289 {
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
290 // Compute proper thresholds.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
291 static const S thmin = compute_threshold (static_cast<S> (min_val ()), min_val ());
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
292 static const S thmax = compute_threshold (static_cast<S> (max_val ()), max_val ());
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
293 if (lo_ieee_isnan (value))
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
294 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
295 fnan = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
296 return static_cast<T> (0);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
297 }
8193
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
298 else if (value < thmin)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
299 {
8193
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
300 ftrunc = true;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
301 return min_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
302 }
8193
5fd507839b76 remove the int->real conversion code vulnerability to compiler optimization
Jaroslav Hajek <highegg@gmail.com>
parents: 8191
diff changeset
303 else if (value > thmax)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
304 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
305 ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
306 return max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
307 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
308 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
309 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
310 S rvalue = xround (value);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
311 if (rvalue != value) fnon_int = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
312 return static_cast<T> (rvalue);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
313 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
314 }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
315
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
316 // Exception flags rationale:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
317 // There is little reason to distinguish math and conversion exceptions at
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
318 // octave_int level. Doing this would require special constructors for
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
319 // intermediate int results in math computations.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
320 //
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
321 // Boolean flags are used rather than a single flag, because raising a boolean
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
322 // flag is faster than masking an int flag (single mov versus mov, or, mov).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
323 // Also, it is atomic, and thus thread-safe (but there is *one* flag for all
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
324 // threads).
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
325
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
326 static bool get_trunc_flag () { return ftrunc; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
327 static bool get_nan_flag () { return fnan; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
328 static bool get_non_int_flag () { return fnon_int; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
329 static void clear_conv_flags ()
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
330 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
331 ftrunc = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
332 fnan = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
333 fnon_int = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
334 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
335 // For compatibility.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
336 static bool get_math_trunc_flag () { return ftrunc || fnan; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
337 static void clear_conv_flag () { clear_conv_flags (); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
338
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
339 protected:
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
340
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
341 // Conversion flags.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
342 static bool ftrunc;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
343 static bool fnon_int;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
344 static bool fnan;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
345 };
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
346
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
347 template<class T> bool octave_int_base<T>::ftrunc = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
348 template<class T> bool octave_int_base<T>::fnon_int = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
349 template<class T> bool octave_int_base<T>::fnan = false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
350
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
351 // Saturated (homogeneous) integer arithmetics. The signed and unsigned
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
352 // implementations are significantly different, so we implement another layer
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
353 // 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
354 // 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
355
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
356 template <class T, bool is_signed>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
357 class octave_int_arith_base
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
358 { };
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
359
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
360 // 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
361 // 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
362 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
363 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
364 {
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
365 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
366
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 abs (T x) { return x; }
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 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
372
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
373 // Shifts do not overflow.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
374 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
375 rshift (T x, int n) { return x >> n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
376
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
377 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
378 lshift (T x, int n) { return x << n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
379
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
380 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
381 minus (T x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
382 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
383 if (x != 0) octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
384 return static_cast<T> (0);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
385 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
386
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
387 // 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
388 // so the following should always work.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
389 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
390 add (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
391 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
392 T u = x + y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
393 if (u < x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
394 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
395 u = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
396 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
397 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
398 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
399 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
400
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
401 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
402 sub (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
403 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
404 T u = x - y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
405 if (u > x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
406 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
407 u = 0;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
408 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
409 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
410 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
411 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
412
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
413 // Multiplication is done using promotion to wider integer type. If there is
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
414 // no suitable promotion type, this operation *MUST* be specialized.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
415 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
416 mul (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
417 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
418 // Promotion type for multiplication (if exists).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
419 typedef typename query_integer_type<2*sizeof (T), false>::type mptype;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
420 return truncate_int (static_cast<mptype> (x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
421 * static_cast<mptype> (y));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
422 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
423
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
424 // 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
425 // computed by a single instruction.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
426 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
427 div (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
428 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
429 if (y != 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
430 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
431 T z = x / y, w = x % y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
432 if (w >= y-w) z += 1;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
433 return z;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
434 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
435 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
436 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
437 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
438 return x ? octave_int_base<T>::max_val () : 0;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
439 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
440 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
441 };
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
442
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
443 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
444 // Handle 64-bit multiply using long double
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
445 template <>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
446 inline uint64_t
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
447 octave_int_arith_base<uint64_t, false>:: mul (uint64_t x, uint64_t y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
448 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
449 long double p = static_cast<long double> (x) * static_cast<long double> (y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
450 if (p > static_cast<long double> (octave_int_base<uint64_t>::max_val ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
451 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
452 octave_int_base<uint64_t>::ftrunc = true;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
453 return octave_int_base<uint64_t>::max_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
454 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
455 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
456 return static_cast<uint64_t> (p);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
457 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
458 #else
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
459 // Special handler for 64-bit integer multiply.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
460 template <>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
461 OCTAVE_API uint64_t
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
462 octave_int_arith_base<uint64_t, false>::mul (uint64_t, uint64_t);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
463 #endif
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
464
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
465 // Signed integer arithmetics.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
466 // 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
467 // should hold:
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
468 // 1. Signed numbers are represented by twos complement
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
469 // (see <http://en.wikipedia.org/wiki/Two%27s_complement>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
470 // 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
471 // 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
472 // 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
473 // (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
474 // need for things like -ftrapv).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
475 // 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
476 // except for the shifts. Shifts are arithmetic.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
477 //
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
478 // 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
479 // HAVE_FAST_INT_OPS is defined, bit tricks and wraparound arithmetics are used
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
480 // to avoid conditional jumps as much as possible, thus being friendly to modern
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
481 // pipeline processor architectures.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
482 // Otherwise, we fall back to a bullet-proof code that only uses assumptions
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
483 // guaranteed by the standard.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
484
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
485 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
486 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
487 {
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
488 // The corresponding unsigned type.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
489 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
490 public:
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
491
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
492 // Returns 1 for negative number, 0 otherwise.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
493 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
494 signbit (T x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
495 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
496 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
497 return static_cast<UT> (x) >> std::numeric_limits<T>::digits;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
498 #else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
499 return (x < 0) ? 1 : 0;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
500 #endif
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
501 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
502
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
503 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
504 abs (T x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
505 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
506 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
507 // This is close to how GCC does std::abs, but we can't just use std::abs,
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
508 // because its behaviour for INT_MIN is undefined and the compiler could
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
509 // discard the following test.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
510 T m = x >> std::numeric_limits<T>::digits;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
511 T y = (x ^ m) - m;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
512 if (y < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
513 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
514 y = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
515 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
516 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
517 return y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
518 #else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
519 // -INT_MAX is safe because C++ actually allows only three implementations
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
520 // of integers: sign & magnitude, ones complement and twos complement.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
521 // The first test will, with modest optimizations, evaluate at compile
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
522 // time, and maybe eliminate the branch completely.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
523 T y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
524 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val ()
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
525 && x == octave_int_base<T>::min_val ())
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
526 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
527 y = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
528 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
529 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
530 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
531 y = (x < 0) ? -x : x;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
532 return y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
533 #endif
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
534 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
535
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
536 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
537 signum (T x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
538 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
539 // With modest optimizations, this will compile without a jump.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
540 return ((x > 0) ? 1 : 0) - signbit (x);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
541 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
542
8202
cf59d542f33e replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents: 8193
diff changeset
543 // FIXME: We do not have an authority what signed shifts should exactly do, so
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
544 // we define them the easy way. Note that Matlab does not define signed
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
545 // shifts.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
546
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
547 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
548 rshift (T x, int n) { return x >> n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
549
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
550 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
551 lshift (T x, int n) { return x << n; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
552
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
553 // Minus has problems similar to abs.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
554 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
555 minus (T x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
556 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
557 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
558 T y = -x;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
559 if (y == octave_int_base<T>::min_val ())
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
560 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
561 --y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
562 octave_int_base<T>::ftrunc = false;
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 return y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
565 #else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
566 T y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
567 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val ()
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
568 && x == octave_int_base<T>::min_val ())
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
569 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
570 y = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
571 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
572 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
573 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
574 y = -x;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
575 return y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
576 #endif
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
577 }
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
578
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
579 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
580 add (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
581 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
582 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
583 // 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
584 // 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
585 // actually return int.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
586 T u = static_cast<UT> (x) + static_cast<UT> (y);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
587 T ux = u ^ x, uy = u ^ y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
588 if ((ux & uy) < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
589 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
590 u = octave_int_base<T>::max_val () + signbit (~u);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
591 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
592 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
593 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
594 #else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
595 // We shall carefully avoid anything that may overflow.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
596 T u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
597 if (y < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
598 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
599 if (x < octave_int_base<T>::min_val () - y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
600 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
601 u = octave_int_base<T>::min_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
602 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
603 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
604 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
605 u = x + y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
606 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
607 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
608 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
609 if (x > octave_int_base<T>::max_val () - y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
610 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
611 u = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
612 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
613 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
614 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
615 u = x + y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
616 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
617
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
618 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
619 #endif
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
620 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
621
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
622 // This is very similar to addition.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
623 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
624 sub (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
625 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
626 #ifdef HAVE_FAST_INT_OPS
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
627 // 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
628 // 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
629 // actually return int.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
630 T u = static_cast<UT> (x) - static_cast<UT> (y);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
631 T ux = u ^ x, uy = u ^ ~y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
632 if ((ux & uy) < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
633 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
634 u = octave_int_base<T>::max_val () + signbit (~u);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
635 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
636 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
637 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
638 #else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
639 // We shall carefully avoid anything that may overflow.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
640 T u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
641 if (y < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
642 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
643 if (x > octave_int_base<T>::max_val () + y)
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 u = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
646 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
647 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
648 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
649 u = x - y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
650 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
651 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
652 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
653 if (x < octave_int_base<T>::min_val () + y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
654 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
655 u = octave_int_base<T>::min_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
656 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
657 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
658 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
659 u = x - y;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
660 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
661
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
662 return u;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
663 #endif
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
664 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
665
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
666 // Multiplication is done using promotion to wider integer type. If there is
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
667 // no suitable promotion type, this operation *MUST* be specialized.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
668 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
669 mul (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
670 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
671 // Promotion type for multiplication (if exists).
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
672 typedef typename query_integer_type<2*sizeof (T), true>::type mptype;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
673 return truncate_int (static_cast<mptype> (x)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
674 * static_cast<mptype> (y));
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
675 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
676
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
677 // Division.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
678 static T
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
679 div (T x, T y)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
680 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
681 T z;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
682 if (y == 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
683 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
684 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
685 if (x < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
686 z = octave_int_base<T>::min_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
687 else if (x != 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
688 z = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
689 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
690 z = 0;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
691 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
692 else if (y < 0)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
693 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
694 // This is a special case that overflows as well.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
695 if (y == -1 && x == octave_int_base<T>::min_val ())
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
696 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
697 octave_int_base<T>::ftrunc = true;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
698 z = octave_int_base<T>::max_val ();
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
699 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
700 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
701 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
702 z = x / y;
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
703 T w = -octave_int_abs (x % y); // Can't overflow, but std::abs (x) can!
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
704 if (w <= y - w)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
705 z -= 1 - (signbit (x) << 1);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
706 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
707 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
708 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
709 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
710 z = x / y;
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
711 // FIXME: this is a workaround due to MSVC's absence of std::abs (int64_t).
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
712 T w = octave_int_abs (x % y); // Can't overflow, but std::abs (x) can!
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
713 if (w >= y - w)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
714 z += 1 - (signbit (x) << 1);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
715 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
716 return z;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
717 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
718
7521
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
719 };
6f10bbb2854a avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents: 7503
diff changeset
720
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
721 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
722 // Handle 64-bit multiply using long double
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
723 template <>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
724 inline int64_t
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
725 octave_int_arith_base<int64_t, true>:: mul (int64_t x, int64_t y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
726 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
727 long double p = static_cast<long double> (x) * static_cast<long double> (y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
728 // NOTE: We could maybe do it with a single branch if HAVE_FAST_INT_OPS, but it
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
729 // would require one more runtime conversion, so the question is whether it would
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
730 // really be faster.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
731 if (p > static_cast<long double> (octave_int_base<int64_t>::max_val ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
732 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
733 octave_int_base<int64_t>::ftrunc = true;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
734 return octave_int_base<int64_t>::max_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
735 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
736 else if (p < static_cast<long double> (octave_int_base<int64_t>::min_val ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
737 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
738 octave_int_base<int64_t>::ftrunc = true;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
739 return octave_int_base<int64_t>::min_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
740 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
741 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
742 return static_cast<int64_t> (p);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
743 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
744 #else
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
745 // Special handler for 64-bit integer multiply.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
746 template <>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
747 OCTAVE_API int64_t
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
748 octave_int_arith_base<int64_t, true>::mul (int64_t, int64_t);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
749 #endif
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
750
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
751 // This class simply selects the proper arithmetics.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
752 template<class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
753 class octave_int_arith
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
754 : public octave_int_arith_base<T, std::numeric_limits<T>::is_signed>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
755 {};
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
756
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
757 template <class T>
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
758 class
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
759 octave_int : public octave_int_base<T>
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
760 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
761 public:
4943
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
762 typedef T val_type;
1a499d0c58f5 [project @ 2004-08-31 00:51:31 by jwe]
jwe
parents: 4920
diff changeset
763
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
764 octave_int (void) : ival () { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
765
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
766 octave_int (T i) : ival (i) { }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
767
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
768 octave_int (double d) : ival (octave_int_base<T>::convert_real (d)) { }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
769
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
770 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
771
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
772 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
773 octave_int (long double d) : ival (octave_int_base<T>::convert_real (d)) { }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
774 #endif
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
775
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
776 octave_int (bool b) : ival (b) { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
777
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
778 template <class U>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
779 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
780
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
781 template <class U>
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
782 octave_int (const octave_int<U>& i)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
783 : ival (octave_int_base<T>::truncate_int (i.value ())) { }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
784
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
785 octave_int (const octave_int<T>& i) : ival (i.ival) { }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
786
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
787 octave_int& operator = (const octave_int<T>& i)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
788 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
789 ival = i.ival;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
790 return *this;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
791 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
792
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
793 T value (void) const { return ival; }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
794
4949
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
795 const unsigned char * iptr (void) const
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
796 { return reinterpret_cast<const unsigned char *> (& ival); }
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
797
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
798 bool operator ! (void) const { return ! ival; }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
799
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
800 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
801
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
802 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
803
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
804 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
805
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
806 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
807
7177
57d2546ad8d5 [project @ 2007-11-14 22:21:35 by jwe]
jwe
parents: 7017
diff changeset
808 operator T (void) const { return value (); }
57d2546ad8d5 [project @ 2007-11-14 22:21:35 by jwe]
jwe
parents: 7017
diff changeset
809
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
810 // char and bool operators intentionally omitted.
5533
667ad2becb63 [project @ 2005-11-10 21:40:48 by jwe]
jwe
parents: 5420
diff changeset
811
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
812 operator double (void) const { return double_value (); }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
813
7198
4555c116b420 [project @ 2007-11-27 03:06:10 by jwe]
jwe
parents: 7177
diff changeset
814 operator float (void) const { return float_value (); }
5030
160365410ad4 [project @ 2004-09-24 03:22:23 by jwe]
jwe
parents: 5029
diff changeset
815
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
816 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
817 operator + () const
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
818 { return *this; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
819
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
820 // unary operators & mappers
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
821 #define OCTAVE_INT_UN_OP(OPNAME,NAME) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
822 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
823 OPNAME () const \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
824 { return octave_int_arith<T>::NAME (ival); }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
825
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
826 OCTAVE_INT_UN_OP(operator -, minus)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
827 OCTAVE_INT_UN_OP(abs, abs)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
828 OCTAVE_INT_UN_OP(signum, signum)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
829
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
830 #undef OCTAVE_INT_UN_OP
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
831
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
832 // Homogeneous binary integer operations.
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
833 #define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
834 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
835 operator OP (const ARGT& y) const \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
836 { return octave_int_arith<T>::NAME (ival, y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
837 inline octave_int<T>& \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
838 operator OP##= (const ARGT& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
839 { \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
840 ival = octave_int_arith<T>::NAME (ival, y); \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
841 return *this; \
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
842 }
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
843
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
844 OCTAVE_INT_BIN_OP(+, add, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
845 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
846 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
847 OCTAVE_INT_BIN_OP(/, div, octave_int<T>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
848 OCTAVE_INT_BIN_OP(<<, lshift, int)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
849 OCTAVE_INT_BIN_OP(>>, rshift, int)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
850
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
851 #undef OCTAVE_INT_BIN_OP
7503
8c32f95c2639 convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents: 7198
diff changeset
852
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
853 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
854 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
855
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
856 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
857
4949
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
858 static int byte_size (void) { return sizeof(T); }
f6b63ff1119b [project @ 2004-08-31 19:17:44 by jwe]
jwe
parents: 4943
diff changeset
859
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
860 static const char *type_name ();
7997
2b8952e133c9 implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents: 7922
diff changeset
861
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
862 // The following are provided for convenience.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
863 static const octave_int zero, one;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
864
5900
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
865 // Unsafe. This function exists to support the MEX interface.
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
866 // You should not use it anywhere else.
c20eb7330d13 [project @ 2006-07-22 08:31:16 by jwe]
jwe
parents: 5898
diff changeset
867 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
868
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
869 private:
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
870
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
871 T ival;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
872 };
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
873
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
874 // No mixed integer binary operations!
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
875
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
876 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
877 inline bool
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
878 xisnan (const octave_int<T>&)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
879 { return false; }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
880
8202
cf59d542f33e replace all TODOs and XXXs with FIXMEs
Jaroslav Hajek <highegg@gmail.com>
parents: 8193
diff changeset
881 // FIXME: Can/should any of these be inline?
7997
2b8952e133c9 implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents: 7922
diff changeset
882
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
883 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
884 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
885 pow (const octave_int<T>&, const octave_int<T>&);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
886
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
887 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
888 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
889 pow (const double& a, const octave_int<T>& b);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
890
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
891 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
892 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
893 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
894
935be827eaf8 error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents: 7789
diff changeset
895 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
896 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
897 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
898
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
899 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
900 extern OCTAVE_API octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
901 powf (const octave_int<T>& a, const float& b);
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
902
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
903 // Binary relations
4953
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
904
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
905 #define OCTAVE_INT_CMP_OP(OP, NAME) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
906 template<class T1, class T2> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
907 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
908 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
909 { 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
910 (x.value (), y.value ()); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
911
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
912 OCTAVE_INT_CMP_OP (<, lt)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
913 OCTAVE_INT_CMP_OP (<=, le)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
914 OCTAVE_INT_CMP_OP (>, gt)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
915 OCTAVE_INT_CMP_OP (>=, ge)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
916 OCTAVE_INT_CMP_OP (==, eq)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
917 OCTAVE_INT_CMP_OP (!=, ne)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
918
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
919 #undef OCTAVE_INT_CMP_OP
4953
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
920
7a3a480e8645 [project @ 2004-09-01 21:10:28 by jwe]
jwe
parents: 4952
diff changeset
921 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
922 inline std::ostream&
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
923 operator << (std::ostream& os, const octave_int<T>& ival)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
924 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
925 os << ival.value ();
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
926 return os;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
927 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
928
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
929 template <class T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
930 inline std::istream&
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
931 operator >> (std::istream& is, octave_int<T>& ival)
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
932 {
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
933 T tmp = 0;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
934 is >> tmp;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
935 ival = tmp;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
936 return is;
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
937 }
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
938
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
939 // Bitwise operations
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7598
diff changeset
940
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
941 #define OCTAVE_INT_BITCMP_OP(OP) \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
942 template <class T> \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
943 octave_int<T> \
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
944 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
945 { return x.value () OP y.value (); }
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
946
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
947 OCTAVE_INT_BITCMP_OP (&)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
948 OCTAVE_INT_BITCMP_OP (|)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
949 OCTAVE_INT_BITCMP_OP (^)
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
950
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
951 #undef OCTAVE_INT_BITCMP_OP
4906
6754342ef7ad [project @ 2004-06-23 11:11:50 by jwe]
jwe
parents: 4902
diff changeset
952
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
953 // General bit shift.
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
954 template <class T>
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
955 octave_int<T>
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4919
diff changeset
956 bitshift (const octave_int<T>& a, int n,
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4919
diff changeset
957 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
958 {
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
959 if (n > 0)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
960 return (a << n) & mask;
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
961 else if (n < 0)
4952
bfd57b466752 [project @ 2004-09-01 00:49:05 by jwe]
jwe
parents: 4949
diff changeset
962 return (a >> -n) & mask;
4909
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
963 else
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
964 return a;
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
965 }
5c9e36402f17 [project @ 2004-07-12 17:08:24 by jwe]
jwe
parents: 4906
diff changeset
966
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
967 typedef octave_int<int8_t> octave_int8;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
968 typedef octave_int<int16_t> octave_int16;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
969 typedef octave_int<int32_t> octave_int32;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
970 typedef octave_int<int64_t> octave_int64;
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
971
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
972 typedef octave_int<uint8_t> octave_uint8;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
973 typedef octave_int<uint16_t> octave_uint16;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
974 typedef octave_int<uint32_t> octave_uint32;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
975 typedef octave_int<uint64_t> octave_uint64;
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
976
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
977 #define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
978 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
979 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
980 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
981 { 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
982 template <class T> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
983 inline octave_int<T> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
984 operator OP (const double& x, const octave_int<T>& y) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
985 { return octave_int<T> (x OP static_cast<double> (y)); } \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
986
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
987 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
988 // Handle mixed op using long double
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
989 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
990 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
991 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
992 inline octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
993 operator OP (const double& x, const octave_int64& y) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
994 { return octave_int64 (x OP static_cast<long double> (y.value ())); } \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
995 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
996 inline octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
997 operator OP (const double& x, const octave_uint64& y) \
8191
9cb73236e552 fix uint64 mixed operations
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
998 { return octave_uint64 (x OP static_cast<long double> (y.value ())); } \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
999 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1000 inline octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1001 operator OP (const octave_int64& x, const double& y) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1002 { return octave_int64 (static_cast<long double> (x.value ()) OP y); } \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1003 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1004 inline octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1005 operator OP (const octave_uint64& x, const double& y) \
8191
9cb73236e552 fix uint64 mixed operations
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
1006 { return octave_uint64 (static_cast<long double> (x.value ()) OP y); }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1007
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1008 #else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1009 // external handlers
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1010 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1011 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1012 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1013 OCTAVE_API octave_int64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1014 operator OP (const double&, const octave_int64&); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1015 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1016 OCTAVE_API octave_uint64 \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1017 operator OP (const double&, const octave_uint64&); \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1018 template <> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1019 OCTAVE_API octave_int64 \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1020 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
1021 template <> \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1022 OCTAVE_API octave_uint64 \
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1023 operator OP (const octave_uint64&, const double&);
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1024
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1025 #endif
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1026
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1027 OCTAVE_INT_DOUBLE_BIN_OP (+)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1028 OCTAVE_INT_DOUBLE_BIN_OP (-)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1029 OCTAVE_INT_DOUBLE_BIN_OP (*)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1030 OCTAVE_INT_DOUBLE_BIN_OP (/)
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1031
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1032 #undef OCTAVE_INT_DOUBLE_BIN_OP0
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1033 #undef OCTAVE_INT_DOUBLE_BIN_OP
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1034
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1035 #define OCTAVE_INT_DOUBLE_CMP_OP(OP,NAME) \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1036 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1037 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1038 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
1039 { 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
1040 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1041 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1042 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
1043 { 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
1044
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1045 OCTAVE_INT_DOUBLE_CMP_OP (<, lt)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1046 OCTAVE_INT_DOUBLE_CMP_OP (<=, le)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1047 OCTAVE_INT_DOUBLE_CMP_OP (>=, ge)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1048 OCTAVE_INT_DOUBLE_CMP_OP (>, gt)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1049 OCTAVE_INT_DOUBLE_CMP_OP (==, eq)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1050 OCTAVE_INT_DOUBLE_CMP_OP (!=, ne)
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1051
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1052 #undef OCTAVE_INT_DOUBLE_CMP_OP
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1053
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1054 // Floats are handled by simply converting to doubles.
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1055
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1056 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1057 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1058 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1059 operator OP (const octave_int<T>& x, float y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1060 { return x OP static_cast<double> (y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1061 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1062 inline octave_int<T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1063 operator OP (float x, const octave_int<T>& y) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1064 { return static_cast<double> (x) OP y; }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1065
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1066 OCTAVE_INT_FLOAT_BIN_OP (+)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1067 OCTAVE_INT_FLOAT_BIN_OP (-)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1068 OCTAVE_INT_FLOAT_BIN_OP (*)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1069 OCTAVE_INT_FLOAT_BIN_OP (/)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1070
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1071 #undef OCTAVE_INT_FLOAT_BIN_OP
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents: 5030
diff changeset
1072
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1073 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1074 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1075 inline bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1076 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
1077 { return x OP static_cast<double> (y); } \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1078 template <class T> \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1079 bool \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1080 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
1081 { return static_cast<double> (x) OP y; }
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1082
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1083 OCTAVE_INT_FLOAT_CMP_OP (<)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1084 OCTAVE_INT_FLOAT_CMP_OP (<=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1085 OCTAVE_INT_FLOAT_CMP_OP (>=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1086 OCTAVE_INT_FLOAT_CMP_OP (>)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1087 OCTAVE_INT_FLOAT_CMP_OP (==)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1088 OCTAVE_INT_FLOAT_CMP_OP (!=)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 8104
diff changeset
1089
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1090 #undef OCTAVE_INT_FLOAT_CMP_OP
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
1091
4902
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1092 #endif
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1093
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1094 /*
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1095 ;;; Local Variables: ***
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1096 ;;; mode: C++ ***
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1097 ;;; End: ***
bd043a433918 [project @ 2004-06-14 18:46:20 by jwe]
jwe
parents:
diff changeset
1098 */