Mercurial > octave
view liboctave/util/oct-inttypes.cc @ 21301:40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
* mk-octave-config-h.sh, mk-opts.pl, Backend.cc, BaseControl.cc,
ButtonControl.cc, Canvas.cc, CheckBoxControl.cc, Container.cc, ContextMenu.cc,
EditControl.cc, Figure.cc, FigureWindow.cc, GLCanvas.cc, KeyMap.cc,
ListBoxControl.cc, Logger.cc, Menu.cc, MouseModeActionGroup.cc, Object.cc,
ObjectFactory.cc, ObjectProxy.cc, Panel.cc, PopupMenuControl.cc,
PushButtonControl.cc, PushTool.cc, QtHandlesUtils.cc, RadioButtonControl.cc,
SliderControl.cc, TextControl.cc, TextEdit.cc, ToggleButtonControl.cc,
ToggleTool.cc, ToolBar.cc, ToolBarButton.cc, __init_qt__.cc,
annotation-dialog.cc, gl-select.cc, module.mk, kpty.cpp, color-picker.cc,
dialog.cc, documentation-dock-widget.cc, files-dock-widget.cc,
find-files-dialog.cc, find-files-model.cc, history-dock-widget.cc,
file-editor-tab.cc, file-editor-tab.h, file-editor.cc, find-dialog.cc,
marker.cc, octave-qscintilla.cc, octave-txt-lexer.cc, main-window.cc,
octave-cmd.cc, octave-dock-widget.cc, octave-gui.cc, octave-interpreter.cc,
octave-qt-link.cc, parser.cc, webinfo.cc, resource-manager.cc,
settings-dialog.cc, shortcut-manager.cc, terminal-dock-widget.cc,
thread-manager.cc, welcome-wizard.cc, workspace-model.cc, workspace-view.cc,
build-env-features.sh, build-env.in.cc, Cell.cc, __contourc__.cc,
__dispatch__.cc, __dsearchn__.cc, __ichol__.cc, __ilu__.cc, __lin_interpn__.cc,
__pchip_deriv__.cc, __qp__.cc, balance.cc, besselj.cc, betainc.cc, bitfcns.cc,
bsxfun.cc, c-file-ptr-stream.cc, cdisplay.c, cellfun.cc, coct-hdf5-types.c,
colloc.cc, comment-list.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc,
debug.cc, defaults.cc, defun.cc, det.cc, dirfns.cc, display.cc, dlmread.cc,
dot.cc, dynamic-ld.cc, eig.cc, ellipj.cc, error.cc, errwarn.cc, event-queue.cc,
fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc,
gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc,
gl-render.cc, gl2ps-print.cc, graphics.cc, gripes.cc, hash.cc, help.cc,
hess.cc, hex2num.cc, hook-fcn.cc, input.cc, inv.cc, jit-ir.cc, jit-typeinfo.cc,
jit-util.cc, kron.cc, load-path.cc, load-save.cc, lookup.cc,
ls-ascii-helper.cc, ls-hdf5.cc, ls-mat-ascii.cc, ls-mat4.cc, ls-mat5.cc,
ls-oct-binary.cc, ls-oct-text.cc, ls-utils.cc, lsode.cc, lu.cc, luinc.cc,
mappers.cc, matrix_type.cc, max.cc, mex.cc, mgorth.cc, nproc.cc,
oct-errno.in.cc, oct-fstrm.cc, oct-hdf5-types.cc, oct-hist.cc, oct-iostrm.cc,
oct-lvalue.cc, oct-map.cc, oct-prcstrm.cc, oct-procbuf.cc, oct-stream.cc,
oct-strstrm.cc, oct-tex-lexer.in.ll, oct-tex-parser.in.yy, octave-link.cc,
ordschur.cc, pager.cc, pinv.cc, pr-output.cc, procstream.cc, profiler.cc,
psi.cc, pt-jit.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc,
schur.cc, sighandlers.cc, siglist.c, sparse-xdiv.cc, sparse-xpow.cc, sparse.cc,
spparms.cc, sqrtm.cc, str2double.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc,
sylvester.cc, symtab.cc, syscalls.cc, sysdep.cc, text-renderer.cc, time.cc,
toplev.cc, tril.cc, tsearch.cc, txt-eng.cc, typecast.cc, urlwrite.cc, utils.cc,
variables.cc, xdiv.cc, xgl2ps.c, xnorm.cc, xpow.cc, zfstream.cc,
__delaunayn__.cc, __eigs__.cc, __fltk_uigetfile__.cc, __glpk__.cc,
__init_fltk__.cc, __init_gnuplot__.cc, __magick_read__.cc, __osmesa_print__.cc,
__voronoi__.cc, amd.cc, audiodevinfo.cc, audioread.cc, ccolamd.cc, chol.cc,
colamd.cc, convhulln.cc, dmperm.cc, fftw.cc, qr.cc, symbfact.cc, symrcm.cc,
mkbuiltins, mkops, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc,
ov-base-scalar.cc, ov-base-sparse.cc, ov-base.cc, ov-bool-mat.cc,
ov-bool-sparse.cc, ov-bool.cc, ov-builtin.cc, ov-cell.cc, ov-ch-mat.cc,
ov-class.cc, ov-classdef.cc, ov-colon.cc, ov-complex.cc, ov-cs-list.cc,
ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-fcn-handle.cc,
ov-fcn-inline.cc, ov-fcn.cc, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc,
ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-int16.cc,
ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-java.cc, ov-lazy-idx.cc,
ov-mex-fcn.cc, ov-null-mat.cc, ov-oncleanup.cc, ov-perm.cc, ov-range.cc,
ov-re-diag.cc, ov-re-mat.cc, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc,
ov-struct.cc, ov-typeinfo.cc, ov-uint16.cc, ov-uint32.cc, ov-uint64.cc,
ov-uint8.cc, ov-usr-fcn.cc, ov.cc, ovl.cc, octave.cc, op-b-b.cc, op-b-bm.cc,
op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc,
op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc,
op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc,
op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc,
op-dm-template.cc, op-dms-template.cc, op-double-conv.cc, op-fcdm-fcdm.cc,
op-fcdm-fdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc,
op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc,
op-fdm-fdm.cc, op-float-conv.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc,
op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc,
op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc,
op-int-conv.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc,
op-m-sm.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc,
op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc,
op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc,
op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc,
op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc,
op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc,
op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, lex.ll, oct-parse.in.yy,
pt-arg-list.cc, pt-array-list.cc, pt-assign.cc, pt-binop.cc, pt-bp.cc,
pt-cbinop.cc, pt-cell.cc, pt-check.cc, pt-classdef.cc, pt-cmd.cc, pt-colon.cc,
pt-const.cc, pt-decl.cc, pt-eval.cc, pt-except.cc, pt-exp.cc, pt-fcn-handle.cc,
pt-funcall.cc, pt-id.cc, pt-idx.cc, pt-jump.cc, pt-loop.cc, pt-mat.cc,
pt-misc.cc, pt-pr-code.cc, pt-select.cc, pt-stmt.cc, pt-unop.cc, pt.cc,
token.cc, Array-jit.cc, Array-os.cc, Array-sym.cc, Array-tc.cc, version.cc,
Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc, Array-f.cc, Array-fC.cc,
Array-i.cc, Array-idx-vec.cc, Array-s.cc, Array-str.cc, Array-util.cc,
Array-voidp.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc,
CNDArray.cc, CRowVector.cc, CSparse.cc, DiagArray2.cc, MArray-C.cc,
MArray-d.cc, MArray-f.cc, MArray-fC.cc, MArray-i.cc, MArray-s.cc, MArray.cc,
MDiagArray2.cc, MSparse-C.cc, MSparse-d.cc, MatrixType.cc, PermMatrix.cc,
Range.cc, Sparse-C.cc, Sparse-b.cc, Sparse-d.cc, Sparse.cc, boolMatrix.cc,
boolNDArray.cc, boolSparse.cc, chMatrix.cc, chNDArray.cc, dColVector.cc,
dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc, dSparse.cc,
dim-vector.cc, fCColVector.cc, fCDiagMatrix.cc, fCMatrix.cc, fCNDArray.cc,
fCRowVector.cc, fColVector.cc, fDiagMatrix.cc, fMatrix.cc, fNDArray.cc,
fRowVector.cc, idx-vector.cc, int16NDArray.cc, int32NDArray.cc,
int64NDArray.cc, int8NDArray.cc, intNDArray.cc, uint16NDArray.cc,
uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc, blaswrap.c, cquit.c,
f77-extern.cc, f77-fcn.c, lo-error.c, quit.cc, CollocWt.cc, DASPK.cc, DASRT.cc,
DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, aepbalance.cc, chol.cc,
eigs-base.cc, fEIG.cc, gepbalance.cc, hess.cc, lo-mappers.cc, lo-specfun.cc,
lu.cc, oct-convn.cc, oct-fftw.cc, oct-norm.cc, oct-rand.cc, oct-spparms.cc,
qr.cc, qrp.cc, randgamma.c, randmtzig.c, randpoisson.c, schur.cc,
sparse-chol.cc, sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, svd.cc,
mk-ops.awk, dir-ops.cc, file-ops.cc, file-stat.cc, lo-sysdep.cc, mach-info.cc,
oct-env.cc, oct-group.cc, oct-passwd.cc, oct-syscalls.cc, oct-time.cc,
oct-uname.cc, cmd-edit.cc, cmd-hist.cc, data-conv.cc, f2c-main.c,
glob-match.cc, kpse.cc, lo-array-errwarn.cc, lo-array-gripes.cc, lo-cutils.c,
lo-ieee.cc, lo-regexp.cc, lo-utils.cc, oct-base64.cc, oct-glob.cc,
oct-inttypes.cc, oct-locbuf.cc, oct-mutex.cc, oct-rl-edit.c, oct-rl-hist.c,
oct-shlib.cc, oct-sort.cc, pathsearch.cc, singleton-cleanup.cc, sparse-sort.cc,
sparse-util.cc, str-vec.cc, unwind-prot.cc, url-transfer.cc,
display-available.c, main-cli.cc, main-gui.cc, main.in.cc, mkoctfile.in.cc,
octave-config.in.cc:
Use '#include "config.h"' rather than <config.h>.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 18 Feb 2016 13:34:50 -0800 |
parents | f7121e111991 |
children | 8376de2eaf00 |
line wrap: on
line source
/* Copyright (C) 2004-2015 John W. Eaton Copyright (C) 2008-2009 Jaroslav Hajek This file is part of Octave. Octave is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. Octave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Octave; see the file COPYING. If not, see <http://www.gnu.org/licenses/>. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include <fpucw.h> #include "lo-error.h" #include "oct-inttypes.h" template <typename T> const octave_int<T> octave_int<T>::zero (static_cast<T> (0)); template <typename T> const octave_int<T> octave_int<T>::one (static_cast<T> (1)); // define type names. #define DECLARE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ template <> \ OCTAVE_API const char * \ octave_int<TYPE>::type_name () { return TYPENAME; } DECLARE_OCTAVE_INT_TYPENAME (int8_t, "int8") DECLARE_OCTAVE_INT_TYPENAME (int16_t, "int16") DECLARE_OCTAVE_INT_TYPENAME (int32_t, "int32") DECLARE_OCTAVE_INT_TYPENAME (int64_t, "int64") DECLARE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") DECLARE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") DECLARE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") DECLARE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") #ifdef OCTAVE_INT_USE_LONG_DOUBLE #ifdef OCTAVE_ENSURE_LONG_DOUBLE_OPERATIONS_ARE_NOT_TRUNCATED #define DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES(T) \ template <typename xop> \ bool \ octave_int_cmp_op::external_mop (double x, T y) \ { \ DECL_LONG_DOUBLE_ROUNDING \ \ BEGIN_LONG_DOUBLE_ROUNDING (); \ \ bool retval = xop::op (static_cast<long double> (x), \ static_cast<long double> (y)); \ \ END_LONG_DOUBLE_ROUNDING (); \ \ return retval; \ } \ \ template <typename xop> \ bool \ octave_int_cmp_op::external_mop (T x, double y) \ { \ DECL_LONG_DOUBLE_ROUNDING \ \ BEGIN_LONG_DOUBLE_ROUNDING (); \ \ bool retval = xop::op (static_cast<long double> (x), \ static_cast<long double> (y)); \ \ END_LONG_DOUBLE_ROUNDING (); \ \ return retval; \ } DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (int64_t) DEFINE_OCTAVE_LONG_DOUBLE_CMP_OP_TEMPLATES (uint64_t) #define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP(OP, T) \ template OCTAVE_API bool \ octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (double, T); \ template OCTAVE_API bool \ octave_int_cmp_op::external_mop<octave_int_cmp_op::OP> (T, double) #define INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS(T) \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (lt, T); \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (le, T); \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (gt, T); \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ge, T); \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (eq, T); \ INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OP (ne, T) INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (int64_t); INSTANTIATE_LONG_DOUBLE_LONG_DOUBLE_CMP_OPS (uint64_t); uint64_t octave_external_uint64_uint64_mul (uint64_t x, uint64_t y) { DECL_LONG_DOUBLE_ROUNDING BEGIN_LONG_DOUBLE_ROUNDING (); uint64_t retval = octave_int_arith_base<uint64_t, false>::mul_internal (x, y); END_LONG_DOUBLE_ROUNDING (); return retval; } int64_t octave_external_int64_int64_mul (int64_t x, int64_t y) { DECL_LONG_DOUBLE_ROUNDING BEGIN_LONG_DOUBLE_ROUNDING (); int64_t retval = octave_int_arith_base<int64_t, true>::mul_internal (x, y); END_LONG_DOUBLE_ROUNDING (); return retval; } // Note that if we return long double it is apparently possible for // truncation to happen at the point of storing the result in retval, // which can happen after we end long double rounding. Attempt to avoid // that problem by storing the full precision temporary value in the // integer value before we end the long double rounding mode. // Similarly, the conversion from the 64-bit integer type to long double // must also occur in long double rounding mode. #define OCTAVE_LONG_DOUBLE_OP(T, OP, NAME) \ T \ external_double_ ## T ## _ ## NAME (double x, T y) \ { \ DECL_LONG_DOUBLE_ROUNDING \ \ BEGIN_LONG_DOUBLE_ROUNDING (); \ \ T retval = T (x OP static_cast<long double> (y.value ())); \ \ END_LONG_DOUBLE_ROUNDING (); \ \ return retval; \ } \ \ T \ external_ ## T ## _double_ ## NAME (T x, double y) \ { \ DECL_LONG_DOUBLE_ROUNDING \ \ BEGIN_LONG_DOUBLE_ROUNDING (); \ \ T retval = T (static_cast<long double> (x.value ()) OP y); \ \ END_LONG_DOUBLE_ROUNDING (); \ \ return retval; \ } #define OCTAVE_LONG_DOUBLE_OPS(T) \ OCTAVE_LONG_DOUBLE_OP (T, +, add); \ OCTAVE_LONG_DOUBLE_OP (T, -, sub); \ OCTAVE_LONG_DOUBLE_OP (T, *, mul); \ OCTAVE_LONG_DOUBLE_OP (T, /, div) OCTAVE_LONG_DOUBLE_OPS(octave_int64); OCTAVE_LONG_DOUBLE_OPS(octave_uint64); #endif #else // Define comparison operators template <typename xop> bool octave_int_cmp_op::emulate_mop (uint64_t x, double y) { static const double xxup = std::numeric_limits<uint64_t>::max (); // This converts to the nearest double. Unless there's an equality, the // result is clear. double xx = x; if (xx != y) return xop::op (xx, y); else { // If equality occurred we compare as integers. if (xx == xxup) return xop::gtval; else return xop::op (x, static_cast<uint64_t> (xx)); } } template <typename xop> bool octave_int_cmp_op::emulate_mop (int64_t x, double y) { static const double xxup = std::numeric_limits<int64_t>::max (); static const double xxlo = std::numeric_limits<int64_t>::min (); // This converts to the nearest double. Unless there's an equality, the // result is clear. double xx = x; if (xx != y) return xop::op (xx, y); else { // If equality occurred we compare as integers. if (xx == xxup) return xop::gtval; else if (xx == xxlo) return xop::ltval; else return xop::op (x, static_cast<int64_t> (xx)); } } // We define double-int operations by reverting the operator // A trait class reverting the operator template <typename xop> class rev_op { public: typedef xop op; }; #define DEFINE_REVERTED_OPERATOR(OP1,OP2) \ template <> \ class rev_op<octave_int_cmp_op::OP1> \ { \ public: \ typedef octave_int_cmp_op::OP2 op; \ } DEFINE_REVERTED_OPERATOR(lt,gt); DEFINE_REVERTED_OPERATOR(gt,lt); DEFINE_REVERTED_OPERATOR(le,ge); DEFINE_REVERTED_OPERATOR(ge,le); template <typename xop> bool octave_int_cmp_op::emulate_mop (double x, uint64_t y) { typedef typename rev_op<xop>::op rop; return mop<rop> (y, x); } template <typename xop> bool octave_int_cmp_op::emulate_mop (double x, int64_t y) { typedef typename rev_op<xop>::op rop; return mop<rop> (y, x); } // Define handlers for int64 multiplication template <> uint64_t octave_int_arith_base<uint64_t, false>::mul_internal (uint64_t x, uint64_t y) { // Get upper words uint64_t ux = x >> 32; uint64_t uy = y >> 32; uint64_t res; if (ux) { if (uy) goto overflow; else { uint64_t ly = static_cast<uint32_t> (y); uint64_t uxly = ux*ly; if (uxly >> 32) goto overflow; uxly <<= 32; // never overflows uint64_t lx = static_cast<uint32_t> (x); uint64_t lxly = lx*ly; res = add (uxly, lxly); } } else if (uy) { uint64_t lx = static_cast<uint32_t> (x); uint64_t uylx = uy*lx; if (uylx >> 32) goto overflow; uylx <<= 32; // never overflows uint64_t ly = static_cast<uint32_t> (y); uint64_t lylx = ly*lx; res = add (uylx, lylx); } else { uint64_t lx = static_cast<uint32_t> (x); uint64_t ly = static_cast<uint32_t> (y); res = lx*ly; } return res; overflow: return max_val (); } template <> int64_t octave_int_arith_base<int64_t, true>::mul_internal (int64_t x, int64_t y) { // The signed case is far worse. The problem is that // even if neither integer fits into signed 32-bit range, the result may // still be OK. Uh oh. // Essentially, what we do is compute sign, multiply absolute values // (as above) and impose the sign. // FIXME: can we do something faster if we HAVE_FAST_INT_OPS? uint64_t usx = octave_int_abs (x); uint64_t usy = octave_int_abs (y); bool positive = (x < 0) == (y < 0); // Get upper words uint64_t ux = usx >> 32; uint64_t uy = usy >> 32; uint64_t res; if (ux) { if (uy) goto overflow; else { uint64_t ly = static_cast<uint32_t> (usy); uint64_t uxly = ux*ly; if (uxly >> 32) goto overflow; uxly <<= 32; // never overflows uint64_t lx = static_cast<uint32_t> (usx); uint64_t lxly = lx*ly; res = uxly + lxly; if (res < uxly) goto overflow; } } else if (uy) { uint64_t lx = static_cast<uint32_t> (usx); uint64_t uylx = uy*lx; if (uylx >> 32) goto overflow; uylx <<= 32; // never overflows uint64_t ly = static_cast<uint32_t> (usy); uint64_t lylx = ly*lx; res = uylx + lylx; if (res < uylx) goto overflow; } else { uint64_t lx = static_cast<uint32_t> (usx); uint64_t ly = static_cast<uint32_t> (usy); res = lx*ly; } if (positive) { if (res > static_cast<uint64_t> (max_val ())) { return max_val (); } else return static_cast<int64_t> (res); } else { if (res > static_cast<uint64_t> (-min_val ())) { return min_val (); } else return -static_cast<int64_t> (res); } overflow: return positive ? max_val () : min_val (); } #define INT_DOUBLE_BINOP_DECL(OP,SUFFIX) \ template <> \ OCTAVE_API octave_ ## SUFFIX \ operator OP (const octave_ ## SUFFIX & x, const double& y) #define DOUBLE_INT_BINOP_DECL(OP,SUFFIX) \ template <> \ OCTAVE_API octave_ ## SUFFIX \ operator OP (const double& x, const octave_ ## SUFFIX & y) INT_DOUBLE_BINOP_DECL (+, uint64) { return (y < 0) ? x - octave_uint64 (-y) : x + octave_uint64 (y); } DOUBLE_INT_BINOP_DECL (+, uint64) { return y + x; } INT_DOUBLE_BINOP_DECL (+, int64) { if (fabs (y) < static_cast<double> (octave_int64::max ())) return x + octave_int64 (y); else { // If the number is within the int64 range (the most common case, // probably), the above will work as expected. If not, it's more // complicated - as long as y is within _twice_ the signed range, the // result may still be an integer. An instance of such an operation is // 3*2**62 + (1+intmin ('int64')) that should yield int64 (2**62) + 1. So // what we do is to try to convert y/2 and add it twice. Note that if y/2 // overflows, the result must overflow as well, and that y/2 cannot be a // fractional number. octave_int64 y2 (y / 2); return (x + y2) + y2; } } DOUBLE_INT_BINOP_DECL (+, int64) { return y + x; } INT_DOUBLE_BINOP_DECL (-, uint64) { return x + (-y); } DOUBLE_INT_BINOP_DECL (-, uint64) { if (x <= static_cast<double> (octave_uint64::max ())) return octave_uint64 (x) - y; else { // Again a trick to get the corner cases right. Things like // 3**2**63 - intmax ('uint64') should produce the correct result, i.e. // int64 (2**63) + 1. const double p2_64 = std::pow (2.0, 64); if (y.bool_value ()) { const uint64_t p2_64my = (~y.value ()) + 1; // Equals 2**64 - y return octave_uint64 (x - p2_64) + octave_uint64 (p2_64my); } else return octave_uint64 (p2_64); } } INT_DOUBLE_BINOP_DECL (-, int64) { return x + (-y); } DOUBLE_INT_BINOP_DECL (-, int64) { static const bool twosc = (std::numeric_limits<int64_t>::min () < -std::numeric_limits<int64_t>::max ()); // In case of symmetric integers (not two's complement), this will probably // be eliminated at compile time. if (twosc && y.value () == std::numeric_limits<int64_t>::min ()) { return octave_int64 (x + std::pow (2.0, 63)); } else return x + (-y); } // NOTE: // Emulated mixed multiplications are tricky due to possible precision loss. // Here, after sorting out common cases for speed, we follow the strategy // of converting the double number into the form sign * 64-bit integer * // 2**exponent, multiply the 64-bit integers to get a 128-bit number, split that // number into 32-bit words and form 4 double-valued summands (none of which // loses precision), then convert these into integers and sum them. Though it is // not immediately obvious, this should work even w.r.t. rounding (none of the // summands lose precision). // Multiplies two unsigned 64-bit ints to get a 128-bit number represented // as four 32-bit words. static void umul128 (uint64_t x, uint64_t y, uint32_t w[4]) { uint64_t lx = static_cast<uint32_t> (x); uint64_t ux = x >> 32; uint64_t ly = static_cast<uint32_t> (y); uint64_t uy = y >> 32; uint64_t a = lx * ly; w[0] = a; a >>= 32; uint64_t uxly = ux*ly; uint64_t uylx = uy*lx; a += static_cast<uint32_t> (uxly); uxly >>= 32; a += static_cast<uint32_t> (uylx); uylx >>= 32; w[1] = a; a >>= 32; uint64_t uxuy = ux * uy; a += uxly; a += uylx; a += uxuy; w[2] = a; a >>= 32; w[3] = a; } // Splits a double into bool sign, unsigned 64-bit mantissa and int exponent static void dblesplit (double x, bool& sign, uint64_t& mtis, int& exp) { sign = x < 0; x = fabs (x); x = gnulib::frexp (x, &exp); exp -= 52; mtis = static_cast<uint64_t> (ldexp (x, 52)); } // Gets a double number from a // 32-bit unsigned integer mantissa, exponent, and sign. static double dbleget (bool sign, uint32_t mtis, int exp) { double x = ldexp (static_cast<double> (mtis), exp); return sign ? -x : x; } INT_DOUBLE_BINOP_DECL (*, uint64) { if (y >= 0 && y < octave_uint64::max () && y == xround (y)) { return x * octave_uint64 (static_cast<uint64_t> (y)); } else if (y == 0.5) { return x / octave_uint64 (static_cast<uint64_t> (2)); } else if (y < 0 || xisnan (y) || xisinf (y)) { return octave_uint64 (x.value () * y); } else { bool sign; uint64_t my; int e; dblesplit (y, sign, my, e); uint32_t w[4]; umul128 (x.value (), my, w); octave_uint64 res = octave_uint64::zero; for (short i = 0; i < 4; i++) { res += octave_uint64 (dbleget (sign, w[i], e)); e += 32; } return res; } } DOUBLE_INT_BINOP_DECL (*, uint64) { return y * x; } INT_DOUBLE_BINOP_DECL (*, int64) { if (fabs (y) < octave_int64::max () && y == xround (y)) { return x * octave_int64 (static_cast<int64_t> (y)); } else if (fabs (y) == 0.5) { return x / octave_int64 (static_cast<uint64_t> (4*y)); } else if (xisnan (y) || xisinf (y)) { return octave_int64 (x.value () * y); } else { bool sign; uint64_t my; int e; dblesplit (y, sign, my, e); uint32_t w[4]; sign = (sign != (x.value () < 0)); umul128 (octave_int_abs (x.value ()), my, w); octave_int64 res = octave_int64::zero; for (short i = 0; i < 4; i++) { res += octave_int64 (dbleget (sign, w[i], e)); e += 32; } return res; } } DOUBLE_INT_BINOP_DECL (*, int64) { return y * x; } DOUBLE_INT_BINOP_DECL (/, uint64) { return octave_uint64 (x / static_cast<double> (y)); } DOUBLE_INT_BINOP_DECL (/, int64) { return octave_int64 (x / static_cast<double> (y)); } INT_DOUBLE_BINOP_DECL (/, uint64) { if (y >= 0 && y < octave_uint64::max () && y == xround (y)) { return x / octave_uint64 (y); } else return x * (1.0/y); } INT_DOUBLE_BINOP_DECL (/, int64) { if (fabs (y) < octave_int64::max () && y == xround (y)) { return x / octave_int64 (y); } else return x * (1.0/y); } #define INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP,T1,T2) \ template OCTAVE_API bool \ octave_int_cmp_op::emulate_mop<octave_int_cmp_op::OP> (T1 x, T2 y) #define INSTANTIATE_INT64_DOUBLE_CMP_OP(OP) \ INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, double, int64_t); \ INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, double, uint64_t); \ INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, int64_t, double); \ INSTANTIATE_INT64_DOUBLE_CMP_OP0(OP, uint64_t, double) INSTANTIATE_INT64_DOUBLE_CMP_OP(lt); INSTANTIATE_INT64_DOUBLE_CMP_OP(le); INSTANTIATE_INT64_DOUBLE_CMP_OP(gt); INSTANTIATE_INT64_DOUBLE_CMP_OP(ge); INSTANTIATE_INT64_DOUBLE_CMP_OP(eq); INSTANTIATE_INT64_DOUBLE_CMP_OP(ne); #endif template <typename T> octave_int<T> pow (const octave_int<T>& a, const octave_int<T>& b) { octave_int<T> retval; octave_int<T> zero = static_cast<T> (0); octave_int<T> one = static_cast<T> (1); if (b == zero || a == one) retval = one; else if (b < zero) { if (a == -one) retval = (b.value () % 2) ? a : one; else retval = zero; } else { octave_int<T> a_val = a; T b_val = b; // no need to do saturation on b retval = a; b_val -= 1; while (b_val != 0) { if (b_val & 1) retval = retval * a_val; b_val = b_val >> 1; if (b_val) a_val = a_val * a_val; } } return retval; } template <typename T> octave_int<T> pow (const double& a, const octave_int<T>& b) { return octave_int<T> (pow (a, b.double_value ())); } template <typename T> octave_int<T> pow (const octave_int<T>& a, const double& b) { return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (pow (a.double_value (), b))); } template <typename T> octave_int<T> pow (const float& a, const octave_int<T>& b) { return octave_int<T> (pow (a, b.float_value ())); } template <typename T> octave_int<T> pow (const octave_int<T>& a, const float& b) { return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (pow (a.double_value (), static_cast<double> (b)))); } // FIXME: Do we really need a differently named single-precision // function integer power function here instead of an overloaded // one? template <typename T> octave_int<T> powf (const float& a, const octave_int<T>& b) { return octave_int<T> (pow (a, b.float_value ())); } template <typename T> octave_int<T> powf (const octave_int<T>& a, const float& b) { return ((b >= 0 && b < std::numeric_limits<T>::digits && b == xround (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (pow (a.double_value (), static_cast<double> (b)))); } #define INSTANTIATE_INTTYPE(T) \ template class OCTAVE_API octave_int<T>; \ template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const octave_int<T>&); \ template OCTAVE_API octave_int<T> pow (const double&, const octave_int<T>&); \ template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const double&); \ template OCTAVE_API octave_int<T> pow (const float&, const octave_int<T>&); \ template OCTAVE_API octave_int<T> pow (const octave_int<T>&, const float&); \ template OCTAVE_API octave_int<T> powf (const float&, const octave_int<T>&); \ template OCTAVE_API octave_int<T> powf (const octave_int<T>&, const float&); \ template OCTAVE_API octave_int<T> \ bitshift (const octave_int<T>&, int, const octave_int<T>&); INSTANTIATE_INTTYPE (int8_t); INSTANTIATE_INTTYPE (int16_t); INSTANTIATE_INTTYPE (int32_t); INSTANTIATE_INTTYPE (int64_t); INSTANTIATE_INTTYPE (uint8_t); INSTANTIATE_INTTYPE (uint16_t); INSTANTIATE_INTTYPE (uint32_t); INSTANTIATE_INTTYPE (uint64_t); /* %!assert (intmax ("int64") / intmin ("int64"), int64 (-1)) %!assert (intmin ("int64") / int64 (-1), intmax ("int64")) %!assert (int64 (2**63), intmax ("int64")) %!assert (uint64 (2**64), intmax ("uint64")) %!test %! a = 1.9*2^61; b = uint64 (a); b++; assert (b > a); %!test %! a = -1.9*2^61; b = int64 (a); b++; assert (b > a); %!test %! a = int64 (-2**60) + 2; assert (1.25*a == (5*a)/4); %!test %! a = uint64 (2**61) + 2; assert (1.25*a == (5*a)/4); %!assert (int32 (2**31+0.5), intmax ("int32")) %!assert (int32 (-2**31-0.5), intmin ("int32")) %!assert ((int64 (2**62)+1)**1, int64 (2**62)+1) %!assert ((int64 (2**30)+1)**2, int64 (2**60+2**31) + 1) %!assert (uint8 (char (128)), uint8 (128)); %!assert (uint8 (char (255)), uint8 (255)); %!assert (int8 (char (128)), int8 (128)); %!assert (int8 (char (255)), int8 (255)); %!assert (uint16 (char (128)), uint16 (128)); %!assert (uint16 (char (255)), uint16 (255)); %!assert (int16 (char (128)), int16 (128)); %!assert (int16 (char (255)), int16 (255)); %!assert (uint32 (char (128)), uint32 (128)); %!assert (uint32 (char (255)), uint32 (255)); %!assert (int32 (char (128)), int32 (128)); %!assert (int32 (char (255)), int32 (255)); %!assert (uint64 (char (128)), uint64 (128)); %!assert (uint64 (char (255)), uint64 (255)); %!assert (int64 (char (128)), int64 (128)); %!assert (int64 (char (255)), int64 (255)); */