Mercurial > octave
view libinterp/operators/op-mi.cc @ 32626:395ab2ffb0c2
maint: Use constexpr where possible for performance.
* libinterp/corefcn/__betainc__.cc, libinterp/corefcn/__expint__.cc,
libinterp/corefcn/__gammainc__.cc, libinterp/corefcn/dlmread.cc,
libinterp/corefcn/gl2ps-print.cc, libinterp/corefcn/graphics.cc,
libinterp/corefcn/input.cc, libinterp/corefcn/oct-stream.cc,
libinterp/corefcn/quadcc.cc, libinterp/corefcn/xpow.cc,
libinterp/octave-value/ov-base-int.cc, libinterp/octave-value/ov-base.cc,
libinterp/octave-value/ov.cc, libinterp/operators/op-mi.cc,
liboctave/array/Array-base.cc, liboctave/array/Sparse.cc,
liboctave/array/idx-vector.cc, liboctave/numeric/lo-mappers.cc,
liboctave/util/oct-inttypes.cc, liboctave/util/oct-inttypes.h,
liboctave/util/oct-string.cc:
Use "static constexpr" rather than "static const" where possible so that
compiler may move some optimizations into compile stage, rather than runtime
initialization. Capitalize constant expression names.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 20 Dec 2023 20:02:42 -0800 |
parents | a9c8b1f8fb32 |
children | 4b601ca024d5 |
line wrap: on
line source
//////////////////////////////////////////////////////////////////////// // // Copyright (C) 2020-2023 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 <iostream> #include "errwarn.h" #include "ops.h" #include "ov-magic-int.h" #include "ov-typeinfo.h" #include "ov.h" OCTAVE_BEGIN_NAMESPACE(octave) // Magic integer unary ops. Only + and - are allowed so that // expressions like // // int64 (-9007199254740994) // // produce proper int64 constants. static octave_value oct_unop_unsigned_uplus (const octave_base_value& a) { OCTAVE_CAST_BASE_VALUE (const octave_magic_uint&, v, a); // no-op. // FIXME: but can we do this just by incrementing the reference count? return octave_value (v.clone ()); } static octave_value oct_unop_unsigned_uminus (const octave_base_value& a) { OCTAVE_CAST_BASE_VALUE (const octave_magic_uint&, v, a); // We are storing a uint64 value, so some fakery is needed here. // Is there a better way? // FIXME: Maybe there should also be octave_magic_int::as_TYPE_value // functions? octave_uint64 val (v.scalar_ref ()); uint64_t ival = val.value (); static constexpr uint64_t max_val = static_cast<uint64_t> (std::numeric_limits<int64_t>::max ()); static constexpr uint64_t max_val_p1 = max_val + 1; if (ival <= max_val) { int64_t signed_ival = ival; return octave_value (new octave_magic_int (-signed_ival)); } if (ival == max_val_p1) { // Correctly capture intmin. For example, negating uint8(128) // should return int8(-128) but converting directly to int8 and // negating will not return the correct result. static constexpr int64_t min_signed_ival = std::numeric_limits<int64_t>::min (); return octave_value (new octave_magic_int (min_signed_ival)); } return octave_value (-static_cast<double> (ival)); } static octave_value oct_unop_signed_uplus (const octave_base_value& a) { OCTAVE_CAST_BASE_VALUE (const octave_magic_int&, v, a); // no-op. // FIXME: but can we do this just by incrementing the reference count? return octave_value (v.clone ()); } static octave_value oct_unop_signed_uminus (const octave_base_value& a) { OCTAVE_CAST_BASE_VALUE (const octave_magic_int&, v, a); // FIXME: Maybe there should also be octave_magic_int::as_TYPE_value // functions? octave_int64 val (v.scalar_ref ()); return octave_value (new octave_magic_int (-val)); } void install_mi_ops (octave::type_info& ti) { INSTALL_UNOP_TI (ti, op_uplus, octave_magic_uint, unsigned_uplus); INSTALL_UNOP_TI (ti, op_uminus, octave_magic_uint, unsigned_uminus); INSTALL_UNOP_TI (ti, op_uplus, octave_magic_int, signed_uplus); INSTALL_UNOP_TI (ti, op_uminus, octave_magic_int, signed_uminus); } OCTAVE_END_NAMESPACE(octave)