annotate liboctave/util/oct-inttypes.h @ 25858:1f52a96c8c21 stable

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