Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkConstexprMath_DEFINED | 8 #ifndef SkConstexprMath_DEFINED |
| 9 #define SkConstexprMath_DEFINED | 9 #define SkConstexprMath_DEFINED |
| 10 | 10 |
| 11 #include "SkTypes.h" | 11 #include <stdint.h> |
| 12 #include <limits.h> | 12 #include <limits> |
| 13 | 13 |
| 14 template <uintmax_t N, uintmax_t B> | 14 /** Compile-time constant floor(log_{b}(n)) */ |
|
bungeman-skia
2016/05/23 15:04:49
We don't want floor, we want ceiling. I think that
hal.canary
2016/05/23 16:55:39
function deleted.
| |
| 15 struct SK_LOG { | 15 constexpr size_t SkLog(uintmax_t n, uintmax_t b) { |
| 16 //! Compile-time constant ceiling(logB(N)). | 16 return n <= 1 ? 0 : 1 + SkLog(n / b, b); |
| 17 static const uintmax_t value = 1 + SK_LOG<N/B, B>::value; | 17 } |
| 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 | 18 |
| 28 template<uintmax_t N> | 19 /** Compile-time constant number of base 10 digits in type t |
|
bungeman-skia
2016/05/23 15:04:49
nit: 'type T'
(capitalized, since that's how its
hal.canary
2016/05/23 16:55:39
function deleted
| |
| 29 struct SK_2N1 { | 20 if the bits of type t are considered as unsigned base two. */ |
| 30 //! Compile-time constant (2^N)-1. | 21 template <typename T> |
| 31 static const uintmax_t value = (SK_2N1<N-1>::value << 1) + 1; | 22 constexpr size_t SkDigitsIn() { |
|
bungeman-skia
2016/05/23 15:04:49
Actually, now that I look, "std::numeric_limits::d
hal.canary
2016/05/23 16:55:39
Done.
| |
| 32 }; | 23 return SkLog(std::numeric_limits<T>::max(), 10); |
| 33 template<> | 24 } |
| 34 struct SK_2N1<1> { | |
| 35 static const uintmax_t value = 1; | |
| 36 }; | |
| 37 | 25 |
| 38 /** Compile-time constant number of base n digits in type t | 26 /** Compile-time constant maximum value of two size_t. */ |
| 39 if the bits of type t are considered as unsigned base two. | 27 constexpr size_t SkSMax(size_t a, size_t b) { |
|
bungeman-skia
2016/05/23 15:04:49
If this is just used for size_t, just use SkTMax d
hal.canary
2016/05/23 16:55:39
Done.
| |
| 40 */ | 28 return b < a ? a : b; |
| 41 #define SK_BASE_N_DIGITS_IN(n, t) (\ | 29 } |
| 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 | |
| 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 }; | |
| 53 | 30 |
| 54 #endif | 31 #endif |
| OLD | NEW |