blob: 9625d5112b13b492952113fc9d25af8678037c14 [file] [log] [blame]
bungeman@google.comb29c8832011-10-10 13:19:10 +00001/*
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
14template <uintmax_t N, uintmax_t B>
15struct SK_LOG {
16 //! Compile-time constant ceiling(logB(N)).
17 static const uintmax_t value = 1 + SK_LOG<N/B, B>::value;
18};
19template <uintmax_t B>
20struct SK_LOG<1, B> {
21 static const uintmax_t value = 0;
22};
23template <uintmax_t B>
24struct SK_LOG<0, B> {
25 static const uintmax_t value = 0;
26};
27
28template<uintmax_t N>
29struct SK_2N1 {
30 //! Compile-time constant (2^N)-1.
31 static const uintmax_t value = (SK_2N1<N-1>::value << 1) + 1;
32};
33template<>
34struct 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.orgb5e34e22013-05-07 15:28:15 +000049// Compile-time constant maximum value of two unsigned values.
50template <uintmax_t a, uintmax_t b> struct SkTUMax {
51 static const uintmax_t value = (b < a) ? a : b;
52};
bungeman@google.comb29c8832011-10-10 13:19:10 +000053
54#endif