L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
arith
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11namespace cxx { namespace arith {
12
27template<typename N, typename D>
28constexpr N
29div_ceil(N const &n, D const &d)
30{
31 // Since C++11 the "quotient is truncated towards zero (fractional part is
32 // discarded)". Thus a negative quotient is already ceiled, whereas a
33 // positive quotient is floored. Furthermore, since C++11 the sign of the
34 // % operator is no longer implementation defined, thus we can use n % d to
35 // detect if the quotient is positive (n % d >= 0) and was truncated (n % d !=
36 // 0). In that case, we add one to round to the next largest integer.
37 return n / d + (n % d > 0);
38}
39
47template< unsigned long V >
48struct Ld
49{
50 enum { value = Ld<V / 2>::value + 1 };
51};
52
53template<>
54struct Ld<0>
55{
56 enum { value = ~0UL };
57};
58
59template<>
60struct Ld<1>
61{
62 enum { value = 0 };
63};
64
72constexpr unsigned
73log2u(unsigned val)
74{
75 return 8 * sizeof(val) - __builtin_clz(val) - 1;
76}
77
79constexpr unsigned
80log2u(unsigned long val)
81{
82 return 8 * sizeof(val) - __builtin_clzl(val) - 1;
83}
84
86constexpr unsigned
87log2u(unsigned long long val)
88{
89 return 8 * sizeof(val) - __builtin_clzll(val) - 1;
90}
91
100template<typename T>
101constexpr unsigned
102log2u_ceil(T val)
103{
104 return val == 1 ? 0 : log2u(val - 1) + 1;
105}
106
107}}
Our C++ library.
Definition arith:11
Computes the binary logarithm of the given number at compile time.
Definition arith:49