Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/inverse_mtf-inl.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
// SIMDified inverse-move-to-front transform.
7
8
#include <cstdint>
9
10
#include "lib/jxl/base/sanitizer_definitions.h"
11
12
#if defined(LIB_JXL_INVERSE_MTF_INL_H_) == defined(HWY_TARGET_TOGGLE)
13
#ifdef LIB_JXL_INVERSE_MTF_INL_H_
14
#undef LIB_JXL_INVERSE_MTF_INL_H_
15
#else
16
#define LIB_JXL_INVERSE_MTF_INL_H_
17
#endif
18
19
#include <hwy/highway.h>
20
21
HWY_BEFORE_NAMESPACE();
22
namespace jxl {
23
namespace HWY_NAMESPACE {
24
25
// These templates are not found via ADL.
26
using hwy::HWY_NAMESPACE::FirstN;
27
using hwy::HWY_NAMESPACE::IfThenElse;
28
using hwy::HWY_NAMESPACE::Load;
29
using hwy::HWY_NAMESPACE::LoadU;
30
using hwy::HWY_NAMESPACE::StoreU;
31
32
125k
inline void MoveToFront(uint8_t* v, uint8_t index) {
33
125k
  uint8_t value = v[index];
34
125k
  uint8_t i = index;
35
125k
  if (i < 4) {
36
183k
    for (; i; --i) v[i] = v[i - 1];
37
81.8k
  } else {
38
43.5k
    const HWY_CAPPED(uint8_t, 64) d;
39
43.5k
    int tail = i & (Lanes(d) - 1);
40
43.5k
    if (tail) {
41
38.1k
      i -= tail;
42
38.1k
      const auto vec = Load(d, v + i);
43
38.1k
      const auto prev = LoadU(d, v + i + 1);
44
38.1k
      StoreU(IfThenElse(FirstN(d, tail), vec, prev), d, v + i + 1);
45
38.1k
    }
46
187k
    while (i) {
47
143k
      i -= Lanes(d);
48
143k
      const auto vec = Load(d, v + i);
49
143k
      StoreU(vec, d, v + i + 1);
50
143k
    }
51
43.5k
  }
52
125k
  v[0] = value;
53
125k
}
54
55
6.00k
inline void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
56
6.00k
  HWY_ALIGN uint8_t mtf[256 + 64];
57
6.00k
  int i;
58
1.54M
  for (i = 0; i < 256; ++i) {
59
1.53M
    mtf[i] = static_cast<uint8_t>(i);
60
1.53M
  }
61
#if JXL_MEMORY_SANITIZER
62
  const HWY_CAPPED(uint8_t, 64) d;
63
  for (size_t j = 0; j < Lanes(d); ++j) {
64
    mtf[256 + j] = 0;
65
  }
66
#endif  // JXL_MEMORY_SANITIZER
67
13.9M
  for (i = 0; i < v_len; ++i) {
68
13.9M
    uint8_t index = v[i];
69
13.9M
    v[i] = mtf[index];
70
13.9M
    if (index) MoveToFront(mtf, index);
71
13.9M
  }
72
6.00k
}
73
74
// NOLINTNEXTLINE(google-readability-namespace-comments)
75
}  // namespace HWY_NAMESPACE
76
}  // namespace jxl
77
HWY_AFTER_NAMESPACE();
78
79
#endif  // LIB_JXL_INVERSE_MTF_INL_H_
80
81
#if HWY_ONCE
82
#ifndef INVERSE_MTF_ONCE
83
#define INVERSE_MTF_ONCE
84
85
namespace jxl {
86
6.00k
inline void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
87
6.00k
  HWY_STATIC_DISPATCH(InverseMoveToFrontTransform)(v, v_len);
88
6.00k
}
89
}  // namespace jxl
90
91
#endif  // INVERSE_MTF_ONCE
92
#endif  // HWY_ONCE