Mercurial > octave
annotate liboctave/util/oct-inttypes.cc @ 31400:e7fc6251b698
Use more specialized API flags for template classes (bug #61711).
* oct-conf-post-public.in.h: Add more specific attributes for exporting
(specialized) template class instantiations.
* liboctave/array/Array.h (Array<T>): Also dllexport member function definitions
(on Windows).
* Array-*.cc: Move extern template class declarations to before including
"Array-base.cc" to avoid some implicit instantiations.
* all files: Use new macros for visibility attributes.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Sat, 05 Nov 2022 19:15:13 +0100 |
parents | 014030798d5e |
children | 9978ef12aea3 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30037
diff
changeset
|
3 // Copyright (C) 2004-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
5072 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
5072 | 28 #endif |
29 | |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
30 #include "fpucw-wrappers.h" |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
31 #include "lo-error.h" |
5072 | 32 #include "oct-inttypes.h" |
33 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
34 template <typename T> |
30037
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
35 const octave_int<T> octave_int<T>::s_zero (static_cast<T> (0)); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
36 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
37 template <typename T> |
30037
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
38 const octave_int<T> octave_int<T>::s_one (static_cast<T> (1)); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
39 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
40 // Define type names. |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
41 |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
42 #define DEFINE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
43 template <> \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
44 OCTAVE_API const char * \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
45 octave_int<TYPE>::type_name (void) { return TYPENAME; } |
5072 | 46 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
47 DEFINE_OCTAVE_INT_TYPENAME (int8_t, "int8") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
48 DEFINE_OCTAVE_INT_TYPENAME (int16_t, "int16") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
49 DEFINE_OCTAVE_INT_TYPENAME (int32_t, "int32") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
50 DEFINE_OCTAVE_INT_TYPENAME (int64_t, "int64") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
51 DEFINE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
52 DEFINE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
53 DEFINE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
54 DEFINE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
55 |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
56 template <class T> |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
57 template <class S> |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
58 T |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
59 octave_int_base<T>::convert_real (const S& value) |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
60 { |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
61 // Compute proper thresholds. |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
62 static const S thmin = compute_threshold (static_cast<S> (min_val ()), |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
63 min_val ()); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
64 static const S thmax = compute_threshold (static_cast<S> (max_val ()), |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
65 max_val ()); |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
66 if (octave::math::isnan (value)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
67 return static_cast<T> (0); |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
68 else if (value < thmin) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
69 return min_val (); |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
70 else if (value > thmax) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
71 return max_val (); |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
72 else |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
73 { |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
74 S rvalue = octave::math::round (value); |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
75 return static_cast<T> (rvalue); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
76 } |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
77 } |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
78 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
79 #define INSTANTIATE_CONVERT_REAL_1(T, S) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
80 template \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
81 OCTAVE_API \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
82 T \ |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
83 octave_int_base<T>::convert_real (const S&) |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
84 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
85 #define INSTANTIATE_CONVERT_REAL(S) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
86 INSTANTIATE_CONVERT_REAL_1 (int8_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
87 INSTANTIATE_CONVERT_REAL_1 (uint8_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
88 INSTANTIATE_CONVERT_REAL_1 (int16_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
89 INSTANTIATE_CONVERT_REAL_1 (uint16_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
90 INSTANTIATE_CONVERT_REAL_1 (int32_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
91 INSTANTIATE_CONVERT_REAL_1 (uint32_t, S); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
92 INSTANTIATE_CONVERT_REAL_1 (int64_t, S); \ |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
93 INSTANTIATE_CONVERT_REAL_1 (uint64_t, S) |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
94 |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
95 INSTANTIATE_CONVERT_REAL (double); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
96 INSTANTIATE_CONVERT_REAL (float); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
97 #if defined (OCTAVE_INT_USE_LONG_DOUBLE) |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
98 INSTANTIATE_CONVERT_REAL (long double); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
99 #endif |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
100 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21574
diff
changeset
|
101 #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:
17843
diff
changeset
|
102 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
103 # 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:
17843
diff
changeset
|
104 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
105 # define DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES(T) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
106 template <typename xop> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
107 bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
108 octave_int_cmp_op::external_mop (double x, T y) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
109 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
110 unsigned int oldcw = octave_begin_long_double_rounding (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
111 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
112 bool retval = xop::op (static_cast<long double> (x), \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
113 static_cast<long double> (y)); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
114 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
115 octave_end_long_double_rounding (oldcw); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
116 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
117 return retval; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
118 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
119 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
120 template <typename xop> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
121 bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
122 octave_int_cmp_op::external_mop (T x, double y) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
123 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
124 unsigned int oldcw = octave_begin_long_double_rounding (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
125 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
126 bool retval = xop::op (static_cast<long double> (x), \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
127 static_cast<long double> (y)); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
128 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
129 octave_end_long_double_rounding (oldcw); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
130 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
131 return retval; \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
132 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
133 |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
134 DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (int64_t) |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
135 DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (uint64_t) |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
136 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
137 # define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP(OP, T) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
138 template OCTAVE_API bool \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
139 octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (double, T); \ |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
140 \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
141 template OCTAVE_API bool \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
142 octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (T, double) |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
143 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
144 # define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS(T) \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
145 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (lt, T); \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
146 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (le, T); \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
147 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (gt, T); \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
148 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ge, T); \ |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
149 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (eq, T); \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
150 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ne, T) |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
151 |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
152 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (int64_t); |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
153 INSTANTIATE_LONG_DOUBLE_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:
17843
diff
changeset
|
154 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
155 uint64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
156 octave_external_uint64_uint64_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:
17843
diff
changeset
|
157 { |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
158 unsigned int oldcw = octave_begin_long_double_rounding (); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
159 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
160 uint64_t retval = octave_int_arith_base<uint64_t, false>::mul_internal (x, y); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
161 |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
162 octave_end_long_double_rounding (oldcw); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
163 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
164 return retval; |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
165 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
166 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
167 int64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
168 octave_external_int64_int64_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:
17843
diff
changeset
|
169 { |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
170 unsigned int oldcw = octave_begin_long_double_rounding (); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
171 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
172 int64_t retval = octave_int_arith_base<int64_t, true>::mul_internal (x, y); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
173 |
22073
d18c63a45070
wrappers for setting long double rounding mode (bug #48319)
John W. Eaton <jwe@octave.org>
parents:
22022
diff
changeset
|
174 octave_end_long_double_rounding (oldcw); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
175 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
176 return retval; |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
177 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
178 |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
179 // Note that if we return long double it is apparently possible for |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
180 // truncation to happen at the point of storing the result in retval, |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
181 // which can happen after we end long double rounding. Attempt to avoid |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
182 // that problem by storing the full precision temporary value in the |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
183 // integer value before we end the long double rounding mode. |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
184 // Similarly, the conversion from the 64-bit integer type to long double |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
185 // must also occur in long double rounding mode. |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
186 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
187 # define DEFINE_OCTAVE_LONG_DOUBLE_OP(T, OP, NAME) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
188 T \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
189 external_double_ ## T ## _ ## NAME (double x, T y) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
190 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
191 unsigned int oldcw = octave_begin_long_double_rounding (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
192 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
193 T retval = T (x OP static_cast<long double> (y.value ())); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
194 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
195 octave_end_long_double_rounding (oldcw); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
196 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
197 return retval; \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
198 } \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
199 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
200 T \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
201 external_ ## T ## _double_ ## NAME (T x, double y) \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
202 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
203 unsigned int oldcw = octave_begin_long_double_rounding (); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
204 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
205 T retval = T (static_cast<long double> (x.value ()) OP y); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
206 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
207 octave_end_long_double_rounding (oldcw); \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
208 \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
209 return retval; \ |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
210 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
211 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
212 # define DEFINE_OCTAVE_LONG_DOUBLE_OPS(T) \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
213 DEFINE_OCTAVE_LONG_DOUBLE_OP (T, +, add); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
214 DEFINE_OCTAVE_LONG_DOUBLE_OP (T, -, sub); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
215 DEFINE_OCTAVE_LONG_DOUBLE_OP (T, *, mul); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
216 DEFINE_OCTAVE_LONG_DOUBLE_OP (T, /, div) |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
217 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
218 DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_int64); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
219 DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_uint64); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
220 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
221 # endif |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
222 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
223 #else |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
224 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
225 // Define comparison operators |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
226 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
227 template <typename xop> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
228 bool |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8797
diff
changeset
|
229 octave_int_cmp_op::emulate_mop (uint64_t x, double y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
230 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
231 // The following cast changes the value to 2^64 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
232 // of `uint64_t`). Take care to handle this correctly (e.g., don't cast back |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
233 // to `uint64_t`)! |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
234 static const double xxup |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
235 = static_cast<double> (std::numeric_limits<uint64_t>::max ()); |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
236 // This converts to the nearest double. Unless there's an equality, the |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
237 // result is clear. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
238 double xx = x; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
239 if (xx != y) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
240 return xop::op (xx, y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
241 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
242 { |
21004
f7e416862e90
doc: fix spelling of "occurred".
Rafael Laboissiere <rafael@laboissiere.net>
parents:
19861
diff
changeset
|
243 // If equality occurred we compare as integers. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
244 if (xx == xxup) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
245 return xop::gtval; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
246 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
247 return xop::op (x, static_cast<uint64_t> (xx)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
248 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
249 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
250 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
251 template <typename xop> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
252 bool |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8797
diff
changeset
|
253 octave_int_cmp_op::emulate_mop (int64_t x, double y) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
254 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
255 // The following cast changes the value to 2^63 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
256 // of `int64_t`). Take care to handle this correctly (e.g., don't cast back |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
257 // to `int64_t`)! The same applies to the lower limit on systems using one's |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
258 // complement. |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
259 static const double xxup |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
260 = static_cast<double> (std::numeric_limits<int64_t>::max ()); |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
261 static const double xxlo |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
262 = static_cast<double> (std::numeric_limits<int64_t>::min ()); |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
263 // This converts to the nearest double. Unless there's an equality, the |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
264 // result is clear. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
265 double xx = x; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
266 if (xx != y) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
267 return xop::op (xx, y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
268 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
269 { |
21004
f7e416862e90
doc: fix spelling of "occurred".
Rafael Laboissiere <rafael@laboissiere.net>
parents:
19861
diff
changeset
|
270 // If equality occurred we compare as integers. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
271 if (xx == xxup) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
272 return xop::gtval; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
273 else if (xx == xxlo) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
274 return xop::ltval; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
275 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
276 return xop::op (x, static_cast<int64_t> (xx)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
277 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
278 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
279 } |
5072 | 280 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
281 // We define double-int operations by reverting the operator |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
282 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
283 // A trait class reverting the operator |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
284 template <typename xop> |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
285 class rev_op |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
286 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
287 public: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
288 typedef xop op; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
289 }; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
290 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
291 #define DEFINE_REVERTED_OPERATOR(OP1, OP2) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
292 template <> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
293 class rev_op<octave_int_cmp_op::OP1> \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
294 { \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
295 public: \ |
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
296 typedef octave_int_cmp_op::OP2 op; \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
297 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
298 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
299 DEFINE_REVERTED_OPERATOR (lt, gt); |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
300 DEFINE_REVERTED_OPERATOR (gt, lt); |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
301 DEFINE_REVERTED_OPERATOR (le, ge); |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
302 DEFINE_REVERTED_OPERATOR (ge, le); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
303 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
304 template <typename xop> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
305 bool |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8797
diff
changeset
|
306 octave_int_cmp_op::emulate_mop (double x, uint64_t y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
307 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
308 typedef typename rev_op<xop>::op rop; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
309 return mop<rop> (y, x); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
310 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
311 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
312 template <typename xop> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
313 bool |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8797
diff
changeset
|
314 octave_int_cmp_op::emulate_mop (double x, int64_t y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
315 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
316 typedef typename rev_op<xop>::op rop; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
317 return mop<rop> (y, x); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
318 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
319 |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
320 // Define handlers for (u)int64 multiplication. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
321 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
322 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
323 uint64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
324 octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t x, uint64_t y) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
325 { |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
326 // Get upper words |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
327 uint64_t ux = x >> 32; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
328 uint64_t uy = y >> 32; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
329 uint64_t res; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
330 if (ux) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
331 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
332 if (uy) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
333 goto overflow; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
334 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
335 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
336 uint64_t ly = static_cast<uint32_t> (y); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
337 uint64_t uxly = ux*ly; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
338 if (uxly >> 32) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
339 goto overflow; |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
340 uxly <<= 32; // never overflows |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
341 uint64_t lx = static_cast<uint32_t> (x); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
342 uint64_t lxly = lx*ly; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
343 res = add (uxly, lxly); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
344 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
345 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
346 else if (uy) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
347 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
348 uint64_t lx = static_cast<uint32_t> (x); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
349 uint64_t uylx = uy*lx; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
350 if (uylx >> 32) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
351 goto overflow; |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
352 uylx <<= 32; // never overflows |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
353 uint64_t ly = static_cast<uint32_t> (y); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
354 uint64_t lylx = ly*lx; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
355 res = add (uylx, lylx); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
356 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
357 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
358 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
359 uint64_t lx = static_cast<uint32_t> (x); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
360 uint64_t ly = static_cast<uint32_t> (y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
361 res = lx*ly; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
362 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
363 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
364 return res; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
365 |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
366 overflow: |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
367 return max_val (); |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
368 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
369 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
370 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
371 int64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17843
diff
changeset
|
372 octave_int_arith_base<int64_t, true>::mul_internal (int64_t x, int64_t y) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
373 { |
25843
144820478378
eliminate OCTAVE_HAVE_FAST_INT_OPS macro and associated code (bug #54572)
John W. Eaton <jwe@octave.org>
parents:
25673
diff
changeset
|
374 // The signed case is far worse. The problem is that even if neither |
144820478378
eliminate OCTAVE_HAVE_FAST_INT_OPS macro and associated code (bug #54572)
John W. Eaton <jwe@octave.org>
parents:
25673
diff
changeset
|
375 // integer fits into signed 32-bit range, the result may still be OK. |
144820478378
eliminate OCTAVE_HAVE_FAST_INT_OPS macro and associated code (bug #54572)
John W. Eaton <jwe@octave.org>
parents:
25673
diff
changeset
|
376 // Uh oh. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
377 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
378 // Essentially, what we do is compute sign, multiply absolute values |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
379 // (as above) and impose the sign. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
380 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
381 uint64_t usx = octave_int_abs (x); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
382 uint64_t usy = octave_int_abs (y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
383 bool positive = (x < 0) == (y < 0); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
384 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
385 // Get upper words |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
386 uint64_t ux = usx >> 32; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
387 uint64_t uy = usy >> 32; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
388 uint64_t res; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
389 if (ux) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
390 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
391 if (uy) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
392 goto overflow; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
393 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
394 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
395 uint64_t ly = static_cast<uint32_t> (usy); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
396 uint64_t uxly = ux*ly; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
397 if (uxly >> 32) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
398 goto overflow; |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
399 uxly <<= 32; // never overflows |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
400 uint64_t lx = static_cast<uint32_t> (usx); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
401 uint64_t lxly = lx*ly; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
402 res = uxly + lxly; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
403 if (res < uxly) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
404 goto overflow; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
405 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
406 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
407 else if (uy) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
408 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
409 uint64_t lx = static_cast<uint32_t> (usx); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
410 uint64_t uylx = uy*lx; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
411 if (uylx >> 32) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
412 goto overflow; |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
413 uylx <<= 32; // never overflows |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
414 uint64_t ly = static_cast<uint32_t> (usy); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
415 uint64_t lylx = ly*lx; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
416 res = uylx + lylx; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
417 if (res < uylx) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
418 goto overflow; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
419 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
420 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
421 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
422 uint64_t lx = static_cast<uint32_t> (usx); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
423 uint64_t ly = static_cast<uint32_t> (usy); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
424 res = lx*ly; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
425 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
426 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
427 if (positive) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
428 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
429 if (res > static_cast<uint64_t> (max_val ())) |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
430 return max_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
431 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
432 return static_cast<int64_t> (res); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
433 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
434 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
435 { |
23782
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
436 if (res > static_cast<uint64_t> (min_val ())) |
203d3676730f
Fix overflow in signed type detected by -fsanitize=undefined (bug #51530).
Rik <rik@octave.org>
parents:
23639
diff
changeset
|
437 return min_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
438 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
439 return -static_cast<int64_t> (res); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
440 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
441 |
27933
863ae57eee69
maint: Use Octave coding conventions in liboctave/
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
442 overflow: |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
443 return positive ? max_val () : min_val (); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
444 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
445 } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
446 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
447 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
448 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
449 operator + (const octave_uint64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
450 { |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14427
diff
changeset
|
451 return (y < 0) ? x - octave_uint64 (-y) : x + octave_uint64 (y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
452 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
453 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
454 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
455 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
456 operator + (const double& x, const octave_uint64& y) |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
457 { |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
458 return y + x; |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
459 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
460 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
461 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
462 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
463 operator + (const octave_int64& x, const double& y) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
464 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
465 // The following cast changes the value to 2^63 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
466 // of `int64_t`). |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
467 if (fabs (y) < static_cast<double> (octave_int64::max ())) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
468 return x + octave_int64 (y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
469 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
470 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
471 // If the number is within the int64 range (the most common case, |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
472 // probably), the above will work as expected. If not, it's more |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
473 // complicated - as long as y is within _twice_ the signed range, the |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
474 // result may still be an integer. An instance of such an operation is |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
475 // 3*2^62 + (1+intmin ('int64')) that should yield int64 (2^62) + 1. |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
476 // So what we do is to try to convert y/2 and add it twice. Note that |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
477 // if y/2 overflows, the result must overflow as well, and that y/2 |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
478 // cannot be a fractional number. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
479 octave_int64 y2 (y / 2); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
480 return (x + y2) + y2; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
481 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
482 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
483 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
484 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
485 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
486 operator + (const double& x, const octave_int64& y) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
487 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
488 return y + x; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
489 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
490 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
491 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
492 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
493 operator - (const octave_uint64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
494 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
495 return x + (-y); |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
496 } |
5072 | 497 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
498 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
499 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
500 operator - (const double& x, const octave_uint64& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
501 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
502 // The following cast changes the value to 2^64 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
503 // of `uint64_t`). |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
504 if (x < static_cast<double> (octave_uint64::max ())) |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14427
diff
changeset
|
505 return octave_uint64 (x) - y; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
506 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
507 { |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
508 // Again a trick to get the corner cases right. Things like |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
509 // 3^2^63 - intmax ('uint64') should produce the correct result, i.e. |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
510 // int64 (2^63) + 1. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
511 const double p2_64 = std::pow (2.0, 64); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
512 if (y.bool_value ()) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
513 { |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
514 const uint64_t p2_64my = (~y.value ()) + 1; // Equals 2^64 - y |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
515 return octave_uint64 (x - p2_64) + octave_uint64 (p2_64my); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
516 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
517 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
518 return octave_uint64 (p2_64); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
519 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
520 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
521 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
522 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
523 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
524 operator - (const octave_int64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
525 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
526 return x + (-y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
527 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
528 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
529 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
530 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
531 operator - (const double& x, const octave_int64& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
532 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
533 static const bool twosc = (std::numeric_limits<int64_t>::min () |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
534 < -std::numeric_limits<int64_t>::max ()); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
535 // In case of symmetric integers (not two's complement), this will probably |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
536 // be eliminated at compile time. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
537 if (twosc && y.value () == std::numeric_limits<int64_t>::min ()) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
538 return octave_int64 (x + std::pow (2.0, 63)); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
539 else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
540 return x + (-y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
541 } |
5072 | 542 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
543 // NOTE: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
544 // Emulated mixed multiplications are tricky due to possible precision loss. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
545 // Here, after sorting out common cases for speed, we follow the strategy |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
546 // of converting the double number into the form sign * 64-bit integer * |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
547 // 2^exponent, multiply the 64-bit integers to get a 128-bit number, split that |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
548 // number into 32-bit words and form 4 double-valued summands (none of which |
21751
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
549 // loses precision), then convert these into integers and sum them. Though it |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
550 // is not immediately obvious, this should work even w.r.t. rounding (none of |
b571fc85953f
maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents:
21724
diff
changeset
|
551 // the summands lose precision). |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
552 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
553 // Multiplies two unsigned 64-bit ints to get a 128-bit number represented |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
554 // as four 32-bit words. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
555 static void |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
556 umul128 (uint64_t x, uint64_t y, uint32_t w[4]) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
557 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
558 uint64_t lx = static_cast<uint32_t> (x); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
559 uint64_t ux = x >> 32; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
560 uint64_t ly = static_cast<uint32_t> (y); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
561 uint64_t uy = y >> 32; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
562 uint64_t a = lx * ly; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
563 w[0] = a; a >>= 32; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
564 uint64_t uxly = ux*ly; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
565 uint64_t uylx = uy*lx; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
566 a += static_cast<uint32_t> (uxly); uxly >>= 32; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
567 a += static_cast<uint32_t> (uylx); uylx >>= 32; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
568 w[1] = a; a >>= 32; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
569 uint64_t uxuy = ux * uy; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
570 a += uxly; a += uylx; a += uxuy; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
571 w[2] = a; a >>= 32; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
572 w[3] = a; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
573 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
574 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
575 // Splits a double into bool sign, unsigned 64-bit mantissa and int exponent |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
576 static void |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
577 dblesplit (double x, bool& sign, uint64_t& mtis, int& exp) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
578 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
579 sign = x < 0; x = fabs (x); |
22079
e0dbd81fd9b1
provide wrapper for frexp and frexpf (bug #48363)
Mike Miller <mtmiller@octave.org>
parents:
22073
diff
changeset
|
580 x = octave::math::frexp (x, &exp); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
581 exp -= 52; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
582 mtis = static_cast<uint64_t> (ldexp (x, 52)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
583 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
584 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
585 // Gets a double number from a |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
586 // 32-bit unsigned integer mantissa, exponent, and sign. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
587 static double |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
588 dbleget (bool sign, uint32_t mtis, int exp) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
589 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
590 double x = ldexp (static_cast<double> (mtis), exp); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
591 return sign ? -x : x; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
592 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
593 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
594 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
595 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
596 operator * (const octave_uint64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
597 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
598 // The following cast changes the value to 2^64 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
599 // of `uint64_t`). |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
600 if (y >= 0 && y < static_cast<double> (octave_uint64::max ()) |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
601 && y == octave::math::fix (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
602 return x * octave_uint64 (static_cast<uint64_t> (y)); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
603 else if (y == 0.5) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
604 return x / octave_uint64 (static_cast<uint64_t> (2)); |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
605 else if (y < 0 || octave::math::isnan (y) || octave::math::isinf (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
606 return octave_uint64 (x.value () * y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
607 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
608 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
609 bool sign; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
610 uint64_t my; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
611 int e; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
612 dblesplit (y, sign, my, e); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
613 uint32_t w[4]; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
614 umul128 (x.value (), my, w); |
30037
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
615 octave_uint64 res = octave_uint64::s_zero; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
616 for (short i = 0; i < 4; i++) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
617 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
618 res += octave_uint64 (dbleget (sign, w[i], e)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
619 e += 32; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
620 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
621 return res; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
622 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
623 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
624 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
625 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
626 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
627 operator * (const double& x, const octave_uint64& y) |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
628 { |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
629 return y * x; |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
630 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
631 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
632 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
633 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
634 operator * (const octave_int64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
635 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
636 // The following cast changes the value to 2^63 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
637 // of `int64_t`). |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
638 if (fabs (y) < static_cast<double> (octave_int64::max ()) |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
639 && y == octave::math::fix (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
640 return x * octave_int64 (static_cast<int64_t> (y)); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
641 else if (fabs (y) == 0.5) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
642 return x / octave_int64 (static_cast<uint64_t> (4*y)); |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21751
diff
changeset
|
643 else if (octave::math::isnan (y) || octave::math::isinf (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
644 return octave_int64 (x.value () * y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
645 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
646 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
647 bool sign; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
648 uint64_t my; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
649 int e; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
650 dblesplit (y, sign, my, e); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
651 uint32_t w[4]; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
652 sign = (sign != (x.value () < 0)); |
8293
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
653 umul128 (octave_int_abs (x.value ()), my, w); |
30037
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
654 octave_int64 res = octave_int64::s_zero; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
655 for (short i = 0; i < 4; i++) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
656 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
657 res += octave_int64 (dbleget (sign, w[i], e)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
658 e += 32; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
659 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
660 return res; |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
661 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
662 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
663 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
664 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
665 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
666 operator * (const double& x, const octave_int64& y) |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
667 { |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
668 return y * x; |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
669 } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
670 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
671 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
672 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
673 operator / (const double& x, const octave_uint64& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
674 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
675 return octave_uint64 (x / static_cast<double> (y)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
676 } |
5072 | 677 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
678 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
679 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
680 operator / (const double& x, const octave_int64& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
681 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
682 return octave_int64 (x / static_cast<double> (y)); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
683 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
684 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
685 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
686 OCTAVE_API octave_uint64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
687 operator / (const octave_uint64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
688 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
689 // The following cast changes the value to 2^64 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
690 // of `uint64_t`). |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
691 if (y >= 0 && y < static_cast<double> (octave_uint64::max ()) |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
692 && y == octave::math::fix (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
693 return x / octave_uint64 (y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
694 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
695 return x * (1.0/y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
696 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
697 |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
698 template <> |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
699 OCTAVE_API octave_int64 |
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
700 operator / (const octave_int64& x, const double& y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
701 { |
30867
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
702 // The following cast changes the value to 2^63 (which is outside the range |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
703 // of `int64_t`). |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
704 if (fabs (y) < static_cast<double> (octave_int64::max ()) |
014030798d5e
Avoid issues when converting large integers to floating point (bug #62212).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
705 && y == octave::math::fix (y)) |
25848
4c25d8d3a0ae
style fixes for oct-inttypes.cc
John W. Eaton <jwe@octave.org>
parents:
25843
diff
changeset
|
706 return x / octave_int64 (y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
707 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
708 return x * (1.0/y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
709 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
710 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
711 #define INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, T1, T2) \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
712 template OCTAVE_API bool \ |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8797
diff
changeset
|
713 octave_int_cmp_op::emulate_mop<octave_int_cmp_op::OP> (T1 x, T2 y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
714 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
715 #define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP) \ |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
716 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, int64_t); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
717 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, double, uint64_t); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
718 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, int64_t, double); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
719 INSTANTIATE_INT64_DOUBLE_CMP_OP0 (OP, uint64_t, double) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
720 |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
721 INSTANTIATE_INT64_DOUBLE_CMP_OP (lt); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
722 INSTANTIATE_INT64_DOUBLE_CMP_OP (le); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
723 INSTANTIATE_INT64_DOUBLE_CMP_OP (gt); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
724 INSTANTIATE_INT64_DOUBLE_CMP_OP (ge); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
725 INSTANTIATE_INT64_DOUBLE_CMP_OP (eq); |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
726 INSTANTIATE_INT64_DOUBLE_CMP_OP (ne); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
727 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
728 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
729 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
730 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
731 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
732 pow (const octave_int<T>& a, const octave_int<T>& b) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
733 { |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
734 octave_int<T> retval; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
735 |
30037
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
736 const octave_int<T> zero = octave_int<T>::s_zero; |
8d6bdd961494
maint: use "s_" prefix for static class variables in oct-inttypes.h
Rik <rik@octave.org>
parents:
30031
diff
changeset
|
737 const octave_int<T> one = octave_int<T>::s_one; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
738 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
739 if (b == zero || a == one) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
740 retval = one; |
8864 | 741 else if (b < zero) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
742 { |
8864 | 743 if (a == -one) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
744 retval = (b.value () % 2) ? a : one; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
745 else |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
746 retval = zero; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
747 } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
748 else |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
749 { |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
750 octave_int<T> a_val = a; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
751 T b_val = b; // no need to do saturation on b |
5072 | 752 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
753 retval = a; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
754 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
755 b_val -= 1; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
756 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
757 while (b_val != 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
758 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
759 if (b_val & 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
760 retval = retval * a_val; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
761 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
762 b_val = b_val >> 1; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
763 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
764 if (b_val) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
765 a_val = a_val * a_val; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
766 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
767 } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
768 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
769 return retval; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
770 } |
5072 | 771 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
772 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
773 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
774 pow (const double& a, const octave_int<T>& b) |
23639 | 775 { return octave_int<T> (std::pow (a, b.double_value ())); } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
776 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
777 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
778 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
779 pow (const octave_int<T>& a, const double& b) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
780 { |
22402
4caa7b28d183
maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
781 return ((b >= 0 && b < std::numeric_limits<T>::digits |
26415
f553b02e13c6
Use "x == fix (x)" to detect integers rather than floor or round (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
782 && b == octave::math::fix (b)) |
8195
ec3a55bd876b
fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents:
8192
diff
changeset
|
783 ? pow (a, octave_int<T> (static_cast<T> (b))) |
23639 | 784 : octave_int<T> (std::pow (a.double_value (), b))); |
8195
ec3a55bd876b
fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents:
8192
diff
changeset
|
785 } |
5072 | 786 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
787 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
788 octave_int<T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
789 pow (const float& a, const octave_int<T>& b) |
23639 | 790 { return octave_int<T> (std::pow (a, b.float_value ())); } |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
791 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
792 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
793 octave_int<T> |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
794 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
|
795 { |
22402
4caa7b28d183
maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
796 return ((b >= 0 && b < std::numeric_limits<T>::digits |
26415
f553b02e13c6
Use "x == fix (x)" to detect integers rather than floor or round (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
797 && b == octave::math::fix (b)) |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
798 ? pow (a, octave_int<T> (static_cast<T> (b))) |
23639 | 799 : octave_int<T> (std::pow (a.double_value (), |
800 static_cast<double> (b)))); | |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
801 } |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
802 |
23639 | 803 // FIXME: Do we really need a differently named single-precision function |
804 // integer power function here instead of an overloaded one? | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
805 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
806 octave_int<T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
807 powf (const float& a, const octave_int<T>& b) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
808 { return octave_int<T> (pow (a, b.float_value ())); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
809 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
21004
diff
changeset
|
810 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
811 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
7521
diff
changeset
|
812 powf (const octave_int<T>& a, const float& b) |
8195
ec3a55bd876b
fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents:
8192
diff
changeset
|
813 { |
22402
4caa7b28d183
maint: Style check C++ code in liboctave/
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
814 return ((b >= 0 && b < std::numeric_limits<T>::digits |
26415
f553b02e13c6
Use "x == fix (x)" to detect integers rather than floor or round (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
815 && b == octave::math::fix (b)) |
8195
ec3a55bd876b
fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents:
8192
diff
changeset
|
816 ? pow (a, octave_int<T> (static_cast<T> (b))) |
23639 | 817 : octave_int<T> (std::pow (a.double_value (), |
818 static_cast<double> (b)))); | |
8195
ec3a55bd876b
fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents:
8192
diff
changeset
|
819 } |
5072 | 820 |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
821 #define INSTANTIATE_INTTYPE(T) \ |
31400
e7fc6251b698
Use more specialized API flags for template classes (bug #61711).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30867
diff
changeset
|
822 template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_API octave_int<T>; \ |
25849
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
823 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
824 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
825 pow (const octave_int<T>&, const octave_int<T>&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
826 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
827 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
828 pow (const double&, const octave_int<T>&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
829 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
830 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
831 pow (const octave_int<T>&, const double&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
832 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
833 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
834 pow (const float&, const octave_int<T>&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
835 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
836 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
837 pow (const octave_int<T>&, const float&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
838 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
839 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
840 powf (const float&, const octave_int<T>&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
841 \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
842 template OCTAVE_API octave_int<T> \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
843 powf (const octave_int<T>&, const float&); \ |
2ee4d384224f
* oct-inttypes.cc: Make a few more style fixes.
John W. Eaton <jwe@octave.org>
parents:
25848
diff
changeset
|
844 \ |
22197
e43d83253e28
refill multi-line macro definitions
John W. Eaton <jwe@octave.org>
parents:
22079
diff
changeset
|
845 template OCTAVE_API octave_int<T> \ |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
846 bitshift (const octave_int<T>&, int, const octave_int<T>&); |
5072 | 847 |
5828 | 848 INSTANTIATE_INTTYPE (int8_t); |
849 INSTANTIATE_INTTYPE (int16_t); | |
850 INSTANTIATE_INTTYPE (int32_t); | |
851 INSTANTIATE_INTTYPE (int64_t); | |
5072 | 852 |
5828 | 853 INSTANTIATE_INTTYPE (uint8_t); |
854 INSTANTIATE_INTTYPE (uint16_t); | |
855 INSTANTIATE_INTTYPE (uint32_t); | |
856 INSTANTIATE_INTTYPE (uint64_t); | |
5072 | 857 |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
858 /* |
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
859 |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
860 %!assert (intmax ("int64") / intmin ("int64"), int64 (-1)) |
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
861 %!assert (intmin ("int64") / int64 (-1), intmax ("int64")) |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
862 %!assert (int64 (2^63), intmax ("int64")) |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
863 %!assert (uint64 (2^64), intmax ("uint64")) |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
864 %!test |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
865 %! a = 1.9*2^61; b = uint64 (a); b++; assert (b > a); |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
866 %!test |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
867 %! a = -1.9*2^61; b = int64 (a); b++; assert (b > a); |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
868 %!test |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
869 %! a = int64 (-2^60) + 2; assert (1.25*a == (5*a)/4); |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
870 %!test |
30031
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
871 %! a = uint64 (2^61) + 2; assert (1.25*a == (5*a)/4); |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
872 %!assert (int32 (2^31+0.5), intmax ("int32")) |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
873 %!assert (int32 (-2^31-0.5), intmin ("int32")) |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
874 %!assert ((int64 (2^62)+1)^1, int64 (2^62)+1) |
cf60f8f636f6
Start deprecation of Fortran exponentiation operator.
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
875 %!assert ((int64 (2^30)+1)^2, int64 (2^60+2^31) + 1) |
14427
d07e989686b0
Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
876 |
25673
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
877 %!assert <54382> (uint8 (char (128)), uint8 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
878 %!assert <54382> (uint8 (char (255)), uint8 (255)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
879 %!assert <54382> (int8 (char (128)), int8 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
880 %!assert <54382> (int8 (char (255)), int8 (255)) |
18628
c644ed73c6ce
tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents:
18017
diff
changeset
|
881 |
25673
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
882 %!assert <54382> (uint16 (char (128)), uint16 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
883 %!assert <54382> (uint16 (char (255)), uint16 (255)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
884 %!assert <54382> (int16 (char (128)), int16 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
885 %!assert <54382> (int16 (char (255)), int16 (255)) |
18628
c644ed73c6ce
tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents:
18017
diff
changeset
|
886 |
25673
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
887 %!assert <54382> (uint32 (char (128)), uint32 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
888 %!assert <54382> (uint32 (char (255)), uint32 (255)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
889 %!assert <54382> (int32 (char (128)), int32 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
890 %!assert <54382> (int32 (char (255)), int32 (255)) |
18628
c644ed73c6ce
tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents:
18017
diff
changeset
|
891 |
25673
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
892 %!assert <54382> (uint64 (char (128)), uint64 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
893 %!assert <54382> (uint64 (char (255)), uint64 (255)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
894 %!assert <54382> (int64 (char (128)), int64 (128)) |
a8b0fb44ed94
add some bug numbers to the test suite
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
895 %!assert <54382> (int64 (char (255)), int64 (255)) |
8192
9a0a66f650b1
tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
896 */ |