Coverage Report

Created: 2022-08-24 06:33

/src/libjxl/lib/jxl/lehmer_code.h
Line
Count
Source
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_LEHMER_CODE_H_
7
#define LIB_JXL_LEHMER_CODE_H_
8
9
#include <stddef.h>
10
#include <stdint.h>
11
12
#include "lib/jxl/base/bits.h"
13
#include "lib/jxl/base/compiler_specific.h"
14
#include "lib/jxl/base/status.h"
15
16
namespace jxl {
17
18
// Permutation <=> factorial base representation (Lehmer code).
19
20
using LehmerT = uint32_t;
21
22
template <typename T>
23
9.24M
constexpr T ValueOfLowest1Bit(T t) {
24
9.24M
  return t & -t;
25
9.24M
}
int jxl::ValueOfLowest1Bit<int>(int)
Line
Count
Source
23
1.47M
constexpr T ValueOfLowest1Bit(T t) {
24
1.47M
  return t & -t;
25
1.47M
}
unsigned long jxl::ValueOfLowest1Bit<unsigned long>(unsigned long)
Line
Count
Source
23
7.77M
constexpr T ValueOfLowest1Bit(T t) {
24
7.77M
  return t & -t;
25
7.77M
}
Unexecuted instantiation: unsigned int jxl::ValueOfLowest1Bit<unsigned int>(unsigned int)
26
27
// Computes the Lehmer (factorial basis) code of permutation, an array of n
28
// unique indices in [0..n), and stores it in code[0..len). N*logN time.
29
// temp must have n + 1 elements but need not be initialized.
30
template <typename PermutationT>
31
void ComputeLehmerCode(const PermutationT* JXL_RESTRICT permutation,
32
                       uint32_t* JXL_RESTRICT temp, const size_t n,
33
                       LehmerT* JXL_RESTRICT code) {
34
  for (size_t idx = 0; idx < n + 1; ++idx) temp[idx] = 0;
35
36
  for (size_t idx = 0; idx < n; ++idx) {
37
    const PermutationT s = permutation[idx];
38
39
    // Compute sum in Fenwick tree
40
    uint32_t penalty = 0;
41
    uint32_t i = s + 1;
42
    while (i != 0) {
43
      penalty += temp[i];
44
      i &= i - 1;  // clear lowest bit
45
    }
46
    JXL_DASSERT(s >= penalty);
47
    code[idx] = s - penalty;
48
    i = s + 1;
49
    // Add operation in Fenwick tree
50
    while (i < n + 1) {
51
      temp[i] += 1;
52
      i += ValueOfLowest1Bit(i);
53
    }
54
  }
55
}
56
57
// Decodes the Lehmer code in code[0..n) into permutation[0..n).
58
// temp must have 1 << CeilLog2(n) elements but need not be initialized.
59
template <typename PermutationT>
60
void DecodeLehmerCode(const LehmerT* JXL_RESTRICT code,
61
                      uint32_t* JXL_RESTRICT temp, size_t n,
62
10.3k
                      PermutationT* JXL_RESTRICT permutation) {
63
10.3k
  JXL_DASSERT(n != 0);
64
10.3k
  const size_t log2n = CeilLog2Nonzero(n);
65
10.3k
  const size_t padded_n = 1ull << log2n;
66
67
1.48M
  for (size_t i = 0; i < padded_n; i++) {
68
1.47M
    const int32_t i1 = static_cast<int32_t>(i + 1);
69
1.47M
    temp[i] = static_cast<uint32_t>(ValueOfLowest1Bit(i1));
70
1.47M
  }
71
72
1.23M
  for (size_t i = 0; i < n; i++) {
73
1.22M
    JXL_DASSERT(code[i] + i < n);
74
1.22M
    uint32_t rank = code[i] + 1;
75
76
    // Extract i-th unused element via implicit order-statistics tree.
77
1.22M
    size_t bit = padded_n;
78
1.22M
    size_t next = 0;
79
15.1M
    for (size_t i = 0; i <= log2n; i++) {
80
13.9M
      const size_t cand = next + bit;
81
13.9M
      JXL_DASSERT(cand >= 1);
82
13.9M
      bit >>= 1;
83
13.9M
      if (temp[cand - 1] < rank) {
84
6.17M
        next = cand;
85
6.17M
        rank -= temp[cand - 1];
86
6.17M
      }
87
13.9M
    }
88
89
1.22M
    permutation[i] = next;
90
91
    // Mark as used
92
1.22M
    next += 1;
93
8.99M
    while (next <= padded_n) {
94
7.77M
      temp[next - 1] -= 1;
95
7.77M
      next += ValueOfLowest1Bit(next);
96
7.77M
    }
97
1.22M
  }
98
10.3k
}
99
100
}  // namespace jxl
101
102
#endif  // LIB_JXL_LEHMER_CODE_H_