Mercurial > octave
annotate liboctave/util/oct-inttypes.h @ 21244:1473547f50f5
include octave-config.h in public header files
* mk-opts.pl, mkbuiltins, mk-ops.awk, sparse-mk-ops.awk:
Emit "#include octave-config.h" statement for generated header files.
* build-env.h, builtins.h, Cell.h, base-text-renderer.h,
c-file-ptr-stream.h, cdisplay.h, comment-list.h, data.h, debug.h,
defaults.in.h, defun-dld.h, defun-int.h, defun.h, dirfns.h, display.h,
dynamic-ld.h, error.h, errwarn.h, event-queue.h, file-io.h,
ft-text-renderer.h, gl-render.h, gl2ps-print.h, graphics.in.h,
gripes.h, help.h, hook-fcn.h, input.h, jit-ir.h, jit-typeinfo.h,
jit-util.h, load-path.h, load-save.h, ls-ascii-helper.h, ls-hdf5.h,
ls-mat-ascii.h, ls-mat4.h, ls-mat5.h, ls-oct-binary.h, ls-oct-text.h,
ls-utils.h, mex.h, mexproto.h, mxarray.in.h, oct-errno.h, oct-fstrm.h,
oct-handle.h, oct-hdf5-types.h, oct-hdf5.h, oct-hist.h, oct-iostrm.h,
oct-lvalue.h, oct-map.h, oct-obj.h, oct-opengl.h, oct-prcstrm.h,
oct-procbuf.h, oct-stdstrm.h, oct-stream.h, oct-strstrm.h, oct.h,
octave-default-image.h, octave-link.h, octave-preserve-stream-state.h,
pager.h, pr-output.h, procstream.h, profiler.h, pt-jit.h,
sighandlers.h, siglist.h, sparse-xdiv.h, sparse-xpow.h, symtab.h,
sysdep.h, text-renderer.h, toplev.h, txt-eng.h, utils.h, variables.h,
workspace-element.h, xdiv.h, xnorm.h, xpow.h, zfstream.h, oct-qhull.h,
ov-base-diag.h, ov-base-int.h, ov-base-mat.h, ov-base-scalar.h,
ov-base-sparse.h, ov-base.h, ov-bool-mat.h, ov-bool-sparse.h,
ov-bool.h, ov-builtin.h, ov-cell.h, ov-ch-mat.h, ov-class.h,
ov-classdef.h, ov-colon.h, ov-complex.h, ov-cs-list.h, ov-cx-diag.h,
ov-cx-mat.h, ov-cx-sparse.h, ov-dld-fcn.h, ov-fcn-handle.h,
ov-fcn-inline.h, ov-fcn.h, ov-float.h, ov-flt-complex.h,
ov-flt-cx-diag.h, ov-flt-cx-mat.h, ov-flt-re-diag.h, ov-flt-re-mat.h,
ov-int-traits.h, ov-int16.h, ov-int32.h, ov-int64.h, ov-int8.h,
ov-intx.h, ov-java.h, ov-lazy-idx.h, ov-mex-fcn.h, ov-null-mat.h,
ov-oncleanup.h, ov-perm.h, ov-range.h, ov-re-diag.h, ov-re-mat.h,
ov-re-sparse.h, ov-scalar.h, ov-str-mat.h, ov-struct.h,
ov-type-conv.h, ov-typeinfo.h, ov-uint16.h, ov-uint32.h, ov-uint64.h,
ov-uint8.h, ov-usr-fcn.h, ov.h, ovl.h, octave.h, op-int.h, ops.h,
options-usage.h, lex.h, parse.h, pt-all.h, pt-arg-list.h,
pt-array-list.h, pt-assign.h, pt-binop.h, pt-bp.h, pt-cbinop.h,
pt-cell.h, pt-check.h, pt-classdef.h, pt-cmd.h, pt-colon.h,
pt-const.h, pt-decl.h, pt-eval.h, pt-except.h, pt-exp.h,
pt-fcn-handle.h, pt-funcall.h, pt-id.h, pt-idx.h, pt-jump.h,
pt-loop.h, pt-mat.h, pt-misc.h, pt-pr-code.h, pt-select.h, pt-stmt.h,
pt-unop.h, pt-walk.h, pt.h, token.h, version.in.h, Array-util.h,
Array.h, CColVector.h, CDiagMatrix.h, CMatrix.h, CNDArray.h,
CRowVector.h, CSparse.h, DiagArray2.h, MArray.h, MDiagArray2.h,
MSparse.h, Matrix.h, MatrixType.h, PermMatrix.h, Range.h, Sparse.h,
boolMatrix.h, boolNDArray.h, boolSparse.h, chMatrix.h, chNDArray.h,
dColVector.h, dDiagMatrix.h, dMatrix.h, dNDArray.h, dRowVector.h,
dSparse.h, dim-vector.h, fCColVector.h, fCDiagMatrix.h, fCMatrix.h,
fCNDArray.h, fCRowVector.h, fColVector.h, fDiagMatrix.h, fMatrix.h,
fNDArray.h, fRowVector.h, idx-vector.h, int16NDArray.h,
int32NDArray.h, int64NDArray.h, int8NDArray.h, intNDArray.h,
uint16NDArray.h, uint32NDArray.h, uint64NDArray.h, uint8NDArray.h,
f77-fcn.h, lo-error.h, quit.h, CmplxAEPBAL.h, CmplxCHOL.h,
CmplxGEPBAL.h, CmplxHESS.h, CmplxLU.h, CmplxQR.h, CmplxQRP.h,
CmplxSCHUR.h, CmplxSVD.h, CollocWt.h, DAE.h, DAEFunc.h, DAERT.h,
DAERTFunc.h, DASPK.h, DASRT.h, DASSL.h, DET.h, EIG.h, LSODE.h, ODE.h,
ODEFunc.h, ODES.h, ODESFunc.h, Quad.h, base-aepbal.h, base-dae.h,
base-de.h, base-lu.h, base-min.h, base-qr.h, bsxfun-decl.h, bsxfun.h,
dbleAEPBAL.h, dbleCHOL.h, dbleGEPBAL.h, dbleHESS.h, dbleLU.h,
dbleQR.h, dbleQRP.h, dbleSCHUR.h, dbleSVD.h, eigs-base.h,
fCmplxAEPBAL.h, fCmplxCHOL.h, fCmplxGEPBAL.h, fCmplxHESS.h,
fCmplxLU.h, fCmplxQR.h, fCmplxQRP.h, fCmplxSCHUR.h, fCmplxSVD.h,
fEIG.h, floatAEPBAL.h, floatCHOL.h, floatGEPBAL.h, floatHESS.h,
floatLU.h, floatQR.h, floatQRP.h, floatSCHUR.h, floatSVD.h,
lo-mappers.h, lo-specfun.h, oct-convn.h, oct-fftw.h, oct-norm.h,
oct-rand.h, oct-spparms.h, randgamma.h, randmtzig.h, randpoisson.h,
sparse-chol.h, sparse-dmsolve.h, sparse-lu.h, sparse-qr.h,
Sparse-diag-op-defs.h, Sparse-op-decls.h, Sparse-op-defs.h,
Sparse-perm-op-defs.h, mx-base.h, mx-defs.h, mx-ext.h, mx-op-decl.h,
mx-op-defs.h, dir-ops.h, file-ops.h, file-stat.h, lo-sysdep.h,
mach-info.h, oct-env.h, oct-group.h, oct-openmp.h, oct-passwd.h,
oct-syscalls.h, oct-time.h, oct-uname.h, pathlen.h, sysdir.h,
syswait.h, action-container.h, base-list.h, byte-swap.h,
caseless-str.h, cmd-edit.h, cmd-hist.h, data-conv.h, functor.h,
glob-match.h, lo-array-errwarn.h, lo-array-gripes.h, lo-cutils.h,
lo-ieee.h, lo-macros.h, lo-math.h, lo-regexp.h, lo-traits.h,
lo-utils.h, oct-alloc.h, oct-base64.h, oct-binmap.h, oct-cmplx.h,
oct-glob.h, oct-inttypes.h, oct-locbuf.h, oct-mutex.h, oct-refcount.h,
oct-rl-edit.h, oct-rl-hist.h, oct-shlib.h, oct-sort.h, oct-sparse.h,
pathsearch.h, singleton-cleanup.h, sparse-sort.h, sparse-util.h,
statdefs.h, str-vec.h, sun-utils.h, unwind-prot.h, url-transfer.h:
Include octave-config.h.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2016 14:25:53 -0500 |
parents | f7121e111991 |
children | 933083fee7ae |
rev | line source |
---|---|
4902 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18634
diff
changeset
|
3 Copyright (C) 2004-2015 John W. Eaton |
11523 | 4 Copyright (C) 2008-2009 Jaroslav Hajek |
4902 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4902 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4902 | 21 |
22 */ | |
23 | |
20791
f7084eae3318
maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents:
19861
diff
changeset
|
24 #if ! defined (octave_oct_inttypes_h) |
17822
ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
25 #define octave_oct_inttypes_h 1 |
4902 | 26 |
21244
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
27 #include "octave-config.h" |
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21202
diff
changeset
|
28 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
29 #include <cstdlib> |
6482 | 30 |
4902 | 31 #include <limits> |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
32 #include <iosfwd> |
4902 | 33 |
8731
5abe5ae55465
use lo-traits in oct-inttypes
Jaroslav Hajek <highegg@gmail.com>
parents:
8333
diff
changeset
|
34 #include "lo-traits.h" |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
35 #include "lo-math.h" |
4969 | 36 #include "lo-mappers.h" |
4902 | 37 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
38 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
39 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
|
40 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
|
41 { return xisnan (static_cast<double> (x)); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
42 #endif |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
43 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 // FIXME: we define this by our own because some compilers, such as |
8333 | 45 // MSVC, do not provide std::abs (int64_t) and std::abs (uint64_t). In |
46 // the future, it should go away in favor of std::abs. | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
47 template <typename T> |
8293
ad5bb02d267a
workaround missing std::abs(int64_t) in MSVC
Jaroslav Hajek <highegg@gmail.com>
parents:
8202
diff
changeset
|
48 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
|
49 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
50 // Query for an integer type of certain sizeof, and signedness. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
51 template <int qsize, bool qsigned> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
52 struct query_integer_type |
4902 | 53 { |
54 public: | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
55 static const bool registered = false; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
56 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
|
57 // attempt to use it in computations. |
4902 | 58 }; |
59 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
60 #define REGISTER_INT_TYPE(TYPE) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
61 template <> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
62 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
|
63 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
64 public: \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
65 static const bool registered = true; \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
66 typedef TYPE type; \ |
7598
a89b3fa632ee
partial specialization for octave_int_fit_to_range
John W. Eaton <jwe@octave.org>
parents:
7596
diff
changeset
|
67 } |
7596
6929e40fc597
compatible handling of NaN -> int conversions
John W. Eaton <jwe@octave.org>
parents:
7534
diff
changeset
|
68 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
69 // No two registered integers can share sizeof and signedness. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
70 REGISTER_INT_TYPE (int8_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
71 REGISTER_INT_TYPE (uint8_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
72 REGISTER_INT_TYPE (int16_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
73 REGISTER_INT_TYPE (uint16_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
74 REGISTER_INT_TYPE (int32_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
75 REGISTER_INT_TYPE (uint32_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
76 REGISTER_INT_TYPE (int64_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
77 REGISTER_INT_TYPE (uint64_t); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
78 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
79 // 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
|
80 // 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
|
81 // 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
|
82 #define REGISTER_OCTAVE_CMP_OP(NM,OP) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
83 class NM \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
84 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
85 public: \ |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
86 static const bool ltval = (0 OP 1); \ |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
87 static const bool gtval = (1 OP 0); \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
88 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
89 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
|
90 } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
91 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
92 // 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
|
93 // yielding always false. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
94 #define REGISTER_OCTAVE_CONST_OP(NM,value) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
95 class NM \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
96 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
97 public: \ |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
98 static const bool ltval = value; \ |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
99 static const bool gtval = value; \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
100 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
101 static bool op (T, T) { return value; } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
102 } |
4943 | 103 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
104 // Handles non-homogeneous integer comparisons. Avoids doing useless tests. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
105 class octave_int_cmp_op |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
106 { |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
107 // 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
|
108 // 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
|
109 // 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
|
110 // 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
|
111 // size. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
112 template <typename T1, typename T2> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
113 class prom |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 // Promote to int? |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
116 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
|
117 && sizeof (T2) < sizeof (int)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 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
|
119 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
|
120 static const bool psig = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 (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
|
122 static const int psize = |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 (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
|
124 ? sizeof (T2) : sizeof (T1))); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 public: |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 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
|
127 }; |
4943 | 128 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
129 // Implements comparisons between two types of equal size but |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
130 // possibly different signedness. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
131 template <typename xop, int size> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
132 class uiop |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 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
|
135 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
|
136 public: |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 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
|
138 { return xop::op (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 (stype x, stype y) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
140 { return xop::op (x, y); } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
141 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
|
142 { 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
|
143 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
|
144 { 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
|
145 }; |
4943 | 146 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
147 public: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
148 REGISTER_OCTAVE_CMP_OP (lt, <); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
149 REGISTER_OCTAVE_CMP_OP (le, <=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
150 REGISTER_OCTAVE_CMP_OP (gt, >); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
151 REGISTER_OCTAVE_CMP_OP (ge, >=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
152 REGISTER_OCTAVE_CMP_OP (eq, ==); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
153 REGISTER_OCTAVE_CMP_OP (ne, !=); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
154 REGISTER_OCTAVE_CONST_OP (ct, true); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
155 REGISTER_OCTAVE_CONST_OP (cf, false); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
156 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
157 // Universal comparison operation. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
158 template <typename xop, typename T1, typename T2> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
159 static bool |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
160 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
|
161 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 static_cast<PT2> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
166 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
167 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
168 public: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
169 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
170 // Mixed comparisons |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
171 template <typename xop, typename T> |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
172 static bool |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
173 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
|
174 { return xop::op (static_cast<double> (x), y); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
175 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
176 template <typename xop, typename T> |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
177 static bool |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
178 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
|
179 { return xop::op (x, static_cast<double> (y)); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
180 |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
181 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
182 # define DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS(T) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
183 template <typename xop> static OCTAVE_API bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
184 external_mop (double, T); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
185 template <typename xop> static OCTAVE_API bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
186 external_mop (T, double) |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
187 |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
188 DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS (int64_t); |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
189 DECLARE_EXTERNAL_LONG_DOUBLE_CMP_OPS (uint64_t); |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
190 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
191 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
192 // 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
|
193 // they lose precision. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
194 // If we have long doubles, use them... |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
195 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
196 # ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
197 # define DEFINE_LONG_DOUBLE_CMP_OP(T) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
198 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
199 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
200 mop (double x, T y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
201 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
202 return external_mop<xop> (x, y); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
203 } \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
204 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
205 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
206 mop (T x, double y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
207 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
208 return external_mop<xop> (x, y); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
209 } |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
210 # else |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
211 # define DEFINE_LONG_DOUBLE_CMP_OP(T) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
212 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
213 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
214 mop (double x, T y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
215 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
216 return xop::op (static_cast<long double> (x), \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
217 static_cast<long double> (y)); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
218 } \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
219 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
220 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
221 mop (T x, double y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
222 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
223 return xop::op (static_cast<long double> (x), \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
224 static_cast<long double> (y)); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
225 } |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
226 # endif |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
227 #else |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
228 // ... 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
|
229 |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
230 // 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
|
231 // 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
|
232 // explicit instantiations later in that case. |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
233 # define DEFINE_LONG_DOUBLE_CMP_OP(T) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
234 template <typename xop> static OCTAVE_API bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
235 emulate_mop (double, T); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
236 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
237 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
238 mop (double x, T y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
239 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
240 return emulate_mop<xop> (x, y); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
241 } \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
242 template <typename xop> static OCTAVE_API bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
243 emulate_mop (T, double); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
244 template <typename xop> \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
245 static bool \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
246 mop (T x, double y) \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
247 { \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
248 return emulate_mop<xop> (x, y); \ |
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
249 } |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
250 #endif |
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
251 |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
252 DEFINE_LONG_DOUBLE_CMP_OP(int64_t) |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
253 DEFINE_LONG_DOUBLE_CMP_OP(uint64_t) |
8856
ab4db66e286f
workaround gcc 4.3 explicit instantiation bug in octave_int_cmp_op
Jaroslav Hajek <highegg@gmail.com>
parents:
8731
diff
changeset
|
254 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
255 #undef DEFINE_LONG_DOUBLE_CMP_OP |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
256 }; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
257 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
258 // Base integer class. No data, just conversion methods and exception flags. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
259 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
260 class octave_int_base |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
261 { |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
262 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
263 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
264 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
|
265 static T max_val () { return std::numeric_limits<T>:: max (); } |
4943 | 266 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
267 // Convert integer value. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
268 template <typename S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
269 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
270 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
|
271 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
272 // 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
|
273 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
|
274 static const bool s_is_signed = std::numeric_limits<S>::is_signed; |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
275 static const int t_size = sizeof (T); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
276 static const int s_size = sizeof (S); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
277 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
278 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
|
279 (! 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
|
280 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
|
281 (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
|
282 && (! 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
|
283 // 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 typedef typename if_then_else<omit_chk_max, cf, gt>::result chk_max; |
4943 | 289 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
290 // 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
|
291 // 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
|
292 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
|
293 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
294 return min_val (); |
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 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
|
297 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
298 return max_val (); |
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 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
301 return static_cast<T> (value); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
302 } |
4943 | 303 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
304 private: |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
305 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
306 // Computes a real-valued threshold for a max/min check. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
307 template <typename S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
308 static S |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
309 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
|
310 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
311 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
|
312 // 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
|
313 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
|
314 // FIXME: is this always correct? |
19739
3fa35defe495
Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
315 val *= (static_cast<S> (1) - (std::numeric_limits<S>::epsilon () / 2)); |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
316 return val; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
317 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
318 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
319 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
320 // Convert a real number (check NaN and non-int). |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
321 template <typename S> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
322 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
323 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
|
324 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
325 // Compute proper thresholds. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
326 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
|
327 min_val ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
328 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
|
329 max_val ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
330 if (xisnan (value)) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
331 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
332 return static_cast<T> (0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
333 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
334 else if (value < thmin) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
335 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
336 return min_val (); |
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 else if (value > thmax) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
339 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
340 return max_val (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
341 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
342 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
343 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
344 S rvalue = xround (value); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
345 return static_cast<T> (rvalue); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
346 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
347 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
348 }; |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
349 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
350 // Saturated (homogeneous) integer arithmetics. The signed and unsigned |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
351 // implementations are significantly different, so we implement another layer |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
352 // 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
|
353 // 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
|
354 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
355 template <typename T, bool is_signed> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
356 class octave_int_arith_base |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
357 { }; |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
358 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
359 // 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
|
360 // overflows can be handled efficiently and reliably. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
361 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
362 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
|
363 { |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
364 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
365 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
366 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
367 abs (T x) { return x; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
368 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
369 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
370 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
|
371 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
372 // Shifts do not overflow. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
373 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
374 rshift (T x, int n) { return x >> n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
375 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
376 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
377 lshift (T x, int n) { return x << n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
378 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
379 static T |
10405
cc69a17ec801
remove integer math warnings
Jaroslav Hajek <highegg@gmail.com>
parents:
10346
diff
changeset
|
380 minus (T) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
381 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
382 return static_cast<T> (0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
383 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
384 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
385 // 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
|
386 // so the following should always work. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
387 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
388 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
|
389 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
390 T u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
391 if (u < x) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
392 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
393 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
|
394 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
395 return u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
396 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
397 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
398 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
399 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
|
400 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
401 T u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
402 if (u > x) |
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 u = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
405 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
406 return u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
407 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
408 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
409 // 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
|
410 // no suitable promotion type, this operation *MUST* be specialized. |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
411 static T mul (T x, T y) { return mul_internal (x, y); } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
412 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
413 static T |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
414 mul_internal (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
415 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
416 // 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
|
417 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
|
418 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
|
419 * static_cast<mptype> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
420 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
421 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
422 // 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
|
423 // computed by a single instruction. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
424 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
425 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
|
426 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
427 if (y != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
428 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
429 T z = x / y; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
430 T w = x % y; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
431 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
|
432 return z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
433 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
434 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
435 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
436 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
|
437 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
438 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
439 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
440 // Remainder. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
441 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
442 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
|
443 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
444 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
|
445 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
446 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
447 // 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
|
448 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
449 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
|
450 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
451 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
|
452 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
453 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
454 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
455 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
456 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
457 // Handle 64-bit multiply using long double |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
458 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
459 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
460 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
461 extern OCTAVE_API uint64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
462 octave_external_uint64_uint64_mul (uint64_t, uint64_t); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
463 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
464 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
465 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
466 template <> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
467 inline uint64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
468 octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t x, uint64_t y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
469 { |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
470 uint64_t retval; |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
471 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
472 long double p = static_cast<long double> (x) * static_cast<long double> (y); |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
473 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
474 if (p > static_cast<long double> (octave_int_base<uint64_t>::max_val ())) |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
475 retval = octave_int_base<uint64_t>::max_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
476 else |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
477 retval = static_cast<uint64_t> (p); |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
478 |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
479 return retval; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
480 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
481 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
482 template <> |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
483 inline uint64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
484 octave_int_arith_base<uint64_t, false>::mul (uint64_t x, uint64_t y) |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
485 { |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
486 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
487 return octave_external_uint64_uint64_mul (x, y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
488 #else |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
489 return mul_internal (x, y); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
490 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
491 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
492 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
493 #else |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
494 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
495 // Special handler for 64-bit integer multiply. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
496 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
497 OCTAVE_API uint64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
498 octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t, uint64_t); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
499 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
500 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
501 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
502 // Signed integer arithmetics. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
503 // 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
|
504 // should hold: |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
505 // 1. Signed numbers are represented by twos complement |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
506 // (see <http://en.wikipedia.org/wiki/Two%27s_complement>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
507 // 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
|
508 // 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
|
509 // 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
|
510 // (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
|
511 // need for things like -ftrapv). |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
512 // 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
|
513 // except for the shifts. Shifts are arithmetic. |
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 // 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
|
516 // 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
|
517 // 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
|
518 // modern pipeline processor architectures. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
519 // 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
|
520 // guaranteed by the standard. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
521 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
522 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
523 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
|
524 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
525 // The corresponding unsigned type. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
526 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
|
527 public: |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
528 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
529 // Returns 1 for negative number, 0 otherwise. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
530 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
|
531 __signbit (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
532 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
533 #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
|
534 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
|
535 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
536 return (x < 0) ? 1 : 0; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
537 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
538 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
539 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
540 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
541 abs (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
542 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
543 #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
|
544 // 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
|
545 // 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
|
546 // discard the following test. |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
547 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
|
548 T y = (x ^ m) - m; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
549 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
551 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
|
552 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
553 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
554 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
555 // -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
|
556 // 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
|
557 // 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
|
558 // 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
|
559 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
560 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
|
561 && 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
|
562 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
563 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
|
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 y = (x < 0) ? -x : x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
567 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
568 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
569 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
570 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
571 static T |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
572 signum (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
573 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
574 // 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
|
575 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
|
576 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
577 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
578 // FIXME: we do not have an authority what signed shifts should |
8333 | 579 // exactly do, so we define them the easy way. Note that Matlab does |
580 // not define signed shifts. | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
581 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
582 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
583 rshift (T x, int n) { return x >> n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
584 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
585 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
586 lshift (T x, int n) { return x << n; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
587 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
588 // Minus has problems similar to abs. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
589 static T |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
590 minus (T x) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
591 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
592 #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
|
593 T y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
594 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
|
595 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
596 --y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
597 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
598 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
599 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
600 T y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
601 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
|
602 && 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
|
603 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
604 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
|
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 y = -x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
608 return y; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
609 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
610 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
611 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
612 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
613 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
|
614 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
615 #ifdef HAVE_FAST_INT_OPS |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
616 // 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
|
617 // 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
|
618 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
619 T u = static_cast<UT> (x) + static_cast<UT> (y); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
620 T ux = u ^ x; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
621 T uy = u ^ y; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
622 if ((ux & uy) < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
623 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
624 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
|
625 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
626 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
627 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
628 // 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
|
629 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
630 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
631 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
632 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
|
633 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
634 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
|
635 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
636 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
637 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
638 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
639 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
640 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
641 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
|
642 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
643 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
|
644 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
645 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
646 u = x + y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
647 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
648 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
649 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
650 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
651 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
652 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
653 // This is very similar to addition. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
654 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
655 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
|
656 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
657 #ifdef HAVE_FAST_INT_OPS |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
658 // 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
|
659 // 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
|
660 // actually return int. |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
661 T u = static_cast<UT> (x) - static_cast<UT> (y); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
662 T ux = u ^ x; |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
663 T uy = u ^ ~y; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
664 if ((ux & uy) < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
665 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
666 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
|
667 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
668 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
669 #else |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
670 // 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
|
671 T u; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
672 if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
673 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
674 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
|
675 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
676 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
|
677 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
678 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
679 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
680 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
681 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
682 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
683 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
|
684 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
685 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
|
686 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
687 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
688 u = x - y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
689 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
690 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
691 return u; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
692 #endif |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
693 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
694 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
695 // 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
|
696 // no suitable promotion type, this operation *MUST* be specialized. |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
697 static T mul (T x, T y) { return mul_internal (x, y); } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
698 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
699 static T |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
700 mul_internal (T x, T y) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
701 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
702 // 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
|
703 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
|
704 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
|
705 * static_cast<mptype> (y)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
706 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
707 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
708 // Division. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
709 static T |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
710 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
|
711 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
712 T z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
713 if (y == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
714 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
715 if (x < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
716 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
|
717 else if (x != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
718 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
|
719 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
720 z = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
721 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
722 else if (y < 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
723 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
724 // 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
|
725 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
|
726 { |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
727 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
|
728 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
729 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
730 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
731 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
732 // 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
|
733 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
|
734 if (w <= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
735 z -= 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
736 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
737 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
738 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
739 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
740 z = x / y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
741 // 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
|
742 // 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
|
743 // 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
|
744 T w = octave_int_abs (x % y); |
8333 | 745 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
746 if (w >= y - w) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
747 z += 1 - (__signbit (x) << 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
748 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
749 return z; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
750 } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
751 |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
752 // Remainder. |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
753 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
754 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
|
755 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
756 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
|
757 } |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
758 |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
759 // 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
|
760 static T |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
761 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
|
762 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
763 if (y != 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
764 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
765 T r = x % y; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
766 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
|
767 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
768 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
769 return x; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
770 } |
7521
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
771 }; |
6f10bbb2854a
avoid some GCC warnings for unsigned comparisons
John W. Eaton <jwe@octave.org>
parents:
7503
diff
changeset
|
772 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
773 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
774 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
775 // Handle 64-bit multiply using long double |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
776 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
777 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
778 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
779 extern OCTAVE_API int64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
780 octave_external_int64_int64_mul (int64_t, int64_t); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
781 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
782 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
783 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
784 template <> |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
785 inline int64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
786 octave_int_arith_base<int64_t, true>::mul_internal (int64_t x, int64_t y) |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
787 { |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
788 int64_t retval; |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
789 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
790 long double p = static_cast<long double> (x) * static_cast<long double> (y); |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
791 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
792 // 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
|
793 // 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
|
794 // whether it would really be faster. |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
795 if (p > static_cast<long double> (octave_int_base<int64_t>::max_val ())) |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
796 retval = octave_int_base<int64_t>::max_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
797 else if (p < static_cast<long double> (octave_int_base<int64_t>::min_val ())) |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
798 retval = octave_int_base<int64_t>::min_val (); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
799 else |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
800 retval = static_cast<int64_t> (p); |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
801 |
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
802 return retval; |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
803 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
804 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
805 template <> |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
806 inline int64_t |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
807 octave_int_arith_base<int64_t, true>::mul (int64_t x, int64_t y) |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
808 { |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
809 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
810 return octave_external_int64_int64_mul (x, y); |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
811 #else |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
812 return mul_internal (x, y); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
813 #endif |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
814 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
815 |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
816 #else |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
817 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
818 // Special handler for 64-bit integer multiply. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
819 template <> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
820 OCTAVE_API int64_t |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
821 octave_int_arith_base<int64_t, true>::mul_internal (int64_t, int64_t); |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
822 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
823 #endif |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
824 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
825 // This class simply selects the proper arithmetics. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
826 template <typename T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
827 class octave_int_arith |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
828 : 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
|
829 { }; |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
830 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
831 template <typename T> |
4902 | 832 class |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
833 octave_int : public octave_int_base<T> |
4902 | 834 { |
835 public: | |
4943 | 836 typedef T val_type; |
837 | |
4902 | 838 octave_int (void) : ival () { } |
839 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
840 octave_int (T i) : ival (i) { } |
4902 | 841 |
18627
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
842 // Always treat characters as unsigned. |
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
843 octave_int (char c) |
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
844 : ival (octave_int_base<T>::truncate_int (static_cast<unsigned char> (c))) |
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
845 { } |
37c300acfcfd
don't truncate when casting char to uintN values (bug #42054)
John W. Eaton <jwe@octave.org>
parents:
18252
diff
changeset
|
846 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
847 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
|
848 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
849 octave_int (float d) : ival (octave_int_base<T>::convert_real (d)) { } |
6402 | 850 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
851 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
852 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
|
853 #endif |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
854 |
4902 | 855 octave_int (bool b) : ival (b) { } |
856 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
857 template <typename U> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
858 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
|
859 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
860 template <typename U> |
4902 | 861 octave_int (const octave_int<U>& i) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
862 : ival (octave_int_base<T>::truncate_int (i.value ())) { } |
4902 | 863 |
864 octave_int (const octave_int<T>& i) : ival (i.ival) { } | |
865 | |
866 octave_int& operator = (const octave_int<T>& i) | |
867 { | |
868 ival = i.ival; | |
869 return *this; | |
870 } | |
871 | |
872 T value (void) const { return ival; } | |
873 | |
4949 | 874 const unsigned char * iptr (void) const |
875 { return reinterpret_cast<const unsigned char *> (& ival); } | |
876 | |
4902 | 877 bool operator ! (void) const { return ! ival; } |
878 | |
7198 | 879 bool bool_value (void) const { return static_cast<bool> (value ()); } |
880 | |
881 char char_value (void) const { return static_cast<char> (value ()); } | |
882 | |
883 double double_value (void) const { return static_cast<double> (value ()); } | |
884 | |
885 float float_value (void) const { return static_cast<float> (value ()); } | |
886 | |
7177 | 887 operator T (void) const { return value (); } |
888 | |
7198 | 889 // char and bool operators intentionally omitted. |
5533 | 890 |
7198 | 891 operator double (void) const { return double_value (); } |
4902 | 892 |
7198 | 893 operator float (void) const { return float_value (); } |
5030 | 894 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
895 octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
896 operator + () const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
897 { return *this; } |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
898 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
899 // unary operators & mappers |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
900 #define OCTAVE_INT_UN_OP(OPNAME,NAME) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
901 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
902 OPNAME () const \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
903 { return octave_int_arith<T>::NAME (ival); } |
4902 | 904 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
905 OCTAVE_INT_UN_OP(operator -, minus) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
906 OCTAVE_INT_UN_OP(abs, abs) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
907 OCTAVE_INT_UN_OP(signum, signum) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
908 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
909 #undef OCTAVE_INT_UN_OP |
4902 | 910 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
911 // Homogeneous binary integer operations. |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
912 #define OCTAVE_INT_BIN_OP(OP, NAME, ARGT) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
913 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
914 operator OP (const ARGT& y) const \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
915 { return octave_int_arith<T>::NAME (ival, y); } \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
916 inline octave_int<T>& \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
917 operator OP##= (const ARGT& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
918 { \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
919 ival = octave_int_arith<T>::NAME (ival, y); \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
920 return *this; \ |
4952 | 921 } |
922 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
923 OCTAVE_INT_BIN_OP(+, add, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
924 OCTAVE_INT_BIN_OP(-, sub, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
925 OCTAVE_INT_BIN_OP(*, mul, octave_int<T>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
926 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
|
927 OCTAVE_INT_BIN_OP(%, rem, octave_int<T>) |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
928 OCTAVE_INT_BIN_OP(<<, lshift, int) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
929 OCTAVE_INT_BIN_OP(>>, rshift, int) |
4952 | 930 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
931 #undef OCTAVE_INT_BIN_OP |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7198
diff
changeset
|
932 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
933 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
|
934 static octave_int<T> max (void) { return std::numeric_limits<T>::max (); } |
4906 | 935 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
936 static int nbits (void) { return std::numeric_limits<T>::digits; } |
4909 | 937 |
15018
3d8ace26c5b4
maint: Use Octave coding conventions for cuddled parentheses in liboctave/.
Rik <rik@octave.org>
parents:
14168
diff
changeset
|
938 static int byte_size (void) { return sizeof (T); } |
4949 | 939 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
940 static const char *type_name (); |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
941 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
942 // The following are provided for convenience. |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
943 static const octave_int zero, one; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
944 |
5900 | 945 // Unsafe. This function exists to support the MEX interface. |
946 // You should not use it anywhere else. | |
947 void *mex_get_data (void) const { return const_cast<T *> (&ival); } | |
948 | |
4902 | 949 private: |
950 | |
951 T ival; | |
952 }; | |
953 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
954 template <typename T> |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
955 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
956 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
|
957 { 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
|
958 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
959 template <typename T> |
10436
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
960 inline octave_int<T> |
00219bdd2d17
implement built-in rem and mod
Jaroslav Hajek <highegg@gmail.com>
parents:
10405
diff
changeset
|
961 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
|
962 { 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
|
963 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
964 // No mixed integer binary operations! |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
965 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
966 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
967 inline bool |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
968 xisnan (const octave_int<T>&) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
969 { return false; } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
970 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
971 // FIXME: can/should any of these be inline? |
7997
2b8952e133c9
implement checked conversions between integers
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
972 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
973 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
974 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
975 pow (const octave_int<T>&, const octave_int<T>&); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
976 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
977 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
978 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
979 pow (const double& a, const octave_int<T>& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
980 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
981 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
982 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
983 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
|
984 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
985 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
986 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
|
987 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
|
988 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
989 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
990 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
|
991 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
|
992 |
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
993 // 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
|
994 // 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
|
995 // one? |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
996 template <typename T> |
13012
15eefbd9d4e8
Implement a few missing automatic bsxfun power operators
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
11586
diff
changeset
|
997 extern OCTAVE_API octave_int<T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
998 powf (const float& a, const octave_int<T>& b); |
4952 | 999 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1000 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1001 extern OCTAVE_API octave_int<T> |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1002 powf (const octave_int<T>& a, const float& b); |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1003 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1004 // Binary relations |
4953 | 1005 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1006 #define OCTAVE_INT_CMP_OP(OP, NAME) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1007 template <typename T1, typename T2> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1008 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1009 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
|
1010 { 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
|
1011 (x.value (), y.value ()); } |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1012 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1013 OCTAVE_INT_CMP_OP (<, lt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1014 OCTAVE_INT_CMP_OP (<=, le) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1015 OCTAVE_INT_CMP_OP (>, gt) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1016 OCTAVE_INT_CMP_OP (>=, ge) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1017 OCTAVE_INT_CMP_OP (==, eq) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1018 OCTAVE_INT_CMP_OP (!=, ne) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1019 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1020 #undef OCTAVE_INT_CMP_OP |
4953 | 1021 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1022 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1023 inline std::ostream& |
4902 | 1024 operator << (std::ostream& os, const octave_int<T>& ival) |
1025 { | |
1026 os << ival.value (); | |
1027 return os; | |
1028 } | |
1029 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1030 template <typename T> |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1031 inline std::istream& |
4902 | 1032 operator >> (std::istream& is, octave_int<T>& ival) |
1033 { | |
1034 T tmp = 0; | |
1035 is >> tmp; | |
1036 ival = tmp; | |
1037 return is; | |
1038 } | |
1039 | |
18252
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1040 // We need to specialise for char and unsigned char because |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1041 // std::operator<< and std::operator>> are overloaded to input and |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1042 // output the ASCII character values instead of a representation of |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1043 // their numerical value (e.g. os << char(10) outputs a space instead |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1044 // of outputting the characters '1' and '0') |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1045 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1046 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1047 inline std::ostream& |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1048 operator << (std::ostream& os, const octave_int<int8_t>& ival) |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1049 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1050 os << static_cast<int> (ival.value ()); |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1051 return os; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1052 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1053 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1054 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1055 inline std::ostream& |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1056 operator << (std::ostream& os, const octave_int<uint8_t>& ival) |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1057 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1058 os << static_cast<unsigned int> (ival.value ()); |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1059 return os; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1060 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1061 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1062 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1063 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1064 inline std::istream& |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1065 operator >> (std::istream& is, octave_int<int8_t>& ival) |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1066 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1067 int tmp = 0; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1068 is >> tmp; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1069 ival = static_cast<int8_t> (tmp); |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1070 return is; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1071 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1072 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1073 template <> |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1074 inline std::istream& |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1075 operator >> (std::istream& is, octave_int<uint8_t>& ival) |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1076 { |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1077 unsigned int tmp = 0; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1078 is >> tmp; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1079 ival = static_cast<uint8_t> (tmp); |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1080 return is; |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1081 } |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1082 |
284e5c87f27b
Fix saving int8 and uint8 in plain text format (bug #40980)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
18017
diff
changeset
|
1083 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1084 // Bitwise operations |
7789
82be108cc558
First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents:
7598
diff
changeset
|
1085 |
4906 | 1086 #define OCTAVE_INT_BITCMP_OP(OP) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1087 template <typename T> \ |
4906 | 1088 octave_int<T> \ |
1089 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
|
1090 { return x.value () OP y.value (); } |
4906 | 1091 |
1092 OCTAVE_INT_BITCMP_OP (&) | |
1093 OCTAVE_INT_BITCMP_OP (|) | |
1094 OCTAVE_INT_BITCMP_OP (^) | |
1095 | |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1096 #undef OCTAVE_INT_BITCMP_OP |
4906 | 1097 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1098 // General bit shift. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1099 template <typename T> |
4909 | 1100 octave_int<T> |
4920 | 1101 bitshift (const octave_int<T>& a, int n, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1102 const octave_int<T>& mask = std::numeric_limits<T>::max ()) |
4909 | 1103 { |
1104 if (n > 0) | |
4952 | 1105 return (a << n) & mask; |
4909 | 1106 else if (n < 0) |
4952 | 1107 return (a >> -n) & mask; |
4909 | 1108 else |
8963
d1eab3ddb02d
oct-inttypes.h (bitshift): apply mask even if not shifting
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
1109 return a & mask; |
4909 | 1110 } |
1111 | |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1112 typedef octave_int<int8_t> octave_int8; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1113 typedef octave_int<int16_t> octave_int16; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1114 typedef octave_int<int32_t> octave_int32; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1115 typedef octave_int<int64_t> octave_int64; |
4902 | 1116 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1117 typedef octave_int<uint8_t> octave_uint8; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1118 typedef octave_int<uint16_t> octave_uint16; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1119 typedef octave_int<uint32_t> octave_uint32; |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1120 typedef octave_int<uint64_t> octave_uint64; |
5072 | 1121 |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1122 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1123 |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1124 #define DECLARE_EXTERNAL_LONG_DOUBLE_OP(T, OP) \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1125 extern OCTAVE_API T \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1126 external_double_ ## T ## _ ## OP (double x, T y); \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1127 extern OCTAVE_API T \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1128 external_ ## T ## _double_ ## OP (T x, double y) |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1129 |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1130 #define DECLARE_EXTERNAL_LONG_DOUBLE_OPS(T) \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1131 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, add); \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1132 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, sub); \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1133 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, mul); \ |
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1134 DECLARE_EXTERNAL_LONG_DOUBLE_OP (T, div) |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1135 |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1136 DECLARE_EXTERNAL_LONG_DOUBLE_OPS (octave_int64); |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1137 DECLARE_EXTERNAL_LONG_DOUBLE_OPS (octave_uint64); |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1138 |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1139 #endif |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1140 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1141 #define OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1142 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1143 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1144 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
|
1145 { return octave_int<T> (static_cast<double> (x) OP y); } \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1146 template <typename T> \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1147 inline octave_int<T> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1148 operator OP (const double& x, const octave_int<T>& y) \ |
19861
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19739
diff
changeset
|
1149 { return octave_int<T> (x OP static_cast<double> (y)); } |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1150 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1151 #ifdef OCTAVE_INT_USE_LONG_DOUBLE |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1152 // Handle mixed op using long double |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1153 #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
1154 # define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1155 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1156 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1157 inline octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1158 operator OP (const double& x, const octave_int64& y) \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1159 { \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1160 return external_double_octave_int64_ ## NAME (x, y); \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1161 } \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1162 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1163 inline octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1164 operator OP (const double& x, const octave_uint64& y) \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1165 { \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1166 return external_double_octave_uint64_ ## NAME (x, y); \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1167 } \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1168 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1169 inline octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1170 operator OP (const octave_int64& x, const double& y) \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1171 { \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1172 return external_octave_int64_double_ ## NAME (x, y); \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1173 } \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1174 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1175 inline octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1176 operator OP (const octave_uint64& x, const double& y) \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1177 { \ |
18017
0cd39f7f2409
additional improvements for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
18005
diff
changeset
|
1178 return external_octave_uint64_double_ ## NAME (x, y); \ |
17980
824c05a6d3ec
avoid errors for int64 ops implemented with long double (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17822
diff
changeset
|
1179 } |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1180 #else |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21139
diff
changeset
|
1181 # define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \ |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1182 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1183 template <> \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1184 inline octave_int64 \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1185 operator OP (const double& x, const octave_int64& y) \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1186 { \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1187 return octave_int64 (x OP static_cast<long double> (y.value ())); \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1188 } \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1189 template <> \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1190 inline octave_uint64 \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1191 operator OP (const double& x, const octave_uint64& y) \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1192 { \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1193 return octave_uint64 (x OP static_cast<long double> (y.value ())); \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1194 } \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1195 template <> \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1196 inline octave_int64 \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1197 operator OP (const octave_int64& x, const double& y) \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1198 { \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1199 return octave_int64 (static_cast<long double> (x.value ()) OP y); \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1200 } \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1201 template <> \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1202 inline octave_uint64 \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1203 operator OP (const octave_uint64& x, const double& y) \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1204 { \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1205 return octave_uint64 (static_cast<long double> (x.value ()) OP y); \ |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1206 } |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1207 #endif |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1208 #else |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1209 // external handlers |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1210 #define OCTAVE_INT_DOUBLE_BIN_OP(OP, NAME) \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1211 OCTAVE_INT_DOUBLE_BIN_OP0(OP) \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1212 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1213 OCTAVE_API octave_int64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1214 operator OP (const double&, const octave_int64&); \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1215 template <> \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1216 OCTAVE_API octave_uint64 \ |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1217 operator OP (const double&, const octave_uint64&); \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1218 template <> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1219 OCTAVE_API octave_int64 \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1220 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
|
1221 template <> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1222 OCTAVE_API octave_uint64 \ |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1223 operator OP (const octave_uint64&, const double&); |
5072 | 1224 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1225 #endif |
5072 | 1226 |
17993
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1227 OCTAVE_INT_DOUBLE_BIN_OP (+, add) |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1228 OCTAVE_INT_DOUBLE_BIN_OP (-, sub) |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1229 OCTAVE_INT_DOUBLE_BIN_OP (*, mul) |
ac9fd5010620
avoid including gnulib header in installed Octave header file (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17980
diff
changeset
|
1230 OCTAVE_INT_DOUBLE_BIN_OP (/, div) |
5072 | 1231 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1232 #undef OCTAVE_INT_DOUBLE_BIN_OP0 |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1233 #undef OCTAVE_INT_DOUBLE_BIN_OP |
18005
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1234 #undef DECLARE_EXTERNAL_LONG_DOUBLE_OP |
79653c5b6147
make int64 ops implemented with long double work again (bug #40607)
John W. Eaton <jwe@octave.org>
parents:
17993
diff
changeset
|
1235 #undef DECLARE_EXTERNAL_LONG_DOUBLE_OPS |
5072 | 1236 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1237 #define OCTAVE_INT_DOUBLE_CMP_OP(OP,NAME) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1238 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1239 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1240 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
|
1241 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x.value (), y); } \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1242 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1243 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1244 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
|
1245 { return octave_int_cmp_op::mop<octave_int_cmp_op::NAME> (x, y.value ()); } |
5072 | 1246 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1247 OCTAVE_INT_DOUBLE_CMP_OP (<, lt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1248 OCTAVE_INT_DOUBLE_CMP_OP (<=, le) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1249 OCTAVE_INT_DOUBLE_CMP_OP (>=, ge) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1250 OCTAVE_INT_DOUBLE_CMP_OP (>, gt) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1251 OCTAVE_INT_DOUBLE_CMP_OP (==, eq) |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1252 OCTAVE_INT_DOUBLE_CMP_OP (!=, ne) |
5072 | 1253 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1254 #undef OCTAVE_INT_DOUBLE_CMP_OP |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1255 |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1256 // Floats are handled by simply converting to doubles. |
5072 | 1257 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1258 #define OCTAVE_INT_FLOAT_BIN_OP(OP) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1259 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1260 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1261 operator OP (const octave_int<T>& x, float y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1262 { return x OP static_cast<double> (y); } \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1263 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1264 inline octave_int<T> \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1265 operator OP (float x, const octave_int<T>& y) \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1266 { return static_cast<double> (x) OP y; } |
5072 | 1267 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1268 OCTAVE_INT_FLOAT_BIN_OP (+) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1269 OCTAVE_INT_FLOAT_BIN_OP (-) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1270 OCTAVE_INT_FLOAT_BIN_OP (*) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1271 OCTAVE_INT_FLOAT_BIN_OP (/) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1272 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1273 #undef OCTAVE_INT_FLOAT_BIN_OP |
5072 | 1274 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1275 #define OCTAVE_INT_FLOAT_CMP_OP(OP) \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1276 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1277 inline bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1278 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
|
1279 { return x OP static_cast<double> (y); } \ |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1280 template <typename T> \ |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1281 bool \ |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1282 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
|
1283 { return static_cast<double> (x) OP y; } |
4902 | 1284 |
8169
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1285 OCTAVE_INT_FLOAT_CMP_OP (<) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1286 OCTAVE_INT_FLOAT_CMP_OP (<=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1287 OCTAVE_INT_FLOAT_CMP_OP (>=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1288 OCTAVE_INT_FLOAT_CMP_OP (>) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1289 OCTAVE_INT_FLOAT_CMP_OP (==) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1290 OCTAVE_INT_FLOAT_CMP_OP (!=) |
66bc6f9b4f72
rewrite integer arithmetics and conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
8104
diff
changeset
|
1291 |
8185
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1292 #undef OCTAVE_INT_FLOAT_CMP_OP |
69c5cce38c29
implement 64-bit arithmetics
Jaroslav Hajek <highegg@gmail.com>
parents:
8169
diff
changeset
|
1293 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1294 template <typename T> |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1295 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1296 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
|
1297 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1298 const T xv = x.value (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1299 const T yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1300 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
|
1301 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1302 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
1303 template <typename T> |
9743
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1304 octave_int<T> |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1305 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
|
1306 { |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1307 const T xv = x.value (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
18017
diff
changeset
|
1308 const T yv = y.value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1309 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
|
1310 } |
26abff55f6fe
optimize bsxfun for common built-in operations
Jaroslav Hajek <highegg@gmail.com>
parents:
9648
diff
changeset
|
1311 |
4902 | 1312 #endif |