tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

common.h (3612B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #ifndef LIB_JXL_BASE_COMMON_H_
      7 #define LIB_JXL_BASE_COMMON_H_
      8 
      9 // Shared constants and helper functions.
     10 
     11 #include <array>
     12 #include <cstddef>
     13 #include <cstdint>
     14 #include <cstdio>
     15 #include <memory>
     16 #include <string>
     17 #include <type_traits>
     18 
     19 #include "lib/jxl/base/compiler_specific.h"
     20 
     21 namespace jxl {
     22 // Some enums and typedefs used by more than one header file.
     23 
     24 constexpr size_t kBitsPerByte = 8;  // more clear than CHAR_BIT
     25 
     26 constexpr inline size_t RoundUpBitsToByteMultiple(size_t bits) {
     27  return (bits + 7) & ~static_cast<size_t>(7);
     28 }
     29 
     30 constexpr inline size_t RoundUpToBlockDim(size_t dim) {
     31  return (dim + 7) & ~static_cast<size_t>(7);
     32 }
     33 
     34 static inline bool JXL_MAYBE_UNUSED SafeAdd(const uint64_t a, const uint64_t b,
     35                                            uint64_t& sum) {
     36  sum = a + b;
     37  return sum >= a;  // no need to check b - either sum >= both or < both.
     38 }
     39 
     40 template <typename T1, typename T2>
     41 constexpr inline T1 DivCeil(T1 a, T2 b) {
     42  return (a + b - 1) / b;
     43 }
     44 
     45 // Works for any `align`; if a power of two, compiler emits ADD+AND.
     46 constexpr inline size_t RoundUpTo(size_t what, size_t align) {
     47  return DivCeil(what, align) * align;
     48 }
     49 
     50 constexpr double kPi = 3.14159265358979323846264338327950288;
     51 
     52 // Reasonable default for sRGB, matches common monitors. We map white to this
     53 // many nits (cd/m^2) by default. Butteraugli was tuned for 250 nits, which is
     54 // very close.
     55 // NB: This constant is not very "base", but it is shared between modules.
     56 static constexpr float kDefaultIntensityTarget = 255;
     57 
     58 template <typename T>
     59 constexpr T Pi(T multiplier) {
     60  return static_cast<T>(multiplier * kPi);
     61 }
     62 
     63 // Prior to C++14 (i.e. C++11): provide our own make_unique
     64 #if __cplusplus < 201402L
     65 template <typename T, typename... Args>
     66 std::unique_ptr<T> make_unique(Args&&... args) {
     67  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
     68 }
     69 #else
     70 using std::make_unique;
     71 #endif
     72 
     73 typedef std::array<float, 3> Color;
     74 
     75 // Backported std::experimental::to_array
     76 
     77 template <typename T>
     78 using remove_cv_t = typename std::remove_cv<T>::type;
     79 
     80 template <size_t... I>
     81 struct index_sequence {};
     82 
     83 template <size_t N, size_t... I>
     84 struct make_index_sequence : make_index_sequence<N - 1, N - 1, I...> {};
     85 
     86 template <size_t... I>
     87 struct make_index_sequence<0, I...> : index_sequence<I...> {};
     88 
     89 namespace detail {
     90 
     91 template <typename T, size_t N, size_t... I>
     92 constexpr auto to_array(T (&&arr)[N], index_sequence<I...> _)
     93    -> std::array<remove_cv_t<T>, N> {
     94  return {{std::move(arr[I])...}};
     95 }
     96 
     97 }  // namespace detail
     98 
     99 template <typename T, size_t N>
    100 constexpr auto to_array(T (&&arr)[N]) -> std::array<remove_cv_t<T>, N> {
    101  return detail::to_array(std::move(arr), make_index_sequence<N>());
    102 }
    103 
    104 template <typename T>
    105 JXL_INLINE T Clamp1(T val, T low, T hi) {
    106  return val < low ? low : val > hi ? hi : val;
    107 }
    108 
    109 // conversion from integer to string.
    110 template <typename T>
    111 std::string ToString(T n) {
    112  char data[32] = {};
    113  if (std::is_floating_point<T>::value) {
    114    // float
    115    snprintf(data, sizeof(data), "%g", static_cast<double>(n));
    116  } else if (std::is_unsigned<T>::value) {
    117    // unsigned
    118    snprintf(data, sizeof(data), "%llu", static_cast<unsigned long long>(n));
    119  } else {
    120    // signed
    121    snprintf(data, sizeof(data), "%lld", static_cast<long long>(n));
    122  }
    123  return data;
    124 }
    125 
    126 #define JXL_JOIN(x, y) JXL_DO_JOIN(x, y)
    127 #define JXL_DO_JOIN(x, y) x##y
    128 
    129 }  // namespace jxl
    130 
    131 #endif  // LIB_JXL_BASE_COMMON_H_