File: crn_math.cpp

package info (click to toggle)
crunch-dxtc 0.55.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,624 kB
  • sloc: cpp: 64,979; ansic: 633; python: 321; makefile: 116
file content (67 lines) | stat: -rw-r--r-- 1,649 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// File: crn_math.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"

namespace crnlib {
namespace math {
uint g_bitmasks[32] =
    {
        1U << 0U, 1U << 1U, 1U << 2U, 1U << 3U,
        1U << 4U, 1U << 5U, 1U << 6U, 1U << 7U,
        1U << 8U, 1U << 9U, 1U << 10U, 1U << 11U,
        1U << 12U, 1U << 13U, 1U << 14U, 1U << 15U,
        1U << 16U, 1U << 17U, 1U << 18U, 1U << 19U,
        1U << 20U, 1U << 21U, 1U << 22U, 1U << 23U,
        1U << 24U, 1U << 25U, 1U << 26U, 1U << 27U,
        1U << 28U, 1U << 29U, 1U << 30U, 1U << 31U};

double compute_entropy(const uint8* p, uint n) {
  uint hist[256];
  utils::zero_object(hist);

  for (uint i = 0; i < n; i++)
    hist[*p++]++;

  double entropy = 0.0f;

  const double invln2 = 1.0f / log(2.0f);
  for (uint i = 0; i < 256; i++) {
    if (!hist[i])
      continue;

    double prob = static_cast<double>(hist[i]) / n;
    entropy += (-log(prob) * invln2) * hist[i];
  }

  return entropy;
}

void compute_lower_pow2_dim(int& width, int& height) {
  const int tex_width = width;
  const int tex_height = height;

  width = 1;
  for (;;) {
    if ((width * 2) > tex_width)
      break;
    width *= 2;
  }

  height = 1;
  for (;;) {
    if ((height * 2) > tex_height)
      break;
    height *= 2;
  }
}

void compute_upper_pow2_dim(int& width, int& height) {
  if (!math::is_power_of_2((uint32)width))
    width = math::next_pow2((uint32)width);

  if (!math::is_power_of_2((uint32)height))
    height = math::next_pow2((uint32)height);
}

}  // namespace math
}  // namespace crnlib