Skip to content

Commit

Permalink
maint: Use constexpr where possible for performance.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Rik committed Dec 21, 2023
1 parent f91b257 commit 2d50f10
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 83 deletions.
4 changes: 2 additions & 2 deletions libinterp/corefcn/__betainc__.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Continued fraction for incomplete beta function.

// Initialize variables used in algorithm
static const float tiny = math::exp2 (-50.0f);
static const float eps = std::numeric_limits<float>::epsilon ();
static constexpr float eps = std::numeric_limits<float>::epsilon ();
float xj, x2, y, Cj, Dj, aj, bj, Deltaj, alpha_j, beta_j;
int j, maxit;
maxit = 200;
Expand Down Expand Up @@ -150,7 +150,7 @@ Continued fraction for incomplete beta function.

// Initialize variables used in algorithm
static const double tiny = math::exp2 (-100.0);
static const double eps = std::numeric_limits<double>::epsilon ();
static constexpr double eps = std::numeric_limits<double>::epsilon ();
double xj, x2, y, Cj, Dj, aj, bj, Deltaj, alpha_j, beta_j;
int j, maxit;
maxit = 200;
Expand Down
4 changes: 2 additions & 2 deletions libinterp/corefcn/__expint__.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Continued fraction expansion for the exponential integral.

// Initialize variables used in algorithm
static const FloatComplex tiny = math::exp2 (-50.0f);
static const float eps = std::numeric_limits<float>::epsilon ();
static constexpr float eps = std::numeric_limits<float>::epsilon ();
const FloatComplex cone (1.0, 0.0);
const FloatComplex czero (0.0, 0.0);
const int maxit = 100;
Expand Down Expand Up @@ -123,7 +123,7 @@ Continued fraction expansion for the exponential integral.

// Initialize variables used in algorithm
static const Complex tiny = math::exp2 (-100.0);
static const double eps = std::numeric_limits<double>::epsilon ();
static constexpr double eps = std::numeric_limits<double>::epsilon ();
const Complex cone (1.0, 0.0);
const Complex czero (0.0, 0.0);
const int maxit = 200;
Expand Down
4 changes: 2 additions & 2 deletions libinterp/corefcn/__gammainc__.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Continued fraction for incomplete gamma function.

// Initialize variables used in algorithm
static const float tiny = math::exp2 (-50.0f);
static const float eps = std::numeric_limits<float>::epsilon();
static constexpr float eps = std::numeric_limits<float>::epsilon();
float y, Cj, Dj, bj, aj, Deltaj;
int j, maxit;
maxit = 200;
Expand Down Expand Up @@ -126,7 +126,7 @@ Continued fraction for incomplete gamma function.

// Initialize variables used in algorithm
static const double tiny = math::exp2 (-100.0);
static const double eps = std::numeric_limits<double>::epsilon();
static constexpr double eps = std::numeric_limits<double>::epsilon();
double y, Cj, Dj, bj, aj, Deltaj;
int j, maxit;
maxit = 200;
Expand Down
4 changes: 2 additions & 2 deletions libinterp/corefcn/dlmread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
#include "ovl.h"
#include "utils.h"

static const octave_idx_type idx_max
static constexpr octave_idx_type idx_max
= std::numeric_limits<octave_idx_type>::max () - 1;

static const double idx_max_dbl = double (idx_max);
static constexpr double idx_max_dbl = double (idx_max);

static bool
read_cell_spec (std::istream& is, octave_idx_type& row, octave_idx_type& col)
Expand Down
8 changes: 4 additions & 4 deletions libinterp/corefcn/gl2ps-print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1522,10 +1522,10 @@ gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data)

OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);

static const float maxval = std::numeric_limits<uint8_t>::max ();
static constexpr float MAXVAL = std::numeric_limits<uint8_t>::max ();

for (int i = 0; i < 3*w*h; i++)
tmp_data[i] = data[i] / maxval;
tmp_data[i] = data[i] / MAXVAL;

draw_pixels (w, h, tmp_data);
}
Expand All @@ -1537,10 +1537,10 @@ gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data)

OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h);

