Coverage Report

Created: 2025-08-12 07:37

/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
83.3k
inline void MoveToFront(uint8_t* v, uint8_t index) {
33
83.3k
  uint8_t value = v[index];
34
83.3k
  uint8_t i = index;
35
83.3k
  if (i < 4) {
36
117k
    for (; i; --i) v[i] = v[i - 1];
37
50.6k
  } else {
38
32.6k
    const HWY_CAPPED(uint8_t, 64) d;
39
32.6k
    int tail = i & (Lanes(d) - 1);
40
32.6k
    if (tail) {
41
24.2k
      i -= tail;
42
24.2k
      const auto vec = Load(d, v + i);
43
24.2k
      const auto prev = LoadU(d, v + i + 1);
44
24.2k
      StoreU(IfThenElse(FirstN(d, tail), vec, prev), d, v + i + 1);
45
24.2k
    }
46
253k
    while (i) {
47
220k
      i -= Lanes(d);
48
220k
      const auto vec = Load(d, v + i);
49
220k
      StoreU(vec, d, v + i + 1);
50
220k
    }
51
32.6k
  }
52
83.3k
  v[0] = value;
53
83.3k
}
54
55
4.50k
inline void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
56
4.50k
  HWY_ALIGN uint8_t mtf[256 + 64];
57
4.50k
  int i;
58
1.15M
  for (i = 0; i < 256; ++i) {
59
1.15M
    mtf[i] = static_cast<uint8_t>(i);
60
1.15M
  }
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
7.26M
  for (i = 0; i < v_len; ++i) {
68
7.25M
    uint8_t index = v[i];
69
7.25M
    v[i] = mtf[index];
70
7.25M
    if (index) MoveToFront(mtf, index);
71
7.25M
  }
72
4.50k
}
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
4.50k
inline void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
87
4.50k
  HWY_STATIC_DISPATCH(InverseMoveToFrontTransform)(v, v_len);
88
4.50k
}
89
}  // namespace jxl
90
91
#endif  // INVERSE_MTF_ONCE
92
#endif  // HWY_ONCE