SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
math.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <bit>
16 #include <cassert>
17 #include <cmath>
18 #include <concepts>
19 #include <stdexcept>
20 
21 #include <seqan3/core/platform.hpp>
22 
23 namespace seqan3::detail
24 {
25 
53 template <std::unsigned_integral unsigned_t>
54 constexpr unsigned_t floor_log2(unsigned_t const n) noexcept
55 {
56  assert(n > 0u); // n == 0 is undefined behaviour
57  return std::bit_width(n) - 1;
58 }
59 
87 template <std::unsigned_integral unsigned_t>
88 constexpr unsigned_t ceil_log2(unsigned_t const n) noexcept
89 {
90  assert(n > 0u); // n == 0 is undefined behaviour
91  return (n == 1u) ? 0u : seqan3::detail::floor_log2(n - 1u) + 1u;
92 }
93 
94 } // namespace seqan3::detail
95 
96 namespace seqan3
97 {
120 template <typename base_t, std::unsigned_integral exp_t>
121  requires (std::same_as<base_t, uint64_t> || std::same_as<base_t, int64_t>)
122 base_t pow(base_t base, exp_t exp)
123 {
124  base_t result{1};
125 #ifndef NDEBUG
126  if (base == 0)
127  return 0;
128 
129  for (exp_t i = 0; i < exp; ++i)
130  {
131  if ((base < 0 ? std::numeric_limits<base_t>::min() : std::numeric_limits<base_t>::max()) / base < result)
132  {
133  std::string error_message{"Calculating " + std::to_string(base) + '^' + std::to_string(exp)
134  + " will result in an "
135  + (std::same_as<base_t, int64_t> ? "int64_t" : "uint64_t")};
136  if (base < 0)
137  throw std::underflow_error{error_message + " underflow."};
138  else
139  throw std::overflow_error{error_message + " overflow."};
140  }
141  result *= base;
142  }
143 #else
144  for (; exp; exp >>= 1, base *= base)
145  result *= (exp & 1) ? base : 1;
146 #endif
147  return result;
148 }
149 
151 // If base and exponent are unsigned integrals, promote the base to `uint64_t`.
152 template <std::integral base_t, std::unsigned_integral exp_t>
153  requires (std::unsigned_integral<base_t> && !std::same_as<base_t, uint64_t>)
154 uint64_t pow(base_t base, exp_t exp)
155 {
156  return pow(static_cast<uint64_t>(base), exp);
157 }
158 
159 // If the base is a signed integral and the exponent is an unsigned integral, promote the base to `int64_t`.
160 template <std::integral base_t, std::unsigned_integral exp_t>
161  requires (!std::unsigned_integral<base_t> && !std::same_as<base_t, int64_t>)
162 int64_t pow(base_t base, exp_t exp)
163 {
164  return pow(static_cast<int64_t>(base), exp);
165 }
166 
167 // Otherwise delegate to `std::pow`.
168 template <typename base_t, typename exp_t>
169  requires (!(std::integral<base_t> && std::unsigned_integral<exp_t>))
170 auto pow(base_t base, exp_t exp)
171 {
172  return std::pow(base, exp);
173 }
175 
176 } // namespace seqan3
The <bit> header from C++20's standard library.
The <concepts> header from C++20's standard library.
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
Provides platform and dependency checks.
T pow(T... args)
T to_string(T... args)