static const float maxval = std::numeric_limits<uint16_t>::max ();
static constexpr float MAXVAL = std::numeric_limits<uint16_t>::max ();

for (int i = 0; i < 3*w*h; i++)
tmp_data[i] = data[i] / maxval;
tmp_data[i] = data[i] / MAXVAL;

draw_pixels (w, h, tmp_data);
}
Expand Down
14 changes: 7 additions & 7 deletions libinterp/corefcn/graphics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7364,20 +7364,20 @@ axes::properties::calc_tick_sep (double lo, double hi)

magform ((hi - lo) / ticint, a, b);

static const double sqrt_2 = sqrt (2.0);
static const double sqrt_10 = sqrt (10.0);
static const double sqrt_50 = sqrt (50.0);
static constexpr double SQRT_2 = sqrt (2.0);
static constexpr double SQRT_10 = sqrt (10.0);
static constexpr double SQRT_50 = sqrt (50.0);

if (a < sqrt_2)
if (a < SQRT_2)
x = 1;
else if (a < sqrt_10)
else if (a < SQRT_10)
x = 2;
else if (a < sqrt_50)
else if (a < SQRT_50)
x = 5;
else
x = 10;

return x * std::pow (10., b);
return x * std::pow (10.0, b);
}

// Attempt to make "nice" limits from the actual max and min of the data.
Expand Down
4 changes: 2 additions & 2 deletions libinterp/corefcn/input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ static bool
is_completing_dirfns ()
{
static std::string dirfns_commands[] = {"cd", "isfile", "isfolder", "ls"};
static const std::size_t dirfns_commands_length = 4;
static const std::size_t DIRFNS_COMMANDS_LENGTH = 4;

bool retval = false;

std::string line = command_editor::get_line_buffer ();

for (std::size_t i = 0; i < dirfns_commands_length; i++)
for (std::size_t i = 0; i < DIRFNS_COMMANDS_LENGTH; i++)
{
int index = line.find (dirfns_commands[i] + ' ');

Expand Down
5 changes: 2 additions & 3 deletions libinterp/corefcn/oct-stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ get_size (double d, const std::string& who)
::error ("%s: negative value invalid as size specification",
who.c_str ());

static const double out_of_range_top
= static_cast<double> (std::numeric_limits<octave_idx_type>::max ())
+ 1.;
static constexpr double out_of_range_top
= static_cast<double> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;
if (d >= out_of_range_top)
::error ("%s: dimension too large for Octave's index type",
who.c_str ());
Expand Down
2 changes: 1 addition & 1 deletion libinterp/corefcn/quadcc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct cquad_ival
};

// Define relative tolerance used when deciding to drop an interval.
static const double DROP_RELTOL = std::numeric_limits<double>::epsilon () * 10;
static constexpr double DROP_RELTOL = std::numeric_limits<double>::epsilon () * 10;

// Some constants and matrices that we'll need.

Expand Down
5 changes: 3 additions & 2 deletions libinterp/corefcn/xpow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ xisint (T x)
static inline bool
xisint (float x)
{
static const float out_of_range_top
= static_cast<float> (std::numeric_limits<int>::max ()) + 1.;
static constexpr float out_of_range_top
= static_cast<float> (std::numeric_limits<int>::max ()) + 1.0;

return (octave::math::x_nint (x) == x
&& x < out_of_range_top
&& x >= std::numeric_limits<int>::min ());
Expand Down
8 changes: 4 additions & 4 deletions libinterp/octave-value/ov-base-int.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ octave_base_int_matrix<T>::convert_to_str_internal (bool, bool, char type) const

val_type ival = tmp.value ();

static const bool is_signed = std::numeric_limits<val_type>::is_signed;
static const bool can_be_larger_than_uchar_max
static constexpr bool is_signed = std::numeric_limits<val_type>::is_signed;
static constexpr bool can_be_larger_than_uchar_max
= octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max;

if (octave_base_int_helper<val_type, is_signed,
Expand Down Expand Up @@ -578,8 +578,8 @@ octave_base_int_scalar<T>::convert_to_str_internal (bool, bool, char type) const

val_type ival = tmp.value ();

static const bool is_signed = std::numeric_limits<val_type>::is_signed;
static const bool can_be_larger_than_uchar_max
static constexpr bool is_signed = std::numeric_limits<val_type>::is_signed;
static constexpr bool can_be_larger_than_uchar_max
= octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max;

if (octave_base_int_helper<val_type, is_signed,
Expand Down
4 changes: 2 additions & 2 deletions libinterp/octave-value/ov-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ octave_base_value::print_info (std::ostream& os,
err_wrong_type_arg (ee, "octave_base_value::" #F "_value ()", type_name ()); \
} \
\
static const double out_of_range_top \
= static_cast<double> (std::numeric_limits<T>::max ()) + 1.; \
static constexpr double out_of_range_top \
= static_cast<double> (std::numeric_limits<T>::max ()) + 1.0; \
if (require_int && octave::math::x_nint (d) != d) \
error_with_cfn ("conversion of %g to " #T " value failed", d); \
else if (d < std::numeric_limits<T>::min ()) \
Expand Down
12 changes: 5 additions & 7 deletions libinterp/octave-value/ov.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2957,9 +2957,8 @@ check_colon_operand (const octave_value& val, const char *op_str)

double dval = val.double_value ();
double intpart;
static const double out_of_range_top
= static_cast<double> (std::numeric_limits<typename T::val_type>::max ())
+ 1.;
static constexpr double out_of_range_top
= static_cast<double> (std::numeric_limits<typename T::val_type>::max ()) + 1.0;

if (dval >= out_of_range_top
|| dval < std::numeric_limits<typename T::val_type>::min ()
Expand Down Expand Up @@ -2994,8 +2993,7 @@ integer_difference (ST a, ST b)
// Map to unsigned.
// Idea from https://stackoverflow.com/questions/10589559

static const UT offset
= UT (0) - static_cast<UT> (std::numeric_limits<ST>::min ());
static const UT offset = UT (0) - static_cast<UT> (std::numeric_limits<ST>::min ());

UT au = static_cast<UT> (a) + offset;
UT bu = static_cast<UT> (b) + offset;
Expand Down Expand Up @@ -3133,8 +3131,8 @@ range_numel (T base, double increment, T limit)
|| (increment < 0 && base < limit))
return 0;

static const double out_of_range_top
= static_cast<double> (std::numeric_limits<UT>::max ()) + 1.;
static constexpr double out_of_range_top
= static_cast<double> (std::numeric_limits<UT>::max ()) + 1.0;

double abs_increment = std::abs (increment);

Expand Down
6 changes: 3 additions & 3 deletions libinterp/operators/op-mi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ oct_unop_unsigned_uminus (const octave_base_value& a)

uint64_t ival = val.value ();

static const uint64_t max_val
static constexpr uint64_t max_val
= static_cast<uint64_t> (std::numeric_limits<int64_t>::max ());

static const uint64_t max_val_p1 = max_val + 1;
static constexpr uint64_t max_val_p1 = max_val + 1;

if (ival <= max_val)
{
Expand All @@ -84,7 +84,7 @@ oct_unop_unsigned_uminus (const octave_base_value& a)
// should return int8(-128) but converting directly to int8 and
// negating will not return the correct result.

static const int64_t min_signed_ival
static constexpr int64_t min_signed_ival
= std::numeric_limits<int64_t>::min ();

return octave_value (new octave_magic_int (min_signed_ival));
Expand Down
8 changes: 4 additions & 4 deletions liboctave/array/Array-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@ Array<T, Alloc>::resize1 (octave_idx_type n, const T& rfv)
}
else
{
static const octave_idx_type max_stack_chunk = 1024;
octave_idx_type nn = n + std::min (nx, max_stack_chunk);
static const octave_idx_type MAX_STACK_CHUNK = 1024;
octave_idx_type nn = n + std::min (nx, MAX_STACK_CHUNK);
Array<T, Alloc> tmp (Array<T, Alloc> (dim_vector (nn, 1)), dv, 0, n);
T *dest = tmp.fortran_vec ();

Expand Down Expand Up @@ -2196,11 +2196,11 @@ Array<T, Alloc>::lookup (const Array<T, Alloc>& values, sortmode mode) const

// This determines the split ratio between the O(M*log2(N)) and O(M+N)
// algorithms.
static const double ratio = 1.0;
static const double RATIO = 1.0;
sortmode vmode = UNSORTED;

// Attempt the O(M+N) algorithm if M is large enough.
if (nval > ratio * n / octave::math::log2 (n + 1.0))
if (nval > RATIO * n / octave::math::log2 (n + 1.0))
{
vmode = values.issorted ();
// The table must not contain a NaN.
Expand Down
6 changes: 3 additions & 3 deletions liboctave/array/Sparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ Sparse<T, Alloc>::SparseRep::change_length (octave_idx_type nz)
// Always preserve space for 1 element.
nz = (nz > 0 ? nz : 1);

// Skip reallocation if we have less than 1/frac extra elements to discard.
static const int frac = 5;
if (nz > m_nzmax || nz < m_nzmax - m_nzmax/frac)
// Skip reallocation if we have less than 1/FRAC extra elements to discard.
static const int FRAC = 5;
if (nz > m_nzmax || nz < m_nzmax - m_nzmax/FRAC)
{
// Reallocate.
octave_idx_type min_nzmax = std::min (nz, m_nzmax);
Expand Down
2 changes: 1 addition & 1 deletion liboctave/array/idx-vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ idx_vector::idx_vector (const Array<bool>& bnda)
: m_rep (nullptr)
{
// Convert only if it means saving at least half the memory.
static const int factor = (2 * sizeof (octave_idx_type));
static constexpr int factor = (2 * sizeof (octave_idx_type));
octave_idx_type nnz = bnda.nnz ();
if (nnz <= bnda.numel () / factor)
m_rep = new idx_vector_rep (bnda, nnz);
Expand Down
21 changes: 11 additions & 10 deletions liboctave/numeric/lo-mappers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,29 +187,29 @@ negative_sign (float x) { return __lo_ieee_float_signbit (x); }
octave_idx_type
nint_big (double x)
{
static const double out_of_range_top
= static_cast<double> (std::numeric_limits<octave_idx_type>::max ())+1.;
static constexpr double out_of_range_top
= static_cast<double> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;

if (x >= out_of_range_top)
return std::numeric_limits<octave_idx_type>::max ();
else if (x < std::numeric_limits<octave_idx_type>::min ())
return std::numeric_limits<octave_idx_type>::min ();
else
return static_cast<octave_idx_type> ((x > 0.0) ? (x + 0.5)
: (x - 0.5));
return static_cast<octave_idx_type> ((x > 0.0) ? (x + 0.5) : (x - 0.5));
}

octave_idx_type
nint_big (float x)
{
static const float out_of_range_top
= static_cast<float> (std::numeric_limits<octave_idx_type>::max ())+1.;
static constexpr float out_of_range_top
= static_cast<float> (std::numeric_limits<octave_idx_type>::max ()) + 1.0;

if (x >= out_of_range_top)
return std::numeric_limits<octave_idx_type>::max ();
else if (x < std::numeric_limits<octave_idx_type>::min ())
return std::numeric_limits<octave_idx_type>::min ();
else
return static_cast<octave_idx_type> ((x > 0.0f) ? (x + 0.5f)
: (x - 0.5f));
return static_cast<octave_idx_type> ((x > 0.0f) ? (x + 0.5f) : (x - 0.5f));
}

int
Expand All @@ -226,8 +226,9 @@ nint (double x)
int
nint (float x)
{
static const float out_of_range_top
= static_cast<float> (std::numeric_limits<int>::max ()) + 1.;
static constexpr float out_of_range_top
= static_cast<float> (std::numeric_limits<int>::max ()) + 1.0;

if (x >= out_of_range_top)
return std::numeric_limits<int>::max ();
else if (x < std::numeric_limits<int>::min ())
Expand Down
4 changes: 2 additions & 2 deletions liboctave/util/oct-inttypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ 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 ());
static constexpr 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 ())
Expand Down
Loading

0 comments on commit 2d50f10

Please sign in to comment.