Mercurial > octave
annotate liboctave/util/oct-inttypes.h @ 22197:e43d83253e28
refill multi-line macro definitions
Use the Emacs C++ mode style for line continuation markers in
multi-line macro definitions.
* make_int.cc, __dsearchn__.cc, __magick_read__.cc, besselj.cc,
bitfcns.cc, bsxfun.cc, cellfun.cc, data.cc, defun-dld.h, defun-int.h,
defun.h, det.cc, error.h, find.cc, gcd.cc, graphics.cc, interpreter.h,
jit-ir.h, jit-typeinfo.h, lookup.cc, ls-mat5.cc, max.cc, mexproto.h,
mxarray.in.h, oct-stream.cc, ordschur.cc, pr-output.cc, profiler.h,
psi.cc, regexp.cc, sparse-xdiv.cc, sparse-xpow.cc, tril.cc, txt-eng.h,
utils.cc, variables.cc, variables.h, xdiv.cc, xpow.cc, __glpk__.cc,
ov-base.cc, ov-base.h, ov-cell.cc, ov-ch-mat.cc, ov-classdef.cc,
ov-complex.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-float.cc, ov-float.h,
ov-flt-complex.cc, ov-flt-cx-mat.cc, ov-flt-re-mat.cc,
ov-int-traits.h, ov-lazy-idx.h, ov-perm.cc, ov-re-mat.cc,
ov-re-sparse.cc, ov-scalar.cc, ov-scalar.h, ov-str-mat.cc,
ov-type-conv.h, ov.cc, ov.h, op-class.cc, op-int-conv.cc, op-int.h,
op-str-str.cc, ops.h, lex.ll, Array.cc, CMatrix.cc, CSparse.cc,
MArray.cc, MArray.h, MDiagArray2.cc, MDiagArray2.h, MSparse.h,
Sparse.cc, dMatrix.cc, dSparse.cc, fCMatrix.cc, fMatrix.cc,
idx-vector.cc, f77-fcn.h, quit.h, bsxfun-decl.h, bsxfun-defs.cc,
lo-specfun.cc, oct-convn.cc, oct-convn.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, Sparse-op-decls.h, Sparse-op-defs.h, mx-inlines.cc,
mx-op-decl.h, mx-op-defs.h, mach-info.cc, oct-group.cc, oct-passwd.cc,
oct-syscalls.cc, oct-time.cc, data-conv.cc, kpse.cc, lo-ieee.h,
lo-macros.h, oct-cmplx.h, oct-glob.cc, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.h, oct-sparse.h, url-transfer.cc,
oct-conf-post.in.h, shared-fcns.h: Refill macro definitions.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 01 Aug 2016 12:40:18 -0400 |
parents | 35c0ad43c7a5 |
children | bac0d6f07a3e |
rev | line source |
---|---|
4902 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18634
diff
changeset
|
3 Copyright (C) 2004-2015 John W. Eaton |
11523 | 4 Copyright (C) 2008-2009 Jaroslav Hajek |
4902 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4902 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4902 | 21 |
22 */ | |
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 | 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 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
29 #include <cstdlib> |
6482 | 30 |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
31 #include <iosfwd> |
4902 | 32 #include <limits> |
33 | |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
34 #include "lo-math.h" |
4969 | 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 | 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 { |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
56 inline long double round (long double x) { return roundl (x); } |
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 |
22107
35c0ad43c7a5
doc: Use correct function name in deprecation message.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
22073
diff
changeset
|
64 OCTAVE_DEPRECATED ("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 |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
67 OCTAVE_DEPRECATED ("use 'octave::math::isnan' instead") |
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 | 75 // MSVC, do not provide std::abs (int64_t) and std::abs (uint64_t). In |
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> |
8293
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
78 inline T octave_int_abs (T x) { return x >= 0 ? x : -x; } |
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
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 | 83 { |
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 | 88 }; |
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 | 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 | 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 | 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 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
295 static T min_val () { return std::numeric_limits<T>:: min (); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
296 static T max_val () { return std::numeric_limits<T>:: max (); } |
4943 | 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 | 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 | 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 |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
510 // Signed integer arithmetic. |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
511 // |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
512 // Rationale: If OCTAVE_HAVE_FAST_INT_OPS is defined, the following |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
513 // conditions should hold: |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
514 // |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
515 // 1. Signed numbers are represented by twos complement (see |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
516 // <http://en.wikipedia.org/wiki/Two%27s_complement>) |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
517 // |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
518 // 2. static_cast to unsigned int counterpart works like |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
519 // interpreting the signed bit pattern as unsigned (and is thus |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
520 // zero-cost). |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
521 // |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
522 // 3. Signed addition and subtraction yield the same bit results as |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
523 // unsigned. (We use casts to prevent optimization interference, |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
524 // so there is no need for things like -ftrapv). |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
525 // |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
526 // 4. Bit operations on signed integers work like on unsigned |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
527 // integers, except for the shifts. Shifts are arithmetic. |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
528 // |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
529 // The above conditions are satisfied by most modern platforms. If |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
530 // OCTAVE_HAVE_FAST_INT_OPS is defined, bit tricks and wraparound |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
531 // arithmetics are used to avoid conditional jumps as much as |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
532 // possible, thus being friendly to modern pipeline processor |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
533 // architectures. Otherwise, we fall back to a bullet-proof code that |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
534 // only uses assumptions guaranteed by the standard. |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
535 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
536 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
537 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
|
538 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
539 // The corresponding unsigned type. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
540 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
|
541 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
542 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
543 // Returns 1 for negative number, 0 otherwise. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
544 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
|
545 __signbit (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
546 { |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
547 #if defined (OCTAVE_HAVE_FAST_INT_OPS) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
548 return static_cast<UT> (x) >> std::numeric_limits<T>::digits; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
549 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 return (x < 0) ? 1 : 0; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
551 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
552 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
553 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
554 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
555 abs (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
556 { |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
557 #if defined (OCTAVE_HAVE_FAST_INT_OPS) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
558 // This is close to how GCC does std::abs, but we can't just use std::abs, |
21308
c53bfd6d8e08
maint: Use American spelling for "behavior".
Rik <rik@octave.org>
parents:
21302
diff
changeset
|
559 // because its behavior for INT_MIN is undefined and the compiler could |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
560 // discard the following test. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
561 T m = x >> std::numeric_limits<T>::digits; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
562 T y = (x ^ m) - m; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
563 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
564 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
565 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
566 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
567 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
568 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
569 // -INT_MAX is safe because C++ actually allows only three implementations |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
570 // of integers: sign & magnitude, ones complement and twos complement. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
571 // The first test will, with modest optimizations, evaluate at compile |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
572 // time, and maybe eliminate the branch completely. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
573 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
574 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val () |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
575 && 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
|
576 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
577 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
578 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
579 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
580 y = (x < 0) ? -x : x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
581 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
582 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
583 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
584 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
585 static T |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
586 signum (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
587 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
588 // 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
|
589 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
|
590 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
591 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
592 // 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
|
593 // exactly do, so we define them the easy way. Note that Matlab does |
8333 | 594 // not define signed shifts. |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
595 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
596 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
597 rshift (T x, int n) { return x >> n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
598 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
599 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
600 lshift (T x, int n) { return x << n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
601 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
602 // Minus has problems similar to abs. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
603 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
604 minus (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
605 { |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
606 #if defined (OCTAVE_HAVE_FAST_INT_OPS) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
607 T y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
608 if (y == octave_int_base<T>::min_val ()) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
609 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
610 --y; |
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 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
613 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
614 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
615 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val () |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
616 && 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
|
617 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
618 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
619 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
620 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
621 y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
622 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
623 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
624 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
625 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
626 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
627 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
|
628 { |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
629 #if defined (OCTAVE_HAVE_FAST_INT_OPS) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
630 // The typecasts do nothing, but they are here to prevent an optimizing |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
631 // compiler from interfering. Also, the signed operations on small types |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
632 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
633 T u = static_cast<UT> (x) + static_cast<UT> (y); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
634 T ux = u ^ x; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
635 T uy = u ^ y; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
636 if ((ux & uy) < 0) |
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 u = octave_int_base<T>::max_val () + __signbit (~u); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
639 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
640 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
641 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
642 // We shall carefully avoid anything that may overflow. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
643 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
644 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
645 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
646 if (x < octave_int_base<T>::min_val () - y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
647 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
648 u = octave_int_base<T>::min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
649 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
650 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
651 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
652 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
653 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
654 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
655 if (x > octave_int_base<T>::max_val () - y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
656 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
657 u = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
658 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
659 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
660 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
661 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
662 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
663 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
664 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
665 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
666 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
667 // This is very similar to addition. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
668 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
669 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
|
670 { |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
671 #if defined (OCTAVE_HAVE_FAST_INT_OPS) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
672 // The typecasts do nothing, but they are here to prevent an optimizing |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
673 // compiler from interfering. Also, the signed operations on small types |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
674 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
675 T u = static_cast<UT> (x) - static_cast<UT> (y); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
676 T ux = u ^ x; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
677 T uy = u ^ ~y; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
678 if ((ux & uy) < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
679 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
680 u = octave_int_base<T>::max_val () + __signbit (~u); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
681 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
682 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
683 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
684 // We shall carefully avoid anything that may overflow. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
685 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
686 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
687 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
688 if (x > octave_int_base<T>::max_val () + y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
689 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
690 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
|
691 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
692 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
693 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
694 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
695 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
696 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
697 if (x < octave_int_base<T>::min_val () + y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
698 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
699 u = octave_int_base<T>::min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
700 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
701 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
702 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
703 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
704 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
705 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
706 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
707 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
708 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
709 // 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
|
710 // 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
|
711 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
|
712 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
713 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
|
714 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
|
715 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
716 // 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
|
717 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
|
718 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
|
719 * static_cast<mptype> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
720 } |
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 // Division. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
723 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
724 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
|
725 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
726 T z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
727 if (y == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
728 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
729 if (x < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
730 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
|
731 else if (x != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
732 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
|
733 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
734 z = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
735 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
736 else if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
737 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
738 // 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
|
739 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
|
740 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
741 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
|
742 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
743 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
744 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
745 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
746 // 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
|
747 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
|
748 if (w <= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
749 z -= 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
750 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
751 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
752 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
753 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
754 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
755 // 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
|
756 // 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
|
757 // 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
|
758 T w = octave_int_abs (x % y); |
8333 | 759 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
760 if (w >= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
761 z += 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
762 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
763 return z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
764 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
765 |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
766 // Remainder. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
767 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
768 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
|
769 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
770 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
|
771 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
772 |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
773 // 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
|
774 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
775 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
|
776 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
777 if (y != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
778 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
779 T r = x % y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
780 return ((r < 0) != (y < 0)) ? r + y : r; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
781 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
782 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
783 return x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
784 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
785 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
786 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
787 #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
|
788 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
789 // 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
|
790 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
791 #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
|
792 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
793 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
|
794 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
|
795 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
796 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
797 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
798 template <> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
799 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
|
800 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
|
801 { |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
802 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
|
803 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
804 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
|
805 |
21302
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
806 // NOTE: We could maybe do it with a single branch if |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
807 // OCTAVE_HAVE_FAST_INT_OPS, but it would require one more runtime |
8376de2eaf00
define OCTAVE_HAVE_FAST_INT_OPS in octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21245
diff
changeset
|
808 // conversion, so the question is whether it would really be faster. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
809 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
|
810 retval = octave_int_base<int64_t>::max_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
811 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
|
812 retval = octave_int_base<int64_t>::min_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
813 else |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
814 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
|
815 |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
816 return retval; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
817 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
818 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
819 template <> |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
820 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
|
821 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
|
822 { |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
823 #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
|
824 return octave_external_int64_int64_mul (x, y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
825 #else |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
826 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
|
827 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
828 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
829 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
830 #else |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
831 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
832 // Special handler for 64-bit integer multiply. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
833 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
834 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
|
835 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
|
836 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
837 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
838 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
839 // 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
|
840 template <typename T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
841 class octave_int_arith |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
842 : 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
|
843 { }; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
844 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
845 template <typename T> |
4902 | 846 class |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
847 octave_int : public octave_int_base<T> |
4902 | 848 { |
849 public: | |
4943 | 850 typedef T val_type; |
851 | |
4902 | 852 octave_int (void) : ival () { } |
853 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
854 octave_int (T i) : ival (i) { } |
4902 | 855 |
21354
06d15e4e611a
move public data type macros to octave-config.h
John W. Eaton <jwe@octave.org>
parents:
21308
diff
changeset
|
856 #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
|
857 // 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
|
858 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
|
859 : 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
|
860 { } |
21235
b48d65c5df5b
Disable char/int8_t function overloads where not permitted (bug #45411)
Mike Miller <mtmiller@octave.org>
parents:
19861
diff
changeset
|
861 #endif |
18627
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
862 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
863 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
|
864 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
865 octave_int (float d) : ival (octave_int_base<T>::convert_real (d)) { } |
6402 | 866 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
867 #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
|
868 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
|
869 #endif |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
870 |
4902 | 871 octave_int (bool b) : ival (b) { } |
872 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
873 template <typename U> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
874 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
|
875 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
876 template <typename U> |
4902 | 877 octave_int (const octave_int<U>& i) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
878 : ival (octave_int_base<T>::truncate_int (i.value ())) { } |
4902 | 879 |
880 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
881 | |
882 octave_int& operator = (const octave_int<T>& i) | |
883 { | |
884 ival = i.ival; | |
885 return *this; | |
886 } | |
887 | |
888 T value (void) const { return ival; } | |
889 | |
4949 | 890 const unsigned char * iptr (void) const |
891 { return reinterpret_cast<const unsigned char *> (& ival); } | |
892 | |
4902 | 893 bool operator ! (void) const { return ! ival; } |
894 | |
7198 | 895 bool bool_value (void) const { return static_cast<bool> (value ()); } |
896 | |
897 char char_value (void) const { return static_cast<char> (value ()); } | |
898 | |
899 double double_value (void) const { return static_cast<double> (value ()); } | |
900 | |
901 float float_value (void) const { return static_cast<float> (value ()); } | |
902 | |
7177 | 903 operator T (void) const { return value (); } |
904 | |
7198 | 905 // char and bool operators intentionally omitted. |
5533 | 906 |
7198 | 907 operator double (void) const { return double_value (); } |
4902 | 908 |
7198 | 909 operator float (void) const { return float_value (); } |
5030 | 910 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
911 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
912 operator + () const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
913 { return *this; } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
914 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
915 // unary operators & mappers |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
916 #define OCTAVE_INT_UN_OP(OPNAME,NAME) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
917 inline octave_int<T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
918 OPNAME () const \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
919 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
920 return octave_int_arith<T>::NAME (ival); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
921 } |
4902 | 922 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
923 OCTAVE_INT_UN_OP(operator -, minus) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
924 OCTAVE_INT_UN_OP(abs, abs) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
925 OCTAVE_INT_UN_OP(signum, signum) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
926 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
927 #undef OCTAVE_INT_UN_OP |
4902 | 928 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
929 // Homogeneous binary integer operations. |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
930 #define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
931 inline octave_int<T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
932 operator OP (const ARGT& y) const \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
933 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
934 return octave_int_arith<T>::NAME (ival, y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
935 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
936 inline octave_int<T>& \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
937 operator OP##= (const ARGT& y) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
938 { \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
939 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
|
940 return *this; \ |
4952 | 941 } |
942 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
943 OCTAVE_INT_BIN_OP(+, add, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
944 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
945 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
946 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
|
947 OCTAVE_INT_BIN_OP(%, rem, octave_int<T>) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
948 OCTAVE_INT_BIN_OP(<<, lshift, int) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
949 OCTAVE_INT_BIN_OP(>>, rshift, int) |
4952 | 950 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
951 #undef OCTAVE_INT_BIN_OP |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
952 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
953 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
|
954 static octave_int<T> max (void) { return std::numeric_limits<T>::max (); } |
4906 | 955 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
956 static int nbits (void) { return std::numeric_limits<T>::digits; } |
4909 | 957 |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14168
diff
changeset
|
958 static int byte_size (void) { return sizeof (T); } |
4949 | 959 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
960 static const char *type_name (); |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
961 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
962 // The following are provided for convenience. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
963 static const octave_int zero, one; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
964 |
5900 | 965 // Unsafe. This function exists to support the MEX interface. |
966 // You should not use it anywhere else. | |
967 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
968 | |
4902 | 969 private: |
970 | |
971 T ival; | |
972 }; | |
973 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
974 template <typename T> |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
975 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
976 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
|
977 { |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
978 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
|
979 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
980 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
981 template <typename T> |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
982 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
983 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
|
984 { |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
985 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
|
986 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
987 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
988 // No mixed integer binary operations! |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
989 |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
990 namespace octave |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
991 { |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
992 namespace math |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
993 { |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
994 template <typename T> |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
995 bool |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
996 isnan (const octave_int<T>&) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
997 { |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
998 return false; |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
999 } |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1000 } |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1001 } |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1002 |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1003 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1004 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1005 template <typename T> |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1006 OCTAVE_DEPRECATED ("use 'octave::math::isnan' instead") |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1007 bool |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1008 xisnan (const octave_int<T>& x) |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1009 { |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1010 return octave::math::isnan (x); |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1011 } |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1012 |
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
1013 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1014 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1015 // FIXME: can/should any of these be inline? |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
1016 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1017 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1018 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1019 pow (const octave_int<T>&, const octave_int<T>&); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1020 |
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> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1022 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1023 pow (const double& a, const octave_int<T>& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1024 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1025 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1026 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1027 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
|
1028 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1029 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1030 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
|
1031 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
|
1032 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1033 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
1034 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
|
1035 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
|
1036 |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
1037 // 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
|
1038 // 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
|
1039 // one? |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1040 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
1041 extern OCTAVE_API octave_int<T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1042 powf (const float& a, const octave_int<T>& b); |
4952 | 1043 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1044 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1045 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1046 powf (const octave_int<T>& a, const float& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1047 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1048 // Binary relations |
4953 | 1049 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1050 #define OCTAVE_INT_CMP_OP(OP, NAME) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1051 template <typename T1, typename T2> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1052 inline bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1053 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
|
1054 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1055 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
|
1056 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1057 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1058 OCTAVE_INT_CMP_OP (<, lt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1059 OCTAVE_INT_CMP_OP (<=, le) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1060 OCTAVE_INT_CMP_OP (>, gt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1061 OCTAVE_INT_CMP_OP (>=, ge) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1062 OCTAVE_INT_CMP_OP (==, eq) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1063 OCTAVE_INT_CMP_OP (!=, ne) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1064 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1065 #undef OCTAVE_INT_CMP_OP |
4953 | 1066 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1067 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1068 inline std::ostream& |
4902 | 1069 operator << (std::ostream& os, const octave_int<T>& ival) |
1070 { | |
1071 os << ival.value (); | |
1072 return os; | |
1073 } | |
1074 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1075 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1076 inline std::istream& |
4902 | 1077 operator >> (std::istream& is, octave_int<T>& ival) |
1078 { | |
1079 T tmp = 0; | |
1080 is >> tmp; | |
1081 ival = tmp; | |
1082 return is; | |
1083 } | |
1084 | |
18252
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1085 // 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
|
1086 // 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
|
1087 // 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
|
1088 // 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
|
1089 // 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
|
1090 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1091 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1092 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
|
1093 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
|
1094 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1095 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
|
1096 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
|
1097 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1098 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1099 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1100 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
|
1101 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
|
1102 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1103 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
|
1104 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
|
1105 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1106 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1107 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1108 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
|
1109 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
|
1110 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1111 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
|
1112 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
|
1113 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
|
1114 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
|
1115 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1116 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1117 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1118 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
|
1119 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
|
1120 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1121 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
|
1122 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
|
1123 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
|
1124 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
|
1125 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1126 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1127 // Bitwise operations |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
1128 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1129 #define OCTAVE_INT_BITCMP_OP(OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1130 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1131 octave_int<T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1132 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
|
1133 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1134 return x.value () OP y.value (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1135 } |
4906 | 1136 |
1137 OCTAVE_INT_BITCMP_OP (&) | |
1138 OCTAVE_INT_BITCMP_OP (|) | |
1139 OCTAVE_INT_BITCMP_OP (^) | |
1140 | |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1141 #undef OCTAVE_INT_BITCMP_OP |
4906 | 1142 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1143 // General bit shift. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1144 template <typename T> |
4909 | 1145 octave_int<T> |
4920 | 1146 bitshift (const octave_int<T>& a, int n, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1147 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909 | 1148 { |
1149 if (n > 0) | |
4952 | 1150 return (a << n) & mask; |
4909 | 1151 else if (n < 0) |
4952 | 1152 return (a >> -n) & mask; |
4909 | 1153 else |
8963
d1eab3ddb02d
oct-inttypes.h (bitshift): apply mask even if not shifting
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
1154 return a & mask; |
4909 | 1155 } |
1156 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
1157 #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
|
1158 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1159 #define DECLARE_EXTERNAL_LONG_DOUBLE_OP(T, OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1160 extern OCTAVE_API T \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1161 external_double_ ## T ## _ ## OP (double x, T y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1162 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
|
1163 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
|
1164 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1165 #define DECLARE_EXTERNAL_LONG_DOUBLE_OPS(T) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1166 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, add); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1167 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, sub); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1168 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
|
1169 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
|
1170 |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1171 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
|
1172 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
|
1173 |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1174 #endif |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1175 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1176 #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
|
1177 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1178 inline octave_int<T> \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1179 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
|
1180 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1181 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
|
1182 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1183 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1184 inline octave_int<T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1185 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
|
1186 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1187 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
|
1188 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1189 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
1190 #if defined (OCTAVE_INT_USE_LONG_DOUBLE) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1191 // 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
|
1192 #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
|
1193 # define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1194 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1195 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1196 inline octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1197 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
|
1198 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1199 return external_double_octave_int64_ ## NAME (x, y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1200 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1201 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1202 inline octave_uint64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1203 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
|
1204 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1205 return external_double_octave_uint64_ ## NAME (x, y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1206 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1207 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1208 inline octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1209 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
|
1210 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1211 return external_octave_int64_double_ ## NAME (x, y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1212 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1213 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1214 inline octave_uint64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1215 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
|
1216 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1217 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
|
1218 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1219 #else |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
1220 # 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
|
1221 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1222 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1223 inline octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1224 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
|
1225 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1226 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
|
1227 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1228 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1229 inline octave_uint64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1230 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
|
1231 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1232 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
|
1233 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1234 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1235 inline octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1236 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
|
1237 { \ |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1238 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
|
1239 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1240 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1241 inline octave_uint64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1242 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
|
1243 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1244 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
|
1245 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1246 #endif |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1247 #else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1248 // 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
|
1249 #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
|
1250 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1251 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1252 OCTAVE_API octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1253 operator OP (const double&, const octave_int64&); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1254 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1255 OCTAVE_API octave_uint64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1256 operator OP (const double&, const octave_uint64&); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1257 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1258 OCTAVE_API octave_int64 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1259 operator OP (const octave_int64&, const double&); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1260 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1261 OCTAVE_API octave_uint64 \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1262 operator OP (const octave_uint64&, const double&); |
5072 | 1263 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1264 #endif |
5072 | 1265 |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1266 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
|
1267 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
|
1268 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
|
1269 OCTAVE_INT_DOUBLE_BIN_OP (/, div) |
5072 | 1270 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1271 #undef OCTAVE_INT_DOUBLE_BIN_OP0 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1272 #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
|
1273 #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
|
1274 #undef DECLARE_EXTERNAL_LONG_DOUBLE_OPS |
5072 | 1275 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1276 #define OCTAVE_INT_DOUBLE_CMP_OP(OP,NAME) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1277 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1278 inline bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1279 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
|
1280 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1281 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
|
1282 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1283 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1284 inline bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1285 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
|
1286 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1287 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
|
1288 } |
5072 | 1289 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1290 OCTAVE_INT_DOUBLE_CMP_OP (<, lt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1291 OCTAVE_INT_DOUBLE_CMP_OP (<=, le) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1292 OCTAVE_INT_DOUBLE_CMP_OP (>=, ge) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1293 OCTAVE_INT_DOUBLE_CMP_OP (>, gt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1294 OCTAVE_INT_DOUBLE_CMP_OP (==, eq) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1295 OCTAVE_INT_DOUBLE_CMP_OP (!=, ne) |
5072 | 1296 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1297 #undef OCTAVE_INT_DOUBLE_CMP_OP |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1298 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1299 // Floats are handled by simply converting to doubles. |
5072 | 1300 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1301 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1302 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1303 inline octave_int<T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1304 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
|
1305 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1306 return x OP static_cast<double> (y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1307 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1308 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1309 inline octave_int<T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1310 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
|
1311 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1312 return static_cast<double> (x) OP y; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1313 } |
5072 | 1314 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1315 OCTAVE_INT_FLOAT_BIN_OP (+) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1316 OCTAVE_INT_FLOAT_BIN_OP (-) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1317 OCTAVE_INT_FLOAT_BIN_OP (*) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1318 OCTAVE_INT_FLOAT_BIN_OP (/) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1319 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1320 #undef OCTAVE_INT_FLOAT_BIN_OP |
5072 | 1321 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1322 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1323 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1324 inline bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1325 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
|
1326 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1327 return x OP static_cast<double> (y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1328 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1329 template <typename T> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1330 bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1331 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
|
1332 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1333 return static_cast<double> (x) OP y; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22107
diff
changeset
|
1334 } |
4902 | 1335 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1336 OCTAVE_INT_FLOAT_CMP_OP (<) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1337 OCTAVE_INT_FLOAT_CMP_OP (<=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1338 OCTAVE_INT_FLOAT_CMP_OP (>=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1339 OCTAVE_INT_FLOAT_CMP_OP (>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1340 OCTAVE_INT_FLOAT_CMP_OP (==) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1341 OCTAVE_INT_FLOAT_CMP_OP (!=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1342 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1343 #undef OCTAVE_INT_FLOAT_CMP_OP |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1344 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1345 template <typename T> |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1346 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1347 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
|
1348 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1349 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
|
1350 const T yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1351 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
|
1352 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1353 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1354 template <typename T> |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1355 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1356 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
|
1357 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1358 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
|
1359 const T yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1360 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
|
1361 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1362 |
4902 | 1363 #endif |