annotate liboctave/util/oct-inttypes.cc @ 18628:c644ed73c6ce stable

tests missing from previous commit
author John W. Eaton <jwe@octave.org>
date Sat, 05 Apr 2014 15:30:30 -0400
parents 0cd39f7f2409
children 5b6901b06106
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
1 /*
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
2
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 15271
diff changeset
3 Copyright (C) 2004-2013 John W. Eaton
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
4 Copyright (C) 2008-2009 Jaroslav Hajek
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
5
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
7
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6108
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6108
diff changeset
11 option) any later version.
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
12
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
16 for more details.
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
17
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6108
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6108
diff changeset
20 <http://www.gnu.org/licenses/>.
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
21
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
22 */
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
23
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
26 #endif
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
27
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
28 #include <fpucw.h>
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
29
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
30 #include "lo-error.h"
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
31
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
32 #include "oct-inttypes.h"
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
33
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
34 template<class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
35 const octave_int<T> octave_int<T>::zero (static_cast<T> (0));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
36
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
37 template<class T>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
38 const octave_int<T> octave_int<T>::one (static_cast<T> (1));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
39
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
40 // define type names.
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
41 #define DECLARE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
42 template <> \
8319
c374691576f6 Fix for MSVC compilation
Michael Goffioul <michael.goffioul@gmail.com>
parents: 8293
diff changeset
43 OCTAVE_API const char * \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
44 octave_int<TYPE>::type_name () { return TYPENAME; }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
45
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
46 DECLARE_OCTAVE_INT_TYPENAME (int8_t, "int8")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
47 DECLARE_OCTAVE_INT_TYPENAME (int16_t, "int16")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
48 DECLARE_OCTAVE_INT_TYPENAME (int32_t, "int32")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
49 DECLARE_OCTAVE_INT_TYPENAME (int64_t, "int64")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
50 DECLARE_OCTAVE_INT_TYPENAME (uint8_t, "uint8")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
51 DECLARE_OCTAVE_INT_TYPENAME (uint16_t, "uint16")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
52 DECLARE_OCTAVE_INT_TYPENAME (uint32_t, "uint32")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
53 DECLARE_OCTAVE_INT_TYPENAME (uint64_t, "uint64")
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
54
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
55 #ifdef OCTAVE_INT_USE_LONG_DOUBLE
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
56
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
57 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
58
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
59 #define DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
60 template <class xop> \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
61 bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
62 octave_int_cmp_op::external_mop (double x, T y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
63 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
64 DECL_LONG_DOUBLE_ROUNDING \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
65 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
66 BEGIN_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
67 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
68 bool retval = xop::op (static_cast<long double> (x), \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
69 static_cast<long double> (y)); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
70 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
71 END_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
72 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
73 return retval; \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
74 } \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
75 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
76 template <class xop> \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
77 bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
78 octave_int_cmp_op::external_mop (T x, double y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
79 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
80 DECL_LONG_DOUBLE_ROUNDING \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
81 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
82 BEGIN_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
83 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
84 bool retval = xop::op (static_cast<long double> (x), \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
85 static_cast<long double> (y)); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
86 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
87 END_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
88 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
89 return retval; \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
90 }
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
91
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
92 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
93 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
94
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
95 #define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP(OP, T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
96 template OCTAVE_API bool \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
97 octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (double, T); \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
98 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
99 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
100
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
101 #define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
102 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (lt, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
103 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (le, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
104 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (gt, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
105 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ge, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
106 INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (eq, T); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
107 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
108
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
109 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
110 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
111
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
112 uint64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
113 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
114 {
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
115 DECL_LONG_DOUBLE_ROUNDING
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
116
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
117 BEGIN_LONG_DOUBLE_ROUNDING ();
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
118
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
119 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
120
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
121 END_LONG_DOUBLE_ROUNDING ();
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
122
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
123 return retval;
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
124 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
125
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
126 int64_t
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
127 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
128 {
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
129 DECL_LONG_DOUBLE_ROUNDING
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
130
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
131 BEGIN_LONG_DOUBLE_ROUNDING ();
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
132
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
133 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
134
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
135 END_LONG_DOUBLE_ROUNDING ();
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
136
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
137 return retval;
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
138 }
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
139
18005
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
140 // 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
141 // 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
142 // 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
143 // 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
144 // 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
145 // 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
146 // 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
147
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
148 #define OCTAVE_LONG_DOUBLE_OP(T, OP, NAME) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
149 T \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
150 external_double_ ## T ## _ ## NAME (double x, T y) \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
151 { \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
152 DECL_LONG_DOUBLE_ROUNDING \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
153 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
154 BEGIN_LONG_DOUBLE_ROUNDING (); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
155 \
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
156 T retval = T (x OP static_cast<long double> (y.value ())); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
157 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
158 END_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
159 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
160 return retval; \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
161 } \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
162 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
163 T \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
164 external_ ## T ## _double_ ## NAME (T x, double y) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
165 { \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
166 DECL_LONG_DOUBLE_ROUNDING \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
167 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
168 BEGIN_LONG_DOUBLE_ROUNDING (); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
169 \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
170 T retval = T (static_cast<long double> (x.value ()) OP y); \
17993
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 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 END_LONG_DOUBLE_ROUNDING (); \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
173 \
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
174 return retval; \
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
18017
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
177 #define OCTAVE_LONG_DOUBLE_OPS(T) \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
178 OCTAVE_LONG_DOUBLE_OP (T, +, add); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
179 OCTAVE_LONG_DOUBLE_OP (T, -, sub); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
180 OCTAVE_LONG_DOUBLE_OP (T, *, mul); \
0cd39f7f2409 additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 18005
diff changeset
181 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
182
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
183 OCTAVE_LONG_DOUBLE_OPS(octave_int64);
79653c5b6147 make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17993
diff changeset
184 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
185
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
186 #endif
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
187
ac9fd5010620 avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents: 17843
diff changeset
188 #else
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
189
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
190 // Define comparison operators
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
191
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
192 template <class xop>
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
193 bool
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8797
diff changeset
194 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
195 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
196 static const double xxup = std::numeric_limits<uint64_t>::max ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
197 // This converts to the nearest double. Unless there's an equality, the
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
198 // result is clear.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
199 double xx = x;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
200 if (xx != y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
201 return xop::op (xx, y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
202 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
203 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
204 // If equality occured we compare as integers.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
205 if (xx == xxup)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
206 return xop::gtval;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
207 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
208 return xop::op (x, static_cast<uint64_t> (xx));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
209 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
210 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
211
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
212 template <class xop>
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
213 bool
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8797
diff changeset
214 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
215 {
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
216 static const double xxup = std::numeric_limits<int64_t>::max ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
217 static const double xxlo = std::numeric_limits<int64_t>::min ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
218 // This converts to the nearest double. Unless there's an equality, the
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
219 // result is clear.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
220 double xx = x;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
221 if (xx != y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
222 return xop::op (xx, y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
223 else
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 // If equality occured we compare as integers.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
226 if (xx == xxup)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
227 return xop::gtval;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
228 else if (xx == xxlo)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
229 return xop::ltval;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
230 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
231 return xop::op (x, static_cast<int64_t> (xx));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
232 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
233
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
234 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
235
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
236 // We define double-int operations by reverting the operator
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
237
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
238 // A trait class reverting the operator
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
239 template <class xop>
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
240 class rev_op
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
241 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
242 public:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
243 typedef xop op;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
244 };
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
245
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
246 #define DEFINE_REVERTED_OPERATOR(OP1,OP2) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
247 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
248 class rev_op<octave_int_cmp_op::OP1> \
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 public: \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
251 typedef octave_int_cmp_op::OP2 op; \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
252 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
253
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
254 DEFINE_REVERTED_OPERATOR(lt,gt);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
255 DEFINE_REVERTED_OPERATOR(gt,lt);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
256 DEFINE_REVERTED_OPERATOR(le,ge);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
257 DEFINE_REVERTED_OPERATOR(ge,le);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
258
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
259 template <class xop>
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
260 bool
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8797
diff changeset
261 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
262 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
263 typedef typename rev_op<xop>::op rop;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
264 return mop<rop> (y, x);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
265 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
266
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
267 template <class xop>
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
268 bool
8856
ab4db66e286f workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents: 8797
diff changeset
269 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
270 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
271 typedef typename rev_op<xop>::op rop;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
272 return mop<rop> (y, x);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
273 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
274
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
275
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
276 // Define handlers for int64 multiplication
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
277
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
278 template <>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
279 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
280 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
281 {
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
282 // Get upper words
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
283 uint64_t ux = x >> 32, uy = y >> 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
284 uint64_t res;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
285 if (ux)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
286 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
287 if (uy)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
288 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
289 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
290 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
291 uint64_t ly = static_cast<uint32_t> (y), uxly = ux*ly;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
292 if (uxly >> 32)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
293 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
294 uxly <<= 32; // never overflows
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
295 uint64_t lx = static_cast<uint32_t> (x), lxly = lx*ly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
296 res = add (uxly, lxly);
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 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
299 else if (uy)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
300 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
301 uint64_t lx = static_cast<uint32_t> (x), uylx = uy*lx;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
302 if (uylx >> 32)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
303 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
304 uylx <<= 32; // never overflows
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
305 uint64_t ly = static_cast<uint32_t> (y), lylx = ly*lx;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
306 res = add (uylx, lylx);
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 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
309 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
310 uint64_t lx = static_cast<uint32_t> (x);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
311 uint64_t ly = static_cast<uint32_t> (y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
312 res = lx*ly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
313 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
314
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
315 return res;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
316
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
317 overflow:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
318 return max_val ();
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
319 }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
320
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
321 template <>
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
322 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
323 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
324 {
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
325 // The signed case is far worse. The problem is that
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
326 // even if neither integer fits into signed 32-bit range, the result may
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
327 // still be OK. Uh oh.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
328
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
329 // Essentially, what we do is compute sign, multiply absolute values
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
330 // (as above) and impose the sign.
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
331 // FIXME: can we do something faster if we HAVE_FAST_INT_OPS?
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
332
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
333 uint64_t usx = octave_int_abs (x), usy = octave_int_abs (y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
334 bool positive = (x < 0) == (y < 0);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
335
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
336 // Get upper words
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
337 uint64_t ux = usx >> 32, uy = usy >> 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
338 uint64_t res;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
339 if (ux)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
340 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
341 if (uy)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
342 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
343 else
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 uint64_t ly = static_cast<uint32_t> (usy), uxly = ux*ly;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
346 if (uxly >> 32)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
347 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
348 uxly <<= 32; // never overflows
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
349 uint64_t lx = static_cast<uint32_t> (usx), lxly = lx*ly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
350 res = uxly + lxly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
351 if (res < uxly)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
352 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
353 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
354 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
355 else if (uy)
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 uint64_t lx = static_cast<uint32_t> (usx), uylx = uy*lx;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
358 if (uylx >> 32)
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
359 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
360 uylx <<= 32; // never overflows
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
361 uint64_t ly = static_cast<uint32_t> (usy), lylx = ly*lx;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
362 res = uylx + lylx;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
363 if (res < uylx)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
364 goto overflow;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
365 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
366 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
367 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
368 uint64_t lx = static_cast<uint32_t> (usx);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
369 uint64_t ly = static_cast<uint32_t> (usy);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
370 res = lx*ly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
371 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
372
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
373 if (positive)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
374 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
375 if (res > static_cast<uint64_t> (max_val ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
376 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
377 return max_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
378 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
379 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
380 return static_cast<int64_t> (res);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
381 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
382 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
383 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
384 if (res > static_cast<uint64_t> (-min_val ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
385 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
386 return min_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
387 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
388 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
389 return -static_cast<int64_t> (res);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
390 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
391
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
392
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
393 overflow:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
394 return positive ? max_val () : min_val ();
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
395
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
396 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
397
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
398 #define INT_DOUBLE_BINOP_DECL(OP,SUFFIX) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
399 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
400 OCTAVE_API octave_ ## SUFFIX \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
401 operator OP (const octave_ ## SUFFIX & x, const double& y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
402
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
403 #define DOUBLE_INT_BINOP_DECL(OP,SUFFIX) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
404 template <> \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
405 OCTAVE_API octave_ ## SUFFIX \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
406 operator OP (const double& x, const octave_ ## SUFFIX & y)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
407
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
408 INT_DOUBLE_BINOP_DECL (+, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
409 {
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14427
diff changeset
410 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
411 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
412
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
413 DOUBLE_INT_BINOP_DECL (+, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
414 { return y + x; }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
415
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
416 INT_DOUBLE_BINOP_DECL (+, int64)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
417 {
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
418 if (fabs (y) < static_cast<double> (octave_int64::max ()))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
419 return x + octave_int64 (y);
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 // If the number is within the int64 range (the most common case,
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
423 // probably), the above will work as expected. If not, it's more
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
424 // complicated - as long as y is within _twice_ the signed range, the
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
425 // result may still be an integer. An instance of such an operation is
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14427
diff changeset
426 // 3*2**62 + (1+intmin ('int64')) that should yield int64 (2**62) + 1. So
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
427 // what we do is to try to convert y/2 and add it twice. Note that if y/2
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
428 // overflows, the result must overflow as well, and that y/2 cannot be a
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
429 // fractional number.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
430 octave_int64 y2 (y / 2);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
431 return (x + y2) + y2;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
432 }
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
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
435 DOUBLE_INT_BINOP_DECL (+, int64)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
436 {
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
437 return y + x;
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
438 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
439
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
440 INT_DOUBLE_BINOP_DECL (-, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
441 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
442 return x + (-y);
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
443 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
444
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
445 DOUBLE_INT_BINOP_DECL (-, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
446 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
447 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
448 return octave_uint64 (x) - y;
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
449 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
450 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
451 // Again a trick to get the corner cases right. Things like
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14427
diff changeset
452 // 3**2**63 - intmax ('uint64') should produce the correct result, i.e.
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14427
diff changeset
453 // int64 (2**63) + 1.
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
454 const double p2_64 = std::pow (2.0, 64);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
455 if (y.bool_value ())
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
456 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
457 const uint64_t p2_64my = (~y.value ()) + 1; // Equals 2**64 - y
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
458 return octave_uint64 (x - p2_64) + octave_uint64 (p2_64my);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
459 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
460 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
461 return octave_uint64 (p2_64);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
462 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
463 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
464
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
465 INT_DOUBLE_BINOP_DECL (-, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
466 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
467 return x + (-y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
468 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
469
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
470 DOUBLE_INT_BINOP_DECL (-, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
471 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
472 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
473 < -std::numeric_limits<int64_t>::max ());
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
474 // 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
475 // be eliminated at compile time.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
476 if (twosc && y.value () == std::numeric_limits<int64_t>::min ())
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
477 {
15018
3d8ace26c5b4 maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents: 14427
diff changeset
478 return octave_int64 (x + std::pow (2.0, 63));
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
479 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
480 else
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
481 return x + (-y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
482 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
483
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
484 // NOTE:
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
485 // Emulated mixed multiplications are tricky due to possible precision loss.
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
486 // 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
487 // of converting the double number into the form sign * 64-bit integer *
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
488 // 2**exponent, multiply the 64-bit integers to get a 128-bit number, split that
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
489 // number into 32-bit words and form 4 double-valued summands (none of which
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
490 // loses precision), then convert these into integers and sum them. Though it is
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
491 // not immediately obvious, this should work even w.r.t. rounding (none of the
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
492 // summands lose precision).
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
493
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
494 // 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
495 // as four 32-bit words.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
496 static void
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
497 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
498 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
499 uint64_t lx = static_cast<uint32_t> (x), ux = x >> 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
500 uint64_t ly = static_cast<uint32_t> (y), uy = y >> 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
501 uint64_t a = lx * ly;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
502 w[0] = a; a >>= 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
503 uint64_t uxly = ux*ly, uylx = uy*lx;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
504 a += static_cast<uint32_t> (uxly); uxly >>= 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
505 a += static_cast<uint32_t> (uylx); uylx >>= 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
506 w[1] = a; a >>= 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
507 uint64_t uxuy = ux * uy;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
508 a += uxly; a += uylx; a += uxuy;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
509 w[2] = a; a >>= 32;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
510 w[3] = a;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
511 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
512
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
513 // 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
514 static void
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
515 dblesplit (double x, bool& sign, uint64_t& mtis, int& exp)
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 sign = x < 0; x = fabs (x);
17843
573f865aa8c8 use gnulib frexp module
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
518 x = gnulib::frexp (x, &exp);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
519 exp -= 52;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
520 mtis = static_cast<uint64_t> (ldexp (x, 52));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
521 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
522
17769
49a5a4be04a1 maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents: 17744
diff changeset
523 // 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
524 // 32-bit unsigned integer mantissa, exponent, and sign.
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
525 static double
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
526 dbleget (bool sign, uint32_t mtis, int exp)
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 double x = ldexp (static_cast<double> (mtis), exp);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
529 return sign ? -x : x;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
530 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
531
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
532 INT_DOUBLE_BINOP_DECL (*, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
533 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
534 if (y >= 0 && y < octave_uint64::max () && y == xround (y))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
535 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
536 return x * octave_uint64 (static_cast<uint64_t> (y));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
537 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
538 else if (y == 0.5)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
539 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
540 return x / octave_uint64 (static_cast<uint64_t> (2));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
541 }
9013
3b1908b58662 fixes in mixed int64-double multiply emulation
Jaroslav Hajek <highegg@gmail.com>
parents: 9003
diff changeset
542 else if (y < 0 || xisnan (y) || xisinf (y))
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
543 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
544 return octave_uint64 (x.value () * y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
545 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
546 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
547 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
548 bool sign;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
549 uint64_t my;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
550 int e;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
551 dblesplit (y, sign, my, e);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
552 uint32_t w[4];
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
553 umul128 (x.value (), my, w);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
554 octave_uint64 res = octave_uint64::zero;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
555 for (short i = 0; i < 4; i++)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
556 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
557 res += octave_uint64 (dbleget (sign, w[i], e));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
558 e += 32;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
559 }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
560 return res;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
561 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
562 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
563
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
564 DOUBLE_INT_BINOP_DECL (*, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
565 { return y * x; }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
566
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
567 INT_DOUBLE_BINOP_DECL (*, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
568 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
569 if (fabs (y) < octave_int64::max () && y == xround (y))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
570 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
571 return x * octave_int64 (static_cast<int64_t> (y));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
572 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
573 else if (fabs (y) == 0.5)
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 return x / octave_int64 (static_cast<uint64_t> (4*y));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
576 }
9013
3b1908b58662 fixes in mixed int64-double multiply emulation
Jaroslav Hajek <highegg@gmail.com>
parents: 9003
diff changeset
577 else if (xisnan (y) || xisinf (y))
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
578 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
579 return octave_int64 (x.value () * y);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
580 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
581 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
582 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
583 bool sign;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
584 uint64_t my;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
585 int e;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
586 dblesplit (y, sign, my, e);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
587 uint32_t w[4];
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
588 sign = (sign != (x.value () < 0));
8293
ad5bb02d267a workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents: 8202
diff changeset
589 umul128 (octave_int_abs (x.value ()), my, w);
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
590 octave_int64 res = octave_int64::zero;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
591 for (short i = 0; i < 4; i++)
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 res += octave_int64 (dbleget (sign, w[i], e));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
594 e += 32;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
595 }
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
596 return res;
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
597 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
598 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
599
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
600 DOUBLE_INT_BINOP_DECL (*, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
601 { return y * x; }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
602
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
603 DOUBLE_INT_BINOP_DECL (/, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
604 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
605 return octave_uint64 (x / static_cast<double> (y));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
606 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
607
8185
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
608 DOUBLE_INT_BINOP_DECL (/, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
609 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
610 return octave_int64 (x / static_cast<double> (y));
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
611 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
612
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
613 INT_DOUBLE_BINOP_DECL (/, uint64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
614 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
615 if (y >= 0 && y < octave_uint64::max () && y == xround (y))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
616 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
617 return x / octave_uint64 (y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
618 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
619 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
620 return x * (1.0/y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
621 }
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 INT_DOUBLE_BINOP_DECL (/, int64)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
624 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
625 if (fabs (y) < octave_int64::max () && y == xround (y))
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
626 {
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
627 return x / octave_int64 (y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
628 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
629 else
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
630 return x * (1.0/y);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
631 }
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
632
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
633 #define INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP,T1,T2) \
9237
3c1762c7e787 Add missing xxx_API decoration and remove misplaced ones
Michael Goffioul <michael.goffioul@gmail.com>
parents: 9013
diff changeset
634 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
635 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
636
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
637 #define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP) \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
638 INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, double, int64_t); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
639 INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, double, uint64_t); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
640 INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, int64_t, double); \
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
641 INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, uint64_t, double)
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
642
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
643 INSTANTIATE_INT64_DOUBLE_CMP_OP(lt);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
644 INSTANTIATE_INT64_DOUBLE_CMP_OP(le);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
645 INSTANTIATE_INT64_DOUBLE_CMP_OP(gt);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
646 INSTANTIATE_INT64_DOUBLE_CMP_OP(ge);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
647 INSTANTIATE_INT64_DOUBLE_CMP_OP(eq);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
648 INSTANTIATE_INT64_DOUBLE_CMP_OP(ne);
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
649
69c5cce38c29 implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8169
diff changeset
650 #endif
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
651
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
652 //template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
653 //bool
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
654 //xisnan (const octave_int<T>&)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
655 //{
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
656 // return false;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
657 //}
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
658
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
659 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
660 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
661 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
662 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
663 octave_int<T> retval;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
664
8864
4d328b8979c8 simplify integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8856
diff changeset
665 octave_int<T> zero = static_cast<T> (0);
4d328b8979c8 simplify integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8856
diff changeset
666 octave_int<T> one = static_cast<T> (1);
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
667
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
668 if (b == zero || a == one)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
669 retval = one;
8864
4d328b8979c8 simplify integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8856
diff changeset
670 else if (b < zero)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
671 {
8864
4d328b8979c8 simplify integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8856
diff changeset
672 if (a == -one)
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
673 retval = (b.value () % 2) ? a : one;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
674 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
675 retval = zero;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
676 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
677 else
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
678 {
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
679 octave_int<T> a_val = a;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
680 T b_val = b; // no need to do saturation on b
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
681
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
682 retval = a;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
683
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
684 b_val -= 1;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
685
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
686 while (b_val != 0)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
687 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
688 if (b_val & 1)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
689 retval = retval * a_val;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
690
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
691 b_val = b_val >> 1;
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
692
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
693 if (b_val)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
694 a_val = a_val * a_val;
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
695 }
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
696 }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
697
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
698 return retval;
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
699 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
700
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
701 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
702 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
703 pow (const double& a, const octave_int<T>& b)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
704 { return octave_int<T> (pow (a, b.double_value ())); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
705
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
706 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
707 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
708 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
709 {
8195
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
710 return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b))
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
711 ? pow (a, octave_int<T> (static_cast<T> (b)))
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
712 : octave_int<T> (pow (a.double_value (), b)));
8195
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
713 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
714
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
715 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
716 octave_int<T>
13012
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
717 pow (const float& a, const octave_int<T>& b)
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
718 { return octave_int<T> (pow (a, b.float_value ())); }
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
719
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
720 template <class T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
721 octave_int<T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
722 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
723 {
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
724 return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b))
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
725 ? pow (a, octave_int<T> (static_cast<T> (b)))
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
726 : octave_int<T> (pow (a.double_value (), static_cast<double> (b))));
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
727 }
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
728
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
729 // FIXME: Do we really need a differently named single-precision
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
730 // function integer power function here instead of an overloaded
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
731 // one?
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
732 template <class T>
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
733 octave_int<T>
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
734 powf (const float& a, const octave_int<T>& b)
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
735 { return octave_int<T> (pow (a, b.float_value ())); }
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
736
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
737 template <class T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
738 octave_int<T>
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
739 powf (const octave_int<T>& a, const float& b)
8195
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
740 {
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
741 return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b))
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
742 ? pow (a, octave_int<T> (static_cast<T> (b)))
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
743 : octave_int<T> (pow (a.double_value (), static_cast<double> (b))));
8195
ec3a55bd876b fix integer exponentiation
Jaroslav Hajek <highegg@gmail.com>
parents: 8192
diff changeset
744 }
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
745
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
746 #define INSTANTIATE_INTTYPE(T) \
6108
143b556ce725 [project @ 2006-10-27 01:45:54 by jwe]
jwe
parents: 6105
diff changeset
747 template class OCTAVE_API octave_int<T>; \
143b556ce725 [project @ 2006-10-27 01:45:54 by jwe]
jwe
parents: 6105
diff changeset
748 template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const octave_int<T>&); \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
749 template OCTAVE_API octave_int<T> pow (const double&, const octave_int<T>&); \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
750 template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const double&); \
13012
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
751 template OCTAVE_API octave_int<T> pow (const float&, const octave_int<T>&); \
15eefbd9d4e8 Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 11586
diff changeset
752 template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const float&); \
8169
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
753 template OCTAVE_API octave_int<T> powf (const float&, const octave_int<T>&); \
66bc6f9b4f72 rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents: 7521
diff changeset
754 template OCTAVE_API octave_int<T> powf (const octave_int<T>&, const float&); \
6108
143b556ce725 [project @ 2006-10-27 01:45:54 by jwe]
jwe
parents: 6105
diff changeset
755 template OCTAVE_API octave_int<T> \
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
756 bitshift (const octave_int<T>&, int, const octave_int<T>&); \
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
757
5828
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
758 INSTANTIATE_INTTYPE (int8_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
759 INSTANTIATE_INTTYPE (int16_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
760 INSTANTIATE_INTTYPE (int32_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
761 INSTANTIATE_INTTYPE (int64_t);
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
762
5828
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
763 INSTANTIATE_INTTYPE (uint8_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
764 INSTANTIATE_INTTYPE (uint16_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
765 INSTANTIATE_INTTYPE (uint32_t);
22e23bee74c8 [project @ 2006-05-23 06:05:14 by jwe]
jwe
parents: 5307
diff changeset
766 INSTANTIATE_INTTYPE (uint64_t);
5072
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
767
97b62f0c1bee [project @ 2004-11-09 05:51:30 by jwe]
jwe
parents:
diff changeset
768
8192
9a0a66f650b1 tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
769 /*
9a0a66f650b1 tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
770
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
771 %!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
772 %!assert (intmin ("int64") / int64 (-1), intmax ("int64"))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
773 %!assert (int64 (2**63), intmax ("int64"))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
774 %!assert (uint64 (2**64), intmax ("uint64"))
8192
9a0a66f650b1 tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
775 %!test
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
776 %! 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
777 %!test
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
778 %! 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
779 %!test
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
780 %! 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
781 %!test
14427
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
782 %! a = uint64 (2**61) + 2; assert (1.25*a == (5*a)/4);
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
783 %!assert (int32 (2**31+0.5), intmax ("int32"))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
784 %!assert (int32 (-2**31-0.5), intmin ("int32"))
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
785 %!assert ((int64 (2**62)+1)**1, int64 (2**62)+1)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
786 %!assert ((int64 (2**30)+1)**2, int64 (2**60+2**31) + 1)
d07e989686b0 Use Octave coding conventions in liboctave %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
787
18628
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
788 %!assert (uint8 (char (128)), uint8 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
789 %!assert (uint8 (char (255)), uint8 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
790 %!assert (int8 (char (128)), int8 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
791 %!assert (int8 (char (255)), int8 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
792
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
793 %!assert (uint16 (char (128)), uint16 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
794 %!assert (uint16 (char (255)), uint16 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
795 %!assert (int16 (char (128)), int16 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
796 %!assert (int16 (char (255)), int16 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
797
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
798 %!assert (uint32 (char (128)), uint32 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
799 %!assert (uint32 (char (255)), uint32 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
800 %!assert (int32 (char (128)), int32 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
801 %!assert (int32 (char (255)), int32 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
802
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
803 %!assert (uint64 (char (128)), uint64 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
804 %!assert (uint64 (char (255)), uint64 (255));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
805 %!assert (int64 (char (128)), int64 (128));
c644ed73c6ce tests missing from previous commit
John W. Eaton <jwe@octave.org>
parents: 18017
diff changeset
806 %!assert (int64 (char (255)), int64 (255));
8192
9a0a66f650b1 tests for int64 arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents: 8185
diff changeset
807 */