bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkConstexprMath_DEFINED |
| 9 | #define SkConstexprMath_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
| 12 | #include <limits.h> |
| 13 | |
| 14 | template <uintmax_t N, uintmax_t B> |
| 15 | struct SK_LOG { |
| 16 | //! Compile-time constant ceiling(logB(N)). |
| 17 | static const uintmax_t value = 1 + SK_LOG<N/B, B>::value; |
| 18 | }; |
| 19 | template <uintmax_t B> |
| 20 | struct SK_LOG<1, B> { |
| 21 | static const uintmax_t value = 0; |
| 22 | }; |
| 23 | template <uintmax_t B> |
| 24 | struct SK_LOG<0, B> { |
| 25 | static const uintmax_t value = 0; |
| 26 | }; |
| 27 | |
| 28 | template<uintmax_t N> |
| 29 | struct SK_2N1 { |
| 30 | //! Compile-time constant (2^N)-1. |
| 31 | static const uintmax_t value = (SK_2N1<N-1>::value << 1) + 1; |
| 32 | }; |
| 33 | template<> |
| 34 | struct SK_2N1<1> { |
| 35 | static const uintmax_t value = 1; |
| 36 | }; |
| 37 | |
| 38 | /** Compile-time constant number of base n digits in type t |
| 39 | if the bits of type t are considered as unsigned base two. |
| 40 | */ |
| 41 | #define SK_BASE_N_DIGITS_IN(n, t) (\ |
| 42 | SK_LOG<SK_2N1<(sizeof(t) * CHAR_BIT)>::value, n>::value\ |
| 43 | ) |
| 44 | /** Compile-time constant number of base 10 digits in type t |
| 45 | if the bits of type t are considered as unsigned base two. |
| 46 | */ |
| 47 | #define SK_DIGITS_IN(t) SK_BASE_N_DIGITS_IN(10, (t)) |
| 48 | |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 49 | // Compile-time constant maximum value of two unsigned values. |
| 50 | template <uintmax_t a, uintmax_t b> struct SkTUMax { |
| 51 | static const uintmax_t value = (b < a) ? a : b; |
| 52 | }; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 53 | |
| 54 | #endif |