Mercurial > octave
annotate liboctave/util/oct-inttypes.h @ 17822:ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
* libgui/src/dialog.h, libgui/src/find-files-dialog.h,
libgui/src/find-files-model.h, libgui/src/m-editor/file-editor-tab.h,
libgui/src/m-editor/octave-qscintilla.h, libgui/src/main-window.h,
libgui/src/octave-dock-widget.h, libgui/src/workspace-model.h,
libgui/src/workspace-view.h, libinterp/corefcn/Cell.h,
libinterp/corefcn/gl-render.h, libinterp/corefcn/gl2ps-renderer.h,
libinterp/corefcn/graphics.in.h, libinterp/corefcn/oct-errno.h,
libinterp/corefcn/oct-fstrm.h, libinterp/corefcn/oct-handle.h,
libinterp/corefcn/oct-hdf5.h, libinterp/corefcn/oct-hist.h,
libinterp/corefcn/oct-iostrm.h, libinterp/corefcn/oct-lvalue.h,
libinterp/corefcn/oct-prcstrm.h, libinterp/corefcn/oct-procbuf.h,
libinterp/corefcn/oct-stdstrm.h, libinterp/corefcn/oct-stream.h,
libinterp/corefcn/oct-strstrm.h, libinterp/corefcn/octave-link.h,
libinterp/corefcn/pt-jit.h, libinterp/octave-value/ov-base-diag.h,
libinterp/octave-value/ov-base-int.h, libinterp/octave-value/ov-base-mat.h,
libinterp/octave-value/ov-base-scalar.h,
libinterp/octave-value/ov-base-sparse.h, libinterp/octave-value/ov-base.h,
libinterp/octave-value/ov-bool-mat.h, libinterp/octave-value/ov-bool-sparse.h,
libinterp/octave-value/ov-bool.h, libinterp/octave-value/ov-builtin.h,
libinterp/octave-value/ov-cell.h, libinterp/octave-value/ov-ch-mat.h,
libinterp/octave-value/ov-class.h, libinterp/octave-value/ov-colon.h,
libinterp/octave-value/ov-complex.h, libinterp/octave-value/ov-cs-list.h,
libinterp/octave-value/ov-cx-diag.h, libinterp/octave-value/ov-cx-mat.h,
libinterp/octave-value/ov-cx-sparse.h, libinterp/octave-value/ov-dld-fcn.h,
libinterp/octave-value/ov-fcn-handle.h, libinterp/octave-value/ov-fcn-inline.h,
libinterp/octave-value/ov-fcn.h, libinterp/octave-value/ov-float.h,
libinterp/octave-value/ov-flt-complex.h,
libinterp/octave-value/ov-flt-cx-diag.h,
libinterp/octave-value/ov-flt-cx-mat.h,
libinterp/octave-value/ov-flt-re-diag.h,
libinterp/octave-value/ov-flt-re-mat.h, libinterp/octave-value/ov-int-traits.h,
libinterp/octave-value/ov-int16.h, libinterp/octave-value/ov-int32.h,
libinterp/octave-value/ov-int64.h, libinterp/octave-value/ov-int8.h,
libinterp/octave-value/ov-java.h, libinterp/octave-value/ov-lazy-idx.h,
libinterp/octave-value/ov-mex-fcn.h, libinterp/octave-value/ov-null-mat.h,
libinterp/octave-value/ov-perm.h, libinterp/octave-value/ov-range.h,
libinterp/octave-value/ov-re-diag.h, libinterp/octave-value/ov-re-mat.h,
libinterp/octave-value/ov-re-sparse.h, libinterp/octave-value/ov-scalar.h,
libinterp/octave-value/ov-str-mat.h, libinterp/octave-value/ov-struct.h,
libinterp/octave-value/ov-typeinfo.h, libinterp/octave-value/ov-uint16.h,
libinterp/octave-value/ov-uint32.h, libinterp/octave-value/ov-uint64.h,
libinterp/octave-value/ov-uint8.h, libinterp/octave-value/ov-usr-fcn.h,
libinterp/octave-value/ov.h, libinterp/parse-tree/pt-all.h,
libinterp/parse-tree/pt-arg-list.h, libinterp/parse-tree/pt-array-list.h,
libinterp/parse-tree/pt-assign.h, libinterp/parse-tree/pt-binop.h,
libinterp/parse-tree/pt-bp.h, libinterp/parse-tree/pt-cbinop.h,
libinterp/parse-tree/pt-cell.h, libinterp/parse-tree/pt-check.h,
libinterp/parse-tree/pt-cmd.h, libinterp/parse-tree/pt-colon.h,
libinterp/parse-tree/pt-const.h, libinterp/parse-tree/pt-decl.h,
libinterp/parse-tree/pt-eval.h, libinterp/parse-tree/pt-except.h,
libinterp/parse-tree/pt-exp.h, libinterp/parse-tree/pt-fcn-handle.h,
libinterp/parse-tree/pt-id.h, libinterp/parse-tree/pt-idx.h,
libinterp/parse-tree/pt-jump.h, libinterp/parse-tree/pt-loop.h,
libinterp/parse-tree/pt-mat.h, libinterp/parse-tree/pt-misc.h,
libinterp/parse-tree/pt-pr-code.h, libinterp/parse-tree/pt-select.h,
libinterp/parse-tree/pt-stmt.h, libinterp/parse-tree/pt-unop.h,
libinterp/parse-tree/pt-walk.h, libinterp/parse-tree/pt.h,
liboctave/array/CColVector.h, liboctave/array/CDiagMatrix.h,
liboctave/array/CMatrix.h, liboctave/array/CNDArray.h,
liboctave/array/CRowVector.h, liboctave/array/boolMatrix.h,
liboctave/array/chMatrix.h, liboctave/array/chNDArray.h,
liboctave/array/dColVector.h, liboctave/array/dDiagMatrix.h,
liboctave/array/dMatrix.h, liboctave/array/dNDArray.h,
liboctave/array/dRowVector.h, liboctave/array/fCColVector.h,
liboctave/array/fCDiagMatrix.h, liboctave/array/fCMatrix.h,
liboctave/array/fCNDArray.h, liboctave/array/fCRowVector.h,
liboctave/array/fColVector.h, liboctave/array/fDiagMatrix.h,
liboctave/array/fMatrix.h, liboctave/array/fNDArray.h,
liboctave/array/fRowVector.h, liboctave/cruft/misc/lo-error.h,
liboctave/numeric/CmplxAEPBAL.h, liboctave/numeric/CmplxCHOL.h,
liboctave/numeric/CmplxGEPBAL.h, liboctave/numeric/CmplxHESS.h,
liboctave/numeric/CmplxLU.h, liboctave/numeric/CmplxQR.h,
liboctave/numeric/CmplxQRP.h, liboctave/numeric/CmplxSCHUR.h,
liboctave/numeric/CmplxSVD.h, liboctave/numeric/Quad.h,
liboctave/numeric/SparseCmplxCHOL.h, liboctave/numeric/SparseCmplxLU.h,
liboctave/numeric/SparseCmplxQR.h, liboctave/numeric/SparseQR.h,
liboctave/numeric/SparsedbleCHOL.h, liboctave/numeric/SparsedbleLU.h,
liboctave/numeric/bsxfun.h, liboctave/numeric/dbleAEPBAL.h,
liboctave/numeric/dbleCHOL.h, liboctave/numeric/dbleGEPBAL.h,
liboctave/numeric/dbleHESS.h, liboctave/numeric/dbleLU.h,
liboctave/numeric/dbleQR.h, liboctave/numeric/dbleQRP.h,
liboctave/numeric/dbleSCHUR.h, liboctave/numeric/dbleSVD.h,
liboctave/numeric/fCmplxAEPBAL.h, liboctave/numeric/fCmplxCHOL.h,
liboctave/numeric/fCmplxGEPBAL.h, liboctave/numeric/fCmplxHESS.h,
liboctave/numeric/fCmplxLU.h, liboctave/numeric/fCmplxQR.h,
liboctave/numeric/fCmplxQRP.h, liboctave/numeric/fCmplxSCHUR.h,
liboctave/numeric/fCmplxSVD.h, liboctave/numeric/fEIG.h,
liboctave/numeric/floatAEPBAL.h, liboctave/numeric/floatCHOL.h,
liboctave/numeric/floatGEPBAL.h, liboctave/numeric/floatHESS.h,
liboctave/numeric/floatLU.h, liboctave/numeric/floatQR.h,
liboctave/numeric/floatQRP.h, liboctave/numeric/floatSCHUR.h,
liboctave/numeric/floatSVD.h, liboctave/numeric/lo-mappers.h,
liboctave/numeric/lo-specfun.h, liboctave/numeric/oct-convn.h,
liboctave/numeric/oct-norm.h, liboctave/numeric/oct-rand.h,
liboctave/operators/Sparse-diag-op-defs.h,
liboctave/operators/Sparse-op-defs.h,
liboctave/operators/Sparse-perm-op-defs.h, liboctave/system/lo-sysdep.h,
liboctave/system/oct-env.h, liboctave/system/oct-group.h,
liboctave/system/oct-openmp.h, liboctave/system/oct-passwd.h,
liboctave/system/oct-syscalls.h, liboctave/system/oct-time.h,
liboctave/system/oct-uname.h, liboctave/util/lo-array-gripes.h,
liboctave/util/lo-cutils.h, liboctave/util/lo-ieee.h,
liboctave/util/lo-macros.h, liboctave/util/lo-math.h,
liboctave/util/lo-regexp.h, liboctave/util/lo-traits.h,
liboctave/util/lo-utils.h, liboctave/util/oct-base64.h,
liboctave/util/oct-binmap.h, liboctave/util/oct-glob.h,
liboctave/util/oct-inttypes.h, liboctave/util/oct-locbuf.h,
liboctave/util/oct-mem.h, liboctave/util/oct-mutex.h,
liboctave/util/oct-refcount.h, liboctave/util/oct-rl-edit.h,
liboctave/util/oct-rl-hist.h, liboctave/util/oct-shlib.h,
liboctave/util/oct-sort.h, liboctave/util/oct-sparse.h:
Use common #define syntax "octave_filename_h" in h_files.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 31 Oct 2013 22:19:07 -0700 |
parents | 49a5a4be04a1 |
children | 824c05a6d3ec |
rev | line source |
---|---|
4902 | 1 /* |
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 |
11523 | 4 Copyright (C) 2008-2009 Jaroslav Hajek |
4902 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4902 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4902 | 21 |
22 */ | |
23 | |
17822
ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
24 #if !defined (octave_oct_inttypes_h) |
ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
25 #define octave_oct_inttypes_h 1 |
4902 | 26 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
27 #include <cstdlib> |
6482 | 28 |
4902 | 29 #include <limits> |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
30 #include <iosfwd> |
4902 | 31 |
8731
5abe5ae55465
use lo-traits in oct-inttypes
Jaroslav Hajek <highegg@gmail.com>
parents:
8333
diff
changeset
|
32 #include "lo-traits.h" |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
33 #include "lo-math.h" |
4969 | 34 #include "lo-mappers.h" |
4902 | 35 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
36 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
37 inline long double xround (long double x) { return roundl (x); } |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 inline long double xisnan (long double x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
39 { return xisnan (static_cast<double> (x)); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
40 #endif |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
41 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 // FIXME: we define this by our own because some compilers, such as |
8333 | 43 // MSVC, do not provide std::abs (int64_t) and std::abs (uint64_t). In |
44 // the future, it should go away in favor of std::abs. | |
8293
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
45 template <class T> |
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
46 inline T octave_int_abs (T x) { return x >= 0 ? x : -x; } |
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
47 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
48 // Query for an integer type of certain sizeof, and signedness. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
49 template<int qsize, bool qsigned> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
50 struct query_integer_type |
4902 | 51 { |
52 public: | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
53 static const bool registered = false; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 typedef void type; // Void shall result in a compile-time error if we |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
55 // attempt to use it in computations. |
4902 | 56 }; |
57 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
58 #define REGISTER_INT_TYPE(TYPE) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
59 template <> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
60 class query_integer_type<sizeof (TYPE), std::numeric_limits<TYPE>::is_signed> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
61 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
62 public: \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
63 static const bool registered = true; \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
64 typedef TYPE type; \ |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
65 } |
7596
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
66 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
67 // No two registered integers can share sizeof and signedness. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
68 REGISTER_INT_TYPE (int8_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
69 REGISTER_INT_TYPE (uint8_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
70 REGISTER_INT_TYPE (int16_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
71 REGISTER_INT_TYPE (uint16_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
72 REGISTER_INT_TYPE (int32_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
73 REGISTER_INT_TYPE (uint32_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
74 REGISTER_INT_TYPE (int64_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
75 REGISTER_INT_TYPE (uint64_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
76 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
77 // Rationale: Comparators have a single static method, rel(), that returns the |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
78 // result of the binary relation. They also have two static boolean fields: |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
79 // ltval, gtval determine the value of x OP y if x < y, x > y, respectively. |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
80 #define REGISTER_OCTAVE_CMP_OP(NM,OP) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
81 class NM \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
82 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
83 public: \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
84 static const bool ltval = (0 OP 1), gtval = (1 OP 0); \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
85 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
86 static bool op (T x, T y) { return x OP y; } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
87 } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
88 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
89 // We also provide two special relations: ct, yielding always true, and cf, |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
90 // yielding always false. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
91 #define REGISTER_OCTAVE_CONST_OP(NM,value) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
92 class NM \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
93 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
94 public: \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
95 static const bool ltval = value, gtval = value; \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
96 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
97 static bool op (T, T) { return value; } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
98 } |
4943 | 99 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
100 // Handles non-homogeneous integer comparisons. Avoids doing useless tests. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
101 class octave_int_cmp_op |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
102 { |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
103 // This determines a suitable promotion type for T1 when meeting T2 in a |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
104 // binary relation. If promotion to int or T2 is safe, it is used. Otherwise, |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
105 // the signedness of T1 is preserved and it is widened if T2 is wider. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
106 // Notice that if this is applied to both types, they must end up with equal |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
107 // size. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
108 template <class T1, class T2> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
109 class prom |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 // Promote to int? |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 static const bool pint = (sizeof (T1) < sizeof (int) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 && sizeof (T2) < sizeof (int)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 static const bool t1sig = std::numeric_limits<T1>::is_signed; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 static const bool t2sig = std::numeric_limits<T2>::is_signed; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 static const bool psig = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 (pint || (sizeof (T2) > sizeof (T1) && t2sig) || t1sig); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 static const int psize = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
119 (pint ? sizeof (int) : (sizeof (T2) > sizeof (T1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
120 ? sizeof (T2) : sizeof (T1))); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 public: |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
122 typedef typename query_integer_type<psize, psig>::type type; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 }; |
4943 | 124 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
125 // Implements comparisons between two types of equal size but |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
126 // possibly different signedness. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
127 template<class xop, int size> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
128 class uiop |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 typedef typename query_integer_type<size, false>::type utype; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 typedef typename query_integer_type<size, true>::type stype; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
132 public: |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 static bool op (utype x, utype y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 { return xop::op (x, y); } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
135 static bool op (stype x, stype y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
136 { return xop::op (x, y); } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 static bool op (stype x, utype y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 { return (x < 0) ? xop::ltval : xop::op (static_cast<utype> (x), y); } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
139 static bool op (utype x, stype y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
140 { return (y < 0) ? xop::gtval : xop::op (x, static_cast<utype> (y)); } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
141 }; |
4943 | 142 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
143 public: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
144 REGISTER_OCTAVE_CMP_OP (lt, <); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
145 REGISTER_OCTAVE_CMP_OP (le, <=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
146 REGISTER_OCTAVE_CMP_OP (gt, >); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
147 REGISTER_OCTAVE_CMP_OP (ge, >=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
148 REGISTER_OCTAVE_CMP_OP (eq, ==); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
149 REGISTER_OCTAVE_CMP_OP (ne, !=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
150 REGISTER_OCTAVE_CONST_OP (ct, true); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
151 REGISTER_OCTAVE_CONST_OP (cf, false); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
152 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
153 // Universal comparison operation. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
154 template<class xop, class T1, class T2> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
155 static bool |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
156 op (T1 x, T2 y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
157 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
158 typedef typename prom<T1, T2>::type PT1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
159 typedef typename prom<T2, T1>::type PT2; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
160 return uiop<xop, sizeof (PT1)>::op (static_cast<PT1> (x), |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
161 static_cast<PT2> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
162 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
163 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
164 public: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
165 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
166 // Mixed comparisons |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
167 template <class xop, class T> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
168 static bool |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
169 mop (T x, double y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
170 { return xop::op (static_cast<double> (x), y); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
171 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
172 template <class xop, class T> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
173 static bool |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
174 mop (double x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
175 { return xop::op (x, static_cast<double> (y)); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
176 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
177 // Typecasting to doubles won't work properly for 64-bit integers -- |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
178 // they lose precision. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
179 // If we have long doubles, use them... |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
180 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
181 #define DEFINE_LONG_DOUBLE_CMP_OP(T1, T2) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
182 template <class xop> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
183 static bool \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
184 mop (T1 x, T2 y) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
185 { \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
186 return xop::op (static_cast<long double> (x), \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
187 static_cast<long double> (y)); \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
188 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
189 #else |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
190 // ... otherwise, use external handlers |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
191 |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
192 // FIXME: We could declare directly the mop methods as external, |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
193 // but we can't do this because bugs in gcc (<= 4.3) prevent |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
194 // explicit instantiations later in that case. |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
195 #define DEFINE_LONG_DOUBLE_CMP_OP(T1, T2) \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
196 template <class xop> static OCTAVE_API bool \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
197 emulate_mop (T1, T2); \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
198 template <class xop> \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
199 static bool \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
200 mop (T1 x, T2 y) \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
201 { \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
202 return emulate_mop<xop> (x, y); \ |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
203 } |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
204 #endif |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
205 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
206 DEFINE_LONG_DOUBLE_CMP_OP(double, uint64_t) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
207 DEFINE_LONG_DOUBLE_CMP_OP(double, int64_t) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
208 DEFINE_LONG_DOUBLE_CMP_OP(int64_t, double) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
209 DEFINE_LONG_DOUBLE_CMP_OP(uint64_t, double) |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
210 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
211 #undef DEFINE_LONG_DOUBLE_CMP_OP |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
212 }; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
213 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
214 // Base integer class. No data, just conversion methods and exception flags. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
215 template <class T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
216 class octave_int_base |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
217 { |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
218 protected: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
219 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
220 static T min_val () { return std::numeric_limits<T>:: min (); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
221 static T max_val () { return std::numeric_limits<T>:: max (); } |
4943 | 222 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
223 public: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
224 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
225 // Convert integer value. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
226 template <class S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
227 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
228 truncate_int (const S& value) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
229 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
230 // An exhaustive test whether the max and/or min check can be omitted. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
231 static const bool t_is_signed = std::numeric_limits<T>::is_signed; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
232 static const bool s_is_signed = std::numeric_limits<S>::is_signed; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
233 static const int t_size = sizeof (T), s_size = sizeof (S); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
234 static const bool omit_chk_min = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
235 (! s_is_signed || (t_is_signed && t_size >= s_size)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
236 static const bool omit_chk_max = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
237 (t_size > s_size || (t_size == s_size |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
238 && (! t_is_signed || s_is_signed))); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
239 // If the check can be omitted, substitute constant false relation. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
240 typedef octave_int_cmp_op::cf cf; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
241 typedef octave_int_cmp_op::lt lt; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
242 typedef octave_int_cmp_op::gt gt; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
243 typedef typename if_then_else<omit_chk_min, cf, lt>::result chk_min; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
244 typedef typename if_then_else<omit_chk_max, cf, gt>::result chk_max; |
4943 | 245 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
246 // Efficiency of the following depends on inlining and dead code |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
247 // elimination, but that should be a piece of cake for most compilers. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
248 if (chk_min::op (value, static_cast<S> (min_val ()))) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
249 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
250 return min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
251 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
252 else if (chk_max::op (value, static_cast<S> (max_val ()))) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
253 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
254 return max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
255 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
256 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
257 return static_cast<T> (value); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
258 } |
4943 | 259 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
260 private: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
261 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
262 // Computes a real-valued threshold for a max/min check. |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
263 template <class S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
264 static S |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
265 compute_threshold (S val, T orig_val) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
266 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
267 val = xround (val); // Fool optimizations (maybe redundant) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
268 // If val is even, but orig_val is odd, we're one unit off. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
269 if (orig_val % 2 && val / 2 == xround (val / 2)) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
270 // FIXME: is this always correct? |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
271 val *= (static_cast<S>(1) - (std::numeric_limits<S>::epsilon () / 2)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
272 return val; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
273 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
274 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
275 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
276 // Convert a real number (check NaN and non-int). |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
277 template <class S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
278 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
279 convert_real (const S& value) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
280 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
281 // Compute proper thresholds. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
282 static const S thmin = compute_threshold (static_cast<S> (min_val ()), |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
283 min_val ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
284 static const S thmax = compute_threshold (static_cast<S> (max_val ()), |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
285 max_val ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
286 if (xisnan (value)) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
287 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
288 return static_cast<T> (0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
289 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
290 else if (value < thmin) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
291 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
292 return min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
293 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
294 else if (value > thmax) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
295 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
296 return max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
297 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
298 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
299 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
300 S rvalue = xround (value); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
301 return static_cast<T> (rvalue); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
302 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
303 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
304 }; |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
305 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
306 // Saturated (homogeneous) integer arithmetics. The signed and unsigned |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
307 // implementations are significantly different, so we implement another layer |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
308 // and completely specialize. Arithmetics inherits from octave_int_base so that |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
309 // it can use its exceptions and truncation functions. |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
310 |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
311 template <class T, bool is_signed> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
312 class octave_int_arith_base |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
313 { }; |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
314 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
315 // Unsigned arithmetics. C++ standard requires it to be modular, so the |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
316 // overflows can be handled efficiently and reliably. |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
317 template <class T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
318 class octave_int_arith_base<T, false> : octave_int_base<T> |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
319 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
320 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
321 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
322 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
323 abs (T x) { return x; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
324 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
325 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
326 signum (T x) { return x ? static_cast<T> (1) : static_cast<T> (0); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
327 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
328 // Shifts do not overflow. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
329 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
330 rshift (T x, int n) { return x >> n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
331 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
332 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
333 lshift (T x, int n) { return x << n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
334 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
335 static T |
10405
cc69a17ec801
remove integer math warnings
Jaroslav Hajek <highegg@gmail.com>
parents:
10346
diff
changeset
|
336 minus (T) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
337 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
338 return static_cast<T> (0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
339 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
340 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
341 // the overflow behaviour for unsigned integers is guaranteed by C/C++, |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
342 // so the following should always work. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
343 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
344 add (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
345 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
346 T u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
347 if (u < x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
348 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
349 u = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
350 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
351 return u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
352 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
353 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
354 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
355 sub (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
356 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
357 T u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
358 if (u > x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
359 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
360 u = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
361 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
362 return u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
363 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
364 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
365 // Multiplication is done using promotion to wider integer type. If there is |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
366 // no suitable promotion type, this operation *MUST* be specialized. |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
367 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
368 mul (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
369 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
370 // Promotion type for multiplication (if exists). |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
371 typedef typename query_integer_type<2*sizeof (T), false>::type mptype; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
372 return octave_int_base<T>::truncate_int (static_cast<mptype> (x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
373 * static_cast<mptype> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
374 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
375 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
376 // Division with rounding to nearest. Note that / and % are probably |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
377 // computed by a single instruction. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
378 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
379 div (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
380 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
381 if (y != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
382 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
383 T z = x / y, w = x % y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
384 if (w >= y-w) z += 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
385 return z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
386 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
387 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
388 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
389 return x ? octave_int_base<T>::max_val () : 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
390 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
391 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
392 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
393 // Remainder. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
394 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
395 rem (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
396 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
397 return y != 0 ? x % y : 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
398 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
399 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
400 // Modulus. Note the weird y = 0 case for Matlab compatibility. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
401 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
402 mod (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
403 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
404 return y != 0 ? x % y : x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
405 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
406 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
407 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
408 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
409 // Handle 64-bit multiply using long double |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
410 template <> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
411 inline uint64_t |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
412 octave_int_arith_base<uint64_t, false>:: mul (uint64_t x, uint64_t y) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
413 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
414 long double p = static_cast<long double> (x) * static_cast<long double> (y); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
415 if (p > static_cast<long double> (octave_int_base<uint64_t>::max_val ())) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
416 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
417 return octave_int_base<uint64_t>::max_val (); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
418 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
419 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
420 return static_cast<uint64_t> (p); |
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 #else |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
423 // Special handler for 64-bit integer multiply. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
424 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
425 OCTAVE_API uint64_t |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
426 octave_int_arith_base<uint64_t, false>::mul (uint64_t, uint64_t); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
427 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
428 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
429 // Signed integer arithmetics. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
430 // Rationale: If HAVE_FAST_INT_OPS is defined, the following conditions |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
431 // should hold: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
432 // 1. Signed numbers are represented by twos complement |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
433 // (see <http://en.wikipedia.org/wiki/Two%27s_complement>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
434 // 2. static_cast to unsigned int counterpart works like interpreting |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
435 // the signed bit pattern as unsigned (and is thus zero-cost). |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
436 // 3. Signed addition and subtraction yield the same bit results as unsigned. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
437 // (We use casts to prevent optimization interference, so there is no |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
438 // need for things like -ftrapv). |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
439 // 4. Bit operations on signed integers work like on unsigned integers, |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
440 // except for the shifts. Shifts are arithmetic. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
441 // |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
442 // The above conditions are satisfied by most modern platforms. If |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
443 // HAVE_FAST_INT_OPS is defined, bit tricks and wraparound arithmetics are used |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
444 // to avoid conditional jumps as much as possible, thus being friendly to |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
445 // modern pipeline processor architectures. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
446 // Otherwise, we fall back to a bullet-proof code that only uses assumptions |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
447 // guaranteed by the standard. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
448 |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
449 template <class T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
450 class octave_int_arith_base<T, true> : octave_int_base<T> |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
451 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
452 // The corresponding unsigned type. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
453 typedef typename query_integer_type<sizeof (T), false>::type UT; |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
454 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
455 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
456 // Returns 1 for negative number, 0 otherwise. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
457 static T |
13734
28ddb3150943
Rename signbit to __signbit, until the problem is fixed in gnulib.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
13012
diff
changeset
|
458 __signbit (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
459 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
460 #ifdef HAVE_FAST_INT_OPS |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
461 return static_cast<UT> (x) >> std::numeric_limits<T>::digits; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
462 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
463 return (x < 0) ? 1 : 0; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
464 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
465 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
466 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
467 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
468 abs (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
469 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
470 #ifdef HAVE_FAST_INT_OPS |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
471 // This is close to how GCC does std::abs, but we can't just use std::abs, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
472 // because its behaviour for INT_MIN is undefined and the compiler could |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
473 // discard the following test. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
474 T m = x >> std::numeric_limits<T>::digits; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
475 T y = (x ^ m) - m; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
476 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
477 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
478 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
479 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
480 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
481 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
482 // -INT_MAX is safe because C++ actually allows only three implementations |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
483 // of integers: sign & magnitude, ones complement and twos complement. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
484 // The first test will, with modest optimizations, evaluate at compile |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
485 // time, and maybe eliminate the branch completely. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
486 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
487 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val () |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
488 && x == octave_int_base<T>::min_val ()) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
489 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
490 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
491 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
492 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
493 y = (x < 0) ? -x : x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
494 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
495 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
496 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
497 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
498 static T |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
499 signum (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
500 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
501 // With modest optimizations, this will compile without a jump. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
502 return ((x > 0) ? 1 : 0) - __signbit (x); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
503 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
504 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
505 // FIXME: we do not have an authority what signed shifts should |
8333 | 506 // exactly do, so we define them the easy way. Note that Matlab does |
507 // not define signed shifts. | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
508 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
509 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
510 rshift (T x, int n) { return x >> n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
511 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
512 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
513 lshift (T x, int n) { return x << n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
514 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
515 // Minus has problems similar to abs. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
516 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
517 minus (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
518 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
519 #ifdef HAVE_FAST_INT_OPS |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
520 T y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
521 if (y == octave_int_base<T>::min_val ()) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
522 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
523 --y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
524 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
525 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
526 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
527 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
528 if (octave_int_base<T>::min_val () < -octave_int_base<T>::max_val () |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
529 && x == octave_int_base<T>::min_val ()) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
530 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
531 y = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
532 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
533 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
534 y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
535 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
536 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
537 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
538 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
539 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
540 add (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
541 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
542 #ifdef HAVE_FAST_INT_OPS |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
543 // The typecasts do nothing, but they are here to prevent an optimizing |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
544 // compiler from interfering. Also, the signed operations on small types |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
545 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
546 T u = static_cast<UT> (x) + static_cast<UT> (y); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
547 T ux = u ^ x, uy = u ^ y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
548 if ((ux & uy) < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
549 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 u = octave_int_base<T>::max_val () + __signbit (~u); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
551 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
552 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
553 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
554 // We shall carefully avoid anything that may overflow. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
555 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
556 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
557 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
558 if (x < octave_int_base<T>::min_val () - y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
559 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
560 u = octave_int_base<T>::min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
561 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
562 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
563 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
564 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
565 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
566 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
567 if (x > octave_int_base<T>::max_val () - y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
568 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
569 u = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
570 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
571 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
572 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
573 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
574 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
575 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
576 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
577 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
578 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
579 // This is very similar to addition. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
580 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
581 sub (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
582 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
583 #ifdef HAVE_FAST_INT_OPS |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
584 // The typecasts do nothing, but they are here to prevent an optimizing |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
585 // compiler from interfering. Also, the signed operations on small types |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
586 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
587 T u = static_cast<UT> (x) - static_cast<UT> (y); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
588 T ux = u ^ x, uy = u ^ ~y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
589 if ((ux & uy) < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
590 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
591 u = octave_int_base<T>::max_val () + __signbit (~u); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
592 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
593 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
594 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
595 // We shall carefully avoid anything that may overflow. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
596 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
597 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
598 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
599 if (x > octave_int_base<T>::max_val () + y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
600 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
601 u = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
602 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
603 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
604 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
605 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
606 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
607 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
608 if (x < octave_int_base<T>::min_val () + y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
609 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
610 u = octave_int_base<T>::min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
611 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
612 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
613 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
614 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
615 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
616 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
617 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
618 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
619 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
620 // Multiplication is done using promotion to wider integer type. If there is |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
621 // no suitable promotion type, this operation *MUST* be specialized. |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
622 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
623 mul (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
624 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
625 // Promotion type for multiplication (if exists). |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
626 typedef typename query_integer_type<2*sizeof (T), true>::type mptype; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
627 return octave_int_base<T>::truncate_int (static_cast<mptype> (x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
628 * static_cast<mptype> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
629 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
630 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
631 // Division. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
632 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
633 div (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
634 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
635 T z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
636 if (y == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
637 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
638 if (x < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
639 z = octave_int_base<T>::min_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
640 else if (x != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
641 z = octave_int_base<T>::max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
642 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
643 z = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
644 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
645 else if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
646 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
647 // This is a special case that overflows as well. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
648 if (y == -1 && x == octave_int_base<T>::min_val ()) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
649 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
650 z = octave_int_base<T>::max_val (); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
651 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
652 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
653 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
654 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
655 // Can't overflow, but std::abs (x) can! |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
656 T w = -octave_int_abs (x % y); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
657 if (w <= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
658 z -= 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
659 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
660 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
661 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
662 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
663 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
664 // FIXME: this is a workaround due to MSVC's absence of |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
665 // std::abs (int64_t). The call to octave_int_abs can't |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
666 // overflow, but std::abs (x) can! |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
667 T w = octave_int_abs (x % y); |
8333 | 668 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
669 if (w >= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
670 z += 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
671 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
672 return z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
673 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
674 |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
675 // Remainder. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
676 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
677 rem (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
678 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
679 return y != 0 ? x % y : 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
680 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
681 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
682 // Modulus. Note the weird y = 0 case for Matlab compatibility. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
683 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
684 mod (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
685 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
686 if (y != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
687 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
688 T r = x % y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
689 return ((r < 0) != (y < 0)) ? r + y : r; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
690 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
691 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
692 return x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
693 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
694 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
695 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
696 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
697 // Handle 64-bit multiply using long double |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
698 template <> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
699 inline int64_t |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
700 octave_int_arith_base<int64_t, true>:: mul (int64_t x, int64_t y) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
701 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
702 long double p = static_cast<long double> (x) * static_cast<long double> (y); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
703 // NOTE: We could maybe do it with a single branch if HAVE_FAST_INT_OPS, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
704 // but it would require one more runtime conversion, so the question is |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
705 // whether it would really be faster. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
706 if (p > static_cast<long double> (octave_int_base<int64_t>::max_val ())) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
707 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
708 return octave_int_base<int64_t>::max_val (); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
709 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
710 else if (p < static_cast<long double> (octave_int_base<int64_t>::min_val ())) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
711 { |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
712 return octave_int_base<int64_t>::min_val (); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
713 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
714 else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
715 return static_cast<int64_t> (p); |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
716 } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
717 #else |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
718 // Special handler for 64-bit integer multiply. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
719 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
720 OCTAVE_API int64_t |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
721 octave_int_arith_base<int64_t, true>::mul (int64_t, int64_t); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
722 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
723 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
724 // This class simply selects the proper arithmetics. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
725 template<class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
726 class octave_int_arith |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
727 : public octave_int_arith_base<T, std::numeric_limits<T>::is_signed> |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
728 { }; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
729 |
4902 | 730 template <class T> |
731 class | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
732 octave_int : public octave_int_base<T> |
4902 | 733 { |
734 public: | |
4943 | 735 typedef T val_type; |
736 | |
4902 | 737 octave_int (void) : ival () { } |
738 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
739 octave_int (T i) : ival (i) { } |
4902 | 740 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
741 octave_int (double d) : ival (octave_int_base<T>::convert_real (d)) { } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
742 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
743 octave_int (float d) : ival (octave_int_base<T>::convert_real (d)) { } |
6402 | 744 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
745 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
746 octave_int (long double d) : ival (octave_int_base<T>::convert_real (d)) { } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
747 #endif |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
748 |
4902 | 749 octave_int (bool b) : ival (b) { } |
750 | |
751 template <class U> | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
752 octave_int (const U& i) : ival(octave_int_base<T>::truncate_int (i)) { } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
753 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
754 template <class U> |
4902 | 755 octave_int (const octave_int<U>& i) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
756 : ival (octave_int_base<T>::truncate_int (i.value ())) { } |
4902 | 757 |
758 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
759 | |
760 octave_int& operator = (const octave_int<T>& i) | |
761 { | |
762 ival = i.ival; | |
763 return *this; | |
764 } | |
765 | |
766 T value (void) const { return ival; } | |
767 | |
4949 | 768 const unsigned char * iptr (void) const |
769 { return reinterpret_cast<const unsigned char *> (& ival); } | |
770 | |
4902 | 771 bool operator ! (void) const { return ! ival; } |
772 | |
7198 | 773 bool bool_value (void) const { return static_cast<bool> (value ()); } |
774 | |
775 char char_value (void) const { return static_cast<char> (value ()); } | |
776 | |
777 double double_value (void) const { return static_cast<double> (value ()); } | |
778 | |
779 float float_value (void) const { return static_cast<float> (value ()); } | |
780 | |
7177 | 781 operator T (void) const { return value (); } |
782 | |
7198 | 783 // char and bool operators intentionally omitted. |
5533 | 784 |
7198 | 785 operator double (void) const { return double_value (); } |
4902 | 786 |
7198 | 787 operator float (void) const { return float_value (); } |
5030 | 788 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
789 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
790 operator + () const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
791 { return *this; } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
792 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
793 // unary operators & mappers |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
794 #define OCTAVE_INT_UN_OP(OPNAME,NAME) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
795 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
796 OPNAME () const \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
797 { return octave_int_arith<T>::NAME (ival); } |
4902 | 798 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
799 OCTAVE_INT_UN_OP(operator -, minus) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
800 OCTAVE_INT_UN_OP(abs, abs) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
801 OCTAVE_INT_UN_OP(signum, signum) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
802 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
803 #undef OCTAVE_INT_UN_OP |
4902 | 804 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
805 // Homogeneous binary integer operations. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
806 #define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
807 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
808 operator OP (const ARGT& y) const \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
809 { return octave_int_arith<T>::NAME (ival, y); } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
810 inline octave_int<T>& \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
811 operator OP##= (const ARGT& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
812 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
813 ival = octave_int_arith<T>::NAME (ival, y); \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
814 return *this; \ |
4952 | 815 } |
816 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
817 OCTAVE_INT_BIN_OP(+, add, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
818 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
819 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
820 OCTAVE_INT_BIN_OP(/, div, octave_int<T>) |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
821 OCTAVE_INT_BIN_OP(%, rem, octave_int<T>) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
822 OCTAVE_INT_BIN_OP(<<, lshift, int) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
823 OCTAVE_INT_BIN_OP(>>, rshift, int) |
4952 | 824 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
825 #undef OCTAVE_INT_BIN_OP |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
826 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
827 static octave_int<T> min (void) { return std::numeric_limits<T>::min (); } |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
828 static octave_int<T> max (void) { return std::numeric_limits<T>::max (); } |
4906 | 829 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
830 static int nbits (void) { return std::numeric_limits<T>::digits; } |
4909 | 831 |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14168
diff
changeset
|
832 static int byte_size (void) { return sizeof (T); } |
4949 | 833 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
834 static const char *type_name (); |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
835 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
836 // The following are provided for convenience. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
837 static const octave_int zero, one; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
838 |
5900 | 839 // Unsafe. This function exists to support the MEX interface. |
840 // You should not use it anywhere else. | |
841 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
842 | |
4902 | 843 private: |
844 | |
845 T ival; | |
846 }; | |
847 | |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
848 template <class T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
849 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
850 rem (const octave_int<T>& x, const octave_int<T>& y) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
851 { return octave_int_arith<T>::rem (x.value (), y.value ()); } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
852 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
853 template <class T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
854 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
855 mod (const octave_int<T>& x, const octave_int<T>& y) |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
856 { return octave_int_arith<T>::mod (x.value (), y.value ()); } |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
857 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
858 // No mixed integer binary operations! |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
859 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
860 template <class T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
861 inline bool |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
862 xisnan (const octave_int<T>&) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
863 { return false; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
864 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
865 // FIXME: can/should any of these be inline? |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
866 |
4902 | 867 template <class T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
868 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
869 pow (const octave_int<T>&, const octave_int<T>&); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
870 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
871 template <class T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
872 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
873 pow (const double& a, const octave_int<T>& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
874 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
875 template <class T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
876 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
877 pow (const octave_int<T>& a, const double& b); |
7922
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
878 |
935be827eaf8
error for NaN values in & and | expressions
John W. Eaton <jwe@octave.org>
parents:
7789
diff
changeset
|
879 template <class T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
880 extern OCTAVE_API octave_int<T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
881 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
|
882 |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
883 template <class T> |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
884 extern OCTAVE_API octave_int<T> |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
885 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
|
886 |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
887 // 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
|
888 // 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
|
889 // one? |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
890 template <class T> |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
891 extern OCTAVE_API octave_int<T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
892 powf (const float& a, const octave_int<T>& b); |
4952 | 893 |
894 template <class T> | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
895 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
896 powf (const octave_int<T>& a, const float& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
897 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
898 // Binary relations |
4953 | 899 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
900 #define OCTAVE_INT_CMP_OP(OP, NAME) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
901 template<class T1, class T2> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
902 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
903 operator OP (const octave_int<T1>& x, const octave_int<T2>& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
904 { return octave_int_cmp_op::op<octave_int_cmp_op::NAME, T1, T2> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
905 (x.value (), y.value ()); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
906 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
907 OCTAVE_INT_CMP_OP (<, lt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
908 OCTAVE_INT_CMP_OP (<=, le) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
909 OCTAVE_INT_CMP_OP (>, gt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
910 OCTAVE_INT_CMP_OP (>=, ge) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
911 OCTAVE_INT_CMP_OP (==, eq) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
912 OCTAVE_INT_CMP_OP (!=, ne) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
913 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
914 #undef OCTAVE_INT_CMP_OP |
4953 | 915 |
916 template <class T> | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
917 inline std::ostream& |
4902 | 918 operator << (std::ostream& os, const octave_int<T>& ival) |
919 { | |
920 os << ival.value (); | |
921 return os; | |
922 } | |
923 | |
924 template <class T> | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
925 inline std::istream& |
4902 | 926 operator >> (std::istream& is, octave_int<T>& ival) |
927 { | |
928 T tmp = 0; | |
929 is >> tmp; | |
930 ival = tmp; | |
931 return is; | |
932 } | |
933 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
934 // Bitwise operations |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
935 |
4906 | 936 #define OCTAVE_INT_BITCMP_OP(OP) \ |
937 template <class T> \ | |
938 octave_int<T> \ | |
939 operator OP (const octave_int<T>& x, const octave_int<T>& y) \ | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
940 { return x.value () OP y.value (); } |
4906 | 941 |
942 OCTAVE_INT_BITCMP_OP (&) | |
943 OCTAVE_INT_BITCMP_OP (|) | |
944 OCTAVE_INT_BITCMP_OP (^) | |
945 | |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
946 #undef OCTAVE_INT_BITCMP_OP |
4906 | 947 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
948 // General bit shift. |
4909 | 949 template <class T> |
950 octave_int<T> | |
4920 | 951 bitshift (const octave_int<T>& a, int n, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
952 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909 | 953 { |
954 if (n > 0) | |
4952 | 955 return (a << n) & mask; |
4909 | 956 else if (n < 0) |
4952 | 957 return (a >> -n) & mask; |
4909 | 958 else |
8963
d1eab3ddb02d
oct-inttypes.h (bitshift): apply mask even if not shifting
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
959 return a & mask; |
4909 | 960 } |
961 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
962 typedef octave_int<int8_t> octave_int8; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
963 typedef octave_int<int16_t> octave_int16; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
964 typedef octave_int<int32_t> octave_int32; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
965 typedef octave_int<int64_t> octave_int64; |
4902 | 966 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
967 typedef octave_int<uint8_t> octave_uint8; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
968 typedef octave_int<uint16_t> octave_uint16; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
969 typedef octave_int<uint32_t> octave_uint32; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
970 typedef octave_int<uint64_t> octave_uint64; |
5072 | 971 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
972 #define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
973 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
974 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
975 operator OP (const octave_int<T>& x, const double& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
976 { return octave_int<T> (static_cast<double> (x) OP y); } \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
977 template <class T> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
978 inline octave_int<T> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
979 operator OP (const double& x, const octave_int<T>& y) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
980 { return octave_int<T> (x OP static_cast<double> (y)); } \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
981 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
982 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
983 // Handle mixed op using long double |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
984 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
985 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
986 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
987 inline octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
988 operator OP (const double& x, const octave_int64& y) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
989 { return octave_int64 (x OP static_cast<long double> (y.value ())); } \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
990 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
991 inline octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
992 operator OP (const double& x, const octave_uint64& y) \ |
8191
9cb73236e552
fix uint64 mixed operations
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
993 { return octave_uint64 (x OP static_cast<long double> (y.value ())); } \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
994 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
995 inline octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
996 operator OP (const octave_int64& x, const double& y) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
997 { return octave_int64 (static_cast<long double> (x.value ()) OP y); } \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
998 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
999 inline octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1000 operator OP (const octave_uint64& x, const double& y) \ |
8191
9cb73236e552
fix uint64 mixed operations
Jaroslav Hajek <highegg@gmail.com>
parents:
8185
diff
changeset
|
1001 { return octave_uint64 (static_cast<long double> (x.value ()) OP y); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1002 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1003 #else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1004 // external handlers |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1005 #define OCTAVE_INT_DOUBLE_BIN_OP(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1006 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1007 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1008 OCTAVE_API octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1009 operator OP (const double&, const octave_int64&); \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1010 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1011 OCTAVE_API octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1012 operator OP (const double&, const octave_uint64&); \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1013 template <> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1014 OCTAVE_API octave_int64 \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1015 operator OP (const octave_int64&, const double&); \ |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
1016 template <> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1017 OCTAVE_API octave_uint64 \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1018 operator OP (const octave_uint64&, const double&); |
5072 | 1019 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1020 #endif |
5072 | 1021 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1022 OCTAVE_INT_DOUBLE_BIN_OP (+) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1023 OCTAVE_INT_DOUBLE_BIN_OP (-) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1024 OCTAVE_INT_DOUBLE_BIN_OP (*) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1025 OCTAVE_INT_DOUBLE_BIN_OP (/) |
5072 | 1026 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1027 #undef OCTAVE_INT_DOUBLE_BIN_OP0 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1028 #undef OCTAVE_INT_DOUBLE_BIN_OP |
5072 | 1029 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1030 #define OCTAVE_INT_DOUBLE_CMP_OP(OP,NAME) \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1031 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1032 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1033 operator OP (const octave_int<T>& x, const double& y) \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1034 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x.value (), y); } \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1035 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1036 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1037 operator OP (const double& x, const octave_int<T>& y) \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1038 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x, y.value ()); } |
5072 | 1039 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1040 OCTAVE_INT_DOUBLE_CMP_OP (<, lt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1041 OCTAVE_INT_DOUBLE_CMP_OP (<=, le) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1042 OCTAVE_INT_DOUBLE_CMP_OP (>=, ge) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1043 OCTAVE_INT_DOUBLE_CMP_OP (>, gt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1044 OCTAVE_INT_DOUBLE_CMP_OP (==, eq) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1045 OCTAVE_INT_DOUBLE_CMP_OP (!=, ne) |
5072 | 1046 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1047 #undef OCTAVE_INT_DOUBLE_CMP_OP |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1048 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1049 // Floats are handled by simply converting to doubles. |
5072 | 1050 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1051 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1052 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1053 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1054 operator OP (const octave_int<T>& x, float y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1055 { return x OP static_cast<double> (y); } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1056 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1057 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1058 operator OP (float x, const octave_int<T>& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1059 { return static_cast<double> (x) OP y; } |
5072 | 1060 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1061 OCTAVE_INT_FLOAT_BIN_OP (+) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1062 OCTAVE_INT_FLOAT_BIN_OP (-) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1063 OCTAVE_INT_FLOAT_BIN_OP (*) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1064 OCTAVE_INT_FLOAT_BIN_OP (/) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1065 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1066 #undef OCTAVE_INT_FLOAT_BIN_OP |
5072 | 1067 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1068 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1069 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1070 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1071 operator OP (const octave_int<T>& x, const float& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1072 { return x OP static_cast<double> (y); } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1073 template <class T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1074 bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1075 operator OP (const float& x, const octave_int<T>& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1076 { return static_cast<double> (x) OP y; } |
4902 | 1077 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1078 OCTAVE_INT_FLOAT_CMP_OP (<) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1079 OCTAVE_INT_FLOAT_CMP_OP (<=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1080 OCTAVE_INT_FLOAT_CMP_OP (>=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1081 OCTAVE_INT_FLOAT_CMP_OP (>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1082 OCTAVE_INT_FLOAT_CMP_OP (==) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1083 OCTAVE_INT_FLOAT_CMP_OP (!=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1084 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1085 #undef OCTAVE_INT_FLOAT_CMP_OP |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1086 |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1087 template <class T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1088 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1089 xmax (const octave_int<T>& x, const octave_int<T>& y) |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1090 { |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1091 const T xv = x.value (), yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1092 return octave_int<T> (xv >= yv ? xv : yv); |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1093 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1094 |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1095 template <class T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1096 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1097 xmin (const octave_int<T>& x, const octave_int<T>& y) |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1098 { |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1099 const T xv = x.value (), yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1100 return octave_int<T> (xv <= yv ? xv : yv); |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1101 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1102 |
4902 | 1103 #endif |