1 #ifndef BENCHMARK_REGISTER_H
2 #define BENCHMARK_REGISTER_H
12 // Append the powers of 'mult' in the closed interval [lo, hi].
13 // Returns iterator to the start of the inserted range.
15 typename
std::vector
<T
>::iterator
AddPowers(std::vector
<T
>* dst
, T lo
, T hi
,
21 const size_t start_offset
= dst
->size();
23 static const T kmax
= std::numeric_limits
<T
>::max();
25 // Space out the values in multiples of "mult"
26 for (T i
= static_cast<T
>(1); i
<= hi
; i
*= mult
) {
30 // Break the loop here since multiplying by
31 // 'mult' would move outside of the range of T
32 if (i
> kmax
/ mult
) break;
35 return dst
->begin() + start_offset
;
39 void AddNegatedPowers(std::vector
<T
>* dst
, T lo
, T hi
, int mult
) {
40 // We negate lo and hi so we require that they cannot be equal to 'min'.
41 BM_CHECK_GT(lo
, std::numeric_limits
<T
>::min());
42 BM_CHECK_GT(hi
, std::numeric_limits
<T
>::min());
46 // Add positive powers, then negate and reverse.
47 // Casts necessary since small integers get promoted
48 // to 'int' when negating.
49 const auto lo_complement
= static_cast<T
>(-lo
);
50 const auto hi_complement
= static_cast<T
>(-hi
);
52 const auto it
= AddPowers(dst
, hi_complement
, lo_complement
, mult
);
54 std::for_each(it
, dst
->end(), [](T
& t
) { t
*= -1; });
55 std::reverse(it
, dst
->end());
59 void AddRange(std::vector
<T
>* dst
, T lo
, T hi
, int mult
) {
60 static_assert(std::is_integral
<T
>::value
&& std::is_signed
<T
>::value
,
61 "Args type must be a signed integer");
69 // Handle lo == hi as a special case, so we then know
70 // lo < hi and so it is safe to add 1 to lo and subtract 1
71 // from hi without falling outside of the range of T.
74 // Ensure that lo_inner <= hi_inner below.
80 // Add all powers of 'mult' in the range [lo+1, hi-1] (inclusive).
81 const auto lo_inner
= static_cast<T
>(lo
+ 1);
82 const auto hi_inner
= static_cast<T
>(hi
- 1);
84 // Insert negative values
86 AddNegatedPowers(dst
, lo_inner
, std::min(hi_inner
, T
{-1}), mult
);
89 // Treat 0 as a special case (see discussion on #762).
90 if (lo
< 0 && hi
>= 0) {
94 // Insert positive values
96 AddPowers(dst
, std::max(lo_inner
, T
{1}), hi_inner
, mult
);
99 // Add "hi" (if different from last value).
100 if (hi
!= dst
->back()) {
105 } // namespace internal
106 } // namespace benchmark
108 #endif // BENCHMARK_REGISTER_H