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
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
49//! a > b ? a : b
50#define SK_MAX(a,b) (((a) > (b)) ? (a) : (b))
51
52#endif
53