Mercurial > octave
view liboctave/util/oct-inttypes.cc @ 31605:e88a07dec498 stable
maint: Use macros to begin/end C++ namespaces.
* oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE,
OCTAVE_END_NAMESPACE) that can be used to start/end a namespace.
* mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc,
__dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc,
__ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc,
__magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc,
auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h,
besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h,
call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc,
colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc,
defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc,
display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc,
ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h,
event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc,
fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc,
getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc,
gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc,
graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc,
help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h,
interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h,
inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc,
latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h,
lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc,
ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h,
mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc,
oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h,
oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h,
oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h,
oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy,
ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc,
pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc,
qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h,
sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc,
sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h,
stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc,
symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h,
symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h,
text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc,
toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc,
url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc,
variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h,
__delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc,
__init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc,
audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh,
mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc,
cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h,
cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h,
cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc,
ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc,
ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h,
ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc,
ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h,
ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, 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-fcdm-fcdm.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-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-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc,
op-m-scm.cc, op-m-sm.cc, op-mi.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, ops.h,
anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h,
comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc,
oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h,
pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h,
pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h,
pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h,
pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h,
pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc,
pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc,
pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc,
pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc,
pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc,
pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc,
pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h,
pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h,
range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc,
chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h,
lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h,
oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h,
oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc,
qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc,
randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc,
sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc,
child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc,
file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h,
mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h,
oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc,
oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h,
base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h,
file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc,
lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h,
oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h,
oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h,
oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h,
pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h,
url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 01 Dec 2022 14:23:45 -0800 |
parents | 9978ef12aea3 |
children | 597f3ee61a48 |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 2004-2022 The Octave Project Developers // // See the file COPYRIGHT.md in the top-level directory of this // distribution or <https://octave.org/copyright/>. // // 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 // <https://www.gnu.org/licenses/>. // //////////////////////////////////////////////////////////////////////// #if defined (HAVE_CONFIG_H) # include "config.h" #endif #include "fpucw-wrappers.h" #include "lo-error.h" #include "oct-inttypes.h" template <typename T> const octave_int<T> octave_int<T>::s_zero (static_cast<T> (0)); template <typename T> const octave_int<T> octave_int<T>::s_one (static_cast<T> (1)); // Define type names. #define DEFINE_OCTAVE_INT_TYPENAME(TYPE, TYPENAME) \ template <> \ OCTAVE_API const char * \ octave_int<TYPE>::type_name (void) { return TYPENAME; } DEFINE_OCTAVE_INT_TYPENAME (int8_t, "int8") DEFINE_OCTAVE_INT_TYPENAME (int16_t, "int16") DEFINE_OCTAVE_INT_TYPENAME (int32_t, "int32") DEFINE_OCTAVE_INT_TYPENAME (int64_t, "int64") DEFINE_OCTAVE_INT_TYPENAME (uint8_t, "uint8") DEFINE_OCTAVE_INT_TYPENAME (uint16_t, "uint16") DEFINE_OCTAVE_INT_TYPENAME (uint32_t, "uint32") DEFINE_OCTAVE_INT_TYPENAME (uint64_t, "uint64") template <class T> template <class S> T octave_int_base<T>::convert_real (const S& value) { // Compute proper thresholds. static const S thmin = compute_threshold (static_cast<S> (min_val ()), min_val ()); static const S thmax = compute_threshold (static_cast<S> (max_val ()), max_val ()); if (octave::math::isnan (value)) return static_cast<T> (0); else if (value < thmin) return min_val (); else if (value > thmax) return max_val (); else { S rvalue = octave::math::round (value); return static_cast<T> (rvalue); } } #define INSTANTIATE_CONVERT_REAL_1(T, S) \ template \ OCTAVE_API \ T \ octave_int_base<T>::convert_real (const S&) #define INSTANTIATE_CONVERT_REAL(S) \ INSTANTIATE_CONVERT_REAL_1 (int8_t, S); \ INSTANTIATE_CONVERT_REAL_1 (uint8_t, S); \ INSTANTIATE_CONVERT_REAL_1 (int16_t, S); \ INSTANTIATE_CONVERT_REAL_1 (uint16_t, S); \ INSTANTIATE_CONVERT_REAL_1 (int32_t, S); \ INSTANTIATE_CONVERT_REAL_1 (uint32_t, S); \ INSTANTIATE_CONVERT_REAL_1 (int64_t, S); \ INSTANTIATE_CONVERT_REAL_1 (uint64_t, S) INSTANTIATE_CONVERT_REAL (double); INSTANTIATE_CONVERT_REAL (float); #if defined (OCTAVE_INT_USE_LONG_DOUBLE) INSTANTIATE_CONVERT_REAL (long double); #endif #if defined (OCTAVE_INT_USE_LONG_DOUBLE) # if defined (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) \ { \ unsigned int oldcw = octave_begin_long_double_rounding (); \ \ bool retval = xop::op (static_cast<long double> (x), \ static_cast<long double> (y)); \ \ octave_end_long_double_rounding (oldcw); \ \ return retval; \ } \ \ template <typename xop> \ bool \ octave_int_cmp_op::external_mop (T x, double y) \ { \ unsigned int oldcw = octave_begin_long_double_rounding (); \ \ bool retval = xop::op (static_cast<long double> (x), \ static_cast<long double> (y)); \ \ octave_end_long_double_rounding (oldcw); \ \ 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) { unsigned int oldcw = octave_begin_long_double_rounding (); uint64_t retval = octave_int_arith_base<uint64_t, false>::mul_internal (x, y); octave_end_long_double_rounding (oldcw); return retval; } int64_t octave_external_int64_int64_mul (int64_t x, int64_t y) { unsigned int oldcw = octave_begin_long_double_rounding (); int64_t retval = octave_int_arith_base<int64_t, true>::mul_internal (x, y); octave_end_long_double_rounding (oldcw); 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 DEFINE_OCTAVE_LONG_DOUBLE_OP(T, OP, NAME) \ T \ external_double_ ## T ## _ ## NAME (double x, T y) \ { \ unsigned int oldcw = octave_begin_long_double_rounding (); \ \ T retval = T (x OP static_cast<long double> (y.value ())); \ \ octave_end_long_double_rounding (oldcw); \ \ return retval; \ } \ \ T \ external_ ## T ## _double_ ## NAME (T x, double y) \ { \ unsigned int oldcw = octave_begin_long_double_rounding (); \ \ T retval = T (static_cast<long double> (x.value ()) OP y); \ \ octave_end_long_double_rounding (oldcw); \ \ return retval; \ } # define DEFINE_OCTAVE_LONG_DOUBLE_OPS(T) \ DEFINE_OCTAVE_LONG_DOUBLE_OP (T, +, add); \ DEFINE_OCTAVE_LONG_DOUBLE_OP (T, -, sub); \ DEFINE_OCTAVE_LONG_DOUBLE_OP (T, *, mul); \ DEFINE_OCTAVE_LONG_DOUBLE_OP (T, /, div) DEFINE_OCTAVE_LONG_DOUBLE_OPS (octave_int64); DEFINE_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) { // The following cast changes the value to 2^64 (which is outside the range // of `uint64_t`). Take care to handle this correctly (e.g., don't cast back // to `uint64_t`)! static const double xxup = static_cast<double> (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) { // The following cast changes the value to 2^63 (which is outside the range // of `int64_t`). Take care to handle this correctly (e.g., don't cast back // to `int64_t`)! The same applies to the lower limit on systems using one's // complement. static const double xxup = static_cast<double> (std::numeric_limits<int64_t>::max ()); static const double xxlo = static_cast<double> (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 (u)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. // But first, avoid overflow in computation of abs (min_val ()). if (x == min_val ()) return y == 0 ? 0 : (y < 0 ? max_val () : min_val ()); if (y == min_val ()) return x == 0 ? 0 : (x < 0 ? max_val () : min_val ()); 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 (); } template <> OCTAVE_API octave_uint64 operator + (const octave_uint64& x, const double& y) { return (y < 0) ? x - octave_uint64 (-y) : x + octave_uint64 (y); } template <> OCTAVE_API octave_uint64 operator + (const double& x, const octave_uint64& y) { return y + x; } template <> OCTAVE_API octave_int64 operator + (const octave_int64& x, const double& y) { // The following cast changes the value to 2^63 (which is outside the range // of `int64_t`). 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; } } template <> OCTAVE_API octave_int64 operator + (const double& x, const octave_int64& y) { return y + x; } template <> OCTAVE_API octave_uint64 operator - (const octave_uint64& x, const double& y) { return x + (-y); } template <> OCTAVE_API octave_uint64 operator - (const double& x, const octave_uint64& y) { // The following cast changes the value to 2^64 (which is outside the range // of `uint64_t`). 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); } } template <> OCTAVE_API octave_int64 operator - (const octave_int64& x, const double& y) { return x + (-y); } template <> OCTAVE_API octave_int64 operator - (const double& x, const octave_int64& y) { 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 = octave::math::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; } template <> OCTAVE_API octave_uint64 operator * (const octave_uint64& x, const double& y) { // The following cast changes the value to 2^64 (which is outside the range // of `uint64_t`). if (y >= 0 && y < static_cast<double> (octave_uint64::max ()) && y == octave::math::fix (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 || octave::math::isnan (y) || octave::math::isinf (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::s_zero; for (short i = 0; i < 4; i++) { res += octave_uint64 (dbleget (sign, w[i], e)); e += 32; } return res; } } template <> OCTAVE_API octave_uint64 operator * (const double& x, const octave_uint64& y) { return y * x; } template <> OCTAVE_API octave_int64 operator * (const octave_int64& x, const double& y) { // The following cast changes the value to 2^63 (which is outside the range // of `int64_t`). if (fabs (y) < static_cast<double> (octave_int64::max ()) && y == octave::math::fix (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 (octave::math::isnan (y) || octave::math::isinf (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::s_zero; for (short i = 0; i < 4; i++) { res += octave_int64 (dbleget (sign, w[i], e)); e += 32; } return res; } } template <> OCTAVE_API octave_int64 operator * (const double& x, const octave_int64& y) { return y * x; } template <> OCTAVE_API octave_uint64 operator / (const double& x, const octave_uint64& y) { return octave_uint64 (x / static_cast<double> (y)); } template <> OCTAVE_API octave_int64 operator / (const double& x, const octave_int64& y) { return octave_int64 (x / static_cast<double> (y)); } template <> OCTAVE_API octave_uint64 operator / (const octave_uint64& x, const double& y) { // The following cast changes the value to 2^64 (which is outside the range // of `uint64_t`). if (y >= 0 && y < static_cast<double> (octave_uint64::max ()) && y == octave::math::fix (y)) return x / octave_uint64 (y); else return x * (1.0/y); } template <> OCTAVE_API octave_int64 operator / (const octave_int64& x, const double& y) { // The following cast changes the value to 2^63 (which is outside the range // of `int64_t`). if (fabs (y) < static_cast<double> (octave_int64::max ()) && y == octave::math::fix (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; const octave_int<T> zero = octave_int<T>::s_zero; const octave_int<T> one = octave_int<T>::s_one; 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> (std::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 == octave::math::fix (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (std::pow (a.double_value (), b))); } template <typename T> octave_int<T> pow (const float& a, const octave_int<T>& b) { return octave_int<T> (std::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 == octave::math::fix (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (std::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 == octave::math::fix (b)) ? pow (a, octave_int<T> (static_cast<T> (b))) : octave_int<T> (std::pow (a.double_value (), static_cast<double> (b)))); } #define INSTANTIATE_INTTYPE(T) \ template class OCTAVE_CLASS_TEMPLATE_INSTANTIATION_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 <54382> (uint8 (char (128)), uint8 (128)) %!assert <54382> (uint8 (char (255)), uint8 (255)) %!assert <54382> (int8 (char (128)), int8 (128)) %!assert <54382> (int8 (char (255)), int8 (255)) %!assert <54382> (uint16 (char (128)), uint16 (128)) %!assert <54382> (uint16 (char (255)), uint16 (255)) %!assert <54382> (int16 (char (128)), int16 (128)) %!assert <54382> (int16 (char (255)), int16 (255)) %!assert <54382> (uint32 (char (128)), uint32 (128)) %!assert <54382> (uint32 (char (255)), uint32 (255)) %!assert <54382> (int32 (char (128)), int32 (128)) %!assert <54382> (int32 (char (255)), int32 (255)) %!assert <54382> (uint64 (char (128)), uint64 (128)) %!assert <54382> (uint64 (char (255)), uint64 (255)) %!assert <54382> (int64 (char (128)), int64 (128)) %!assert <54382> (int64 (char (255)), int64 (255)) */