annotate libinterp/operators/op-mi.cc @ 29882:38c47f8f7cd6

allow int64 (-large_int) to work properly again (bug #45945) * op-mi.cc (oct_unop_unsigned_uminus, oct_unop_signed_uminus): Use scalar_ref() and conversion to uint64/int64 instead of uint64_scalar_value/int64_scalar_value to avoid conversion to double before converting to integer value.
author John W. Eaton <jwe@octave.org>
date Wed, 14 Jul 2021 16:20:22 -0400
parents 7854d5752dd2
children b260322f6730
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28588
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 ////////////////////////////////////////////////////////////////////////
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 //
29359
7854d5752dd2 maint: merge stable to default.
John W. Eaton <jwe@octave.org>
parents: 28588
diff changeset
3 // Copyright (C) 2020-2021 The Octave Project Developers
28588
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 // distribution or <https://octave.org/copyright/>.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 // This file is part of Octave.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 // under the terms of the GNU General Public License as published by
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 // (at your option) any later version.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 // GNU General Public License for more details.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 // along with Octave; see the file COPYING. If not, see
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 // <https://www.gnu.org/licenses/>.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 ////////////////////////////////////////////////////////////////////////
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #if defined (HAVE_CONFIG_H)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 # include "config.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #endif
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include <iostream>
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include "errwarn.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #include "ops.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 #include "ov-magic-int.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 #include "ov-typeinfo.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 #include "ov.h"
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 // Magic integer unary ops. Only + and - are allowed so that
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 // expressions like
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 // int64 (-9007199254740994)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 //
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 // produce proper int64 constants.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 static octave_value
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 oct_unop_unsigned_uplus (const octave_base_value& a)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 const octave_magic_uint& v = dynamic_cast<const octave_magic_uint&> (a);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 // no-op.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 // FIXME: but can we do this just by incrementing the reference count?
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 return octave_value (v.clone ());
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 static octave_value
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 oct_unop_unsigned_uminus (const octave_base_value& a)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 const octave_magic_uint& v = dynamic_cast<const octave_magic_uint&> (a);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 // We are storing a uint64 value, so some fakery is needed here.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 // Is there a better way?
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61
29882
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
62 // FIXME: Maybe there should also be octave_magic_int::as_TYPE_value
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
63 // functions?
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
64 octave_uint64 val (v.scalar_ref ());
28588
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 uint64_t ival = val.value ();
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 static const uint64_t max_val
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 = static_cast<uint64_t> (std::numeric_limits<int64_t>::max ());
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 static const uint64_t max_val_p1 = max_val + 1;
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 if (ival <= max_val)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 int64_t signed_ival = ival;
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 return octave_value (new octave_magic_int (-signed_ival));
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 if (ival == max_val_p1)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 // Correctly capture intmin. For example, negating uint8(128)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 // should return int8(-128) but converting directly to int8 and
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 // negating will not return the correct result.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 static const int64_t min_signed_ival
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 = std::numeric_limits<int64_t>::min ();
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 return octave_value (new octave_magic_int (min_signed_ival));
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 return octave_value (-static_cast<double> (ival));
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 static octave_value
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 oct_unop_signed_uplus (const octave_base_value& a)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 const octave_magic_int& v = dynamic_cast<const octave_magic_int&> (a);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 // no-op.
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 // FIXME: but can we do this just by incrementing the reference count?
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 return octave_value (v.clone ());
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 static octave_value
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 oct_unop_signed_uminus (const octave_base_value& a)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 const octave_magic_int& v = dynamic_cast<const octave_magic_int&> (a);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107
29882
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
108 // FIXME: Maybe there should also be octave_magic_int::as_TYPE_value
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
109 // functions?
38c47f8f7cd6 allow int64 (-large_int) to work properly again (bug #45945)
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
110 octave_int64 val (v.scalar_ref ());
28588
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 return octave_value (new octave_magic_int (-val));
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 }
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 void
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 install_mi_ops (octave::type_info& ti)
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117 {
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 INSTALL_UNOP_TI (ti, op_uplus, octave_magic_uint, unsigned_uplus);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 INSTALL_UNOP_TI (ti, op_uminus, octave_magic_uint, unsigned_uminus);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 INSTALL_UNOP_TI (ti, op_uplus, octave_magic_int, signed_uplus);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 INSTALL_UNOP_TI (ti, op_uminus, octave_magic_int, signed_uminus);
ee9b1081471f allow integer constants > flintmax to be represented exactly (bug #45945)
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123 }