Coverage Report

Created: 2026-06-14 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/enc_ans_simd.cc
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
#include "lib/jxl/enc_ans_simd.h"
7
8
#include <cstdint>
9
10
#include "lib/jxl/base/compiler_specific.h"
11
#include "lib/jxl/base/status.h"
12
#include "lib/jxl/dec_ans.h"
13
#include "lib/jxl/memory_manager_internal.h"
14
15
#undef HWY_TARGET_INCLUDE
16
#define HWY_TARGET_INCLUDE "lib/jxl/enc_ans_simd.cc"
17
#include <hwy/foreach_target.h>
18
#include <hwy/highway.h>
19
20
HWY_BEFORE_NAMESPACE();
21
namespace jxl {
22
namespace HWY_NAMESPACE {
23
24
// These templates are not found via ADL.
25
using hwy::HWY_NAMESPACE::Add;
26
using hwy::HWY_NAMESPACE::And;
27
using hwy::HWY_NAMESPACE::Ge;
28
using hwy::HWY_NAMESPACE::GetLane;
29
using hwy::HWY_NAMESPACE::Gt;
30
using hwy::HWY_NAMESPACE::IfThenElse;
31
using hwy::HWY_NAMESPACE::IfThenElseZero;
32
using hwy::HWY_NAMESPACE::Iota;
33
using hwy::HWY_NAMESPACE::LoadU;
34
using hwy::HWY_NAMESPACE::Lt;
35
using hwy::HWY_NAMESPACE::Mul;
36
using hwy::HWY_NAMESPACE::Or;
37
using hwy::HWY_NAMESPACE::Set;
38
using hwy::HWY_NAMESPACE::ShiftRight;
39
using hwy::HWY_NAMESPACE::Store;
40
using hwy::HWY_NAMESPACE::Sub;
41
using hwy::HWY_NAMESPACE::Zero;
42
43
template <size_t E, size_t M, size_t L>
44
uint32_t EstimateTokenCostImpl(uint32_t* JXL_RESTRICT values, size_t len,
45
                               uint32_t* JXL_RESTRICT out) {
46
  const HWY_FULL(uint32_t) du;
47
  const HWY_FULL(float) df;
48
  const auto kZero = Zero(du);
49
  const auto kSplit = Set(du, 1 << E);
50
  const auto kExpOffset = Set(du, 127);
51
  const auto kEBOffset = Set(du, 127 + M + L);
52
  const auto kBase = Set(du, static_cast<uint32_t>((1 << E) - (E << (M + L))));
53
  const auto kMulN = Set(du, 1 << (M + L));
54
  const auto kMaskL = Set(du, (1 << L) - 1);
55
  const auto kMaskM = Set(du, ((1 << M) - 1) << L);
56
  const auto kLargeThreshold = Set(du, (1 << 22) - 1);
57
  constexpr size_t kLargeShiftVal = 10;
58
  const auto kLargeShift = Set(du, kLargeShiftVal);
59
60
  auto extra_bits = kZero;
61
  size_t last_full = Lanes(du) * (len / Lanes(du));
62
  for (size_t i = 0; i < last_full; i += Lanes(du)) {
63
    const auto val = LoadU(du, values + i);
64
    const auto is_large = Gt(val, kLargeThreshold);
65
    const auto val_shifted = ShiftRight<kLargeShiftVal>(val);
66
    const auto not_literal = Ge(val, kSplit);
67
    const auto val_fixed = IfThenElse(is_large, val_shifted, val);
68
    const auto b = BitCast(du, ConvertTo(df, val_fixed));
69
    const auto l = And(val, kMaskL);
70
    const auto exp = ShiftRight<23>(b);
71
    const auto exp_fixed = IfThenElse(is_large, Add(exp, kLargeShift), exp);
72
    const auto n = Sub(exp_fixed, kExpOffset);
73
    const auto eb = Sub(exp_fixed, kEBOffset);
74
    const auto m = ShiftRight<23 - M - L>(b);
75
    const auto a = Add(kBase, Mul(n, kMulN));
76
    const auto d = And(m, kMaskM);
77
    const auto eb_fixed = IfThenElseZero(not_literal, eb);
78
    const auto c = Or(a, l);
79
    extra_bits = Add(extra_bits, eb_fixed);
80
    const auto t = Or(c, d);
81
    const auto t_fixed = IfThenElse(not_literal, t, val);
82
    Store(t_fixed, du, out + i);
83
  }
84
  if (last_full < len) {
85
    const auto stop = Set(du, len);
86
    const auto fence = Iota(du, last_full);
87
    const auto take = Lt(fence, stop);
88
    const auto val = LoadU(du, values + last_full);
89
    const auto is_large = Gt(val, kLargeThreshold);
90
    const auto val_shifted = ShiftRight<kLargeShiftVal>(val);
91
    const auto not_literal = Ge(val, kSplit);
92
    const auto val_fixed = IfThenElse(is_large, val_shifted, val);
93
    const auto b = BitCast(du, ConvertTo(df, val_fixed));
94
    const auto l = And(val, kMaskL);
95
    const auto exp = ShiftRight<23>(b);
96
    const auto exp_fixed = IfThenElse(is_large, Add(exp, kLargeShift), exp);
97
    const auto n = Sub(exp_fixed, kExpOffset);
98
    const auto eb = Sub(exp_fixed, kEBOffset);
99
    const auto m = ShiftRight<23 - M - L>(b);
100
    const auto a = Add(kBase, Mul(n, kMulN));
101
    const auto d = And(m, kMaskM);
102
    const auto eb_fixed = IfThenElseZero(not_literal, eb);
103
    const auto eb_masked = IfThenElseZero(take, eb_fixed);
104
    const auto c = Or(a, l);
105
    extra_bits = Add(extra_bits, eb_masked);
106
    const auto t = Or(c, d);
107
    const auto t_fixed = IfThenElse(not_literal, t, val);
108
    Store(t_fixed, du, out + last_full);
109
  }
110
  return GetLane(SumOfLanes(du, extra_bits));
111
}
112
113
uint32_t EstimateTokenCost(uint32_t* JXL_RESTRICT values, size_t len,
114
0
                           HybridUintConfig cfg, AlignedMemory& tokens) {
115
0
  uint32_t* JXL_RESTRICT out = tokens.address<uint32_t>();
116
0
#if HWY_TARGET == HWY_SCALAR
117
0
  uint32_t extra_bits = 0;
118
0
  for (size_t i = 0; i < len; ++i) {
119
0
    uint32_t v = values[i];
120
0
    uint32_t tok, nbits, bits;
121
0
    cfg.Encode(v, &tok, &nbits, &bits);
122
0
    extra_bits += nbits;
123
0
    out[i] = tok;
124
0
  }
125
0
  return extra_bits;
126
#else
127
  if (cfg.split_exponent == 0) {
128
    return EstimateTokenCostImpl<0, 0, 0>(values, len, out);
129
  } else if (cfg.split_exponent == 2) {
130
    JXL_DASSERT((cfg.msb_in_token == 0) && (cfg.lsb_in_token == 1));
131
    return EstimateTokenCostImpl<2, 0, 1>(values, len, out);
132
  } else if (cfg.split_exponent == 3) {
133
    if (cfg.msb_in_token == 1) {
134
      if (cfg.lsb_in_token == 0) {
135
        return EstimateTokenCostImpl<3, 1, 0>(values, len, out);
136
      } else {
137
        JXL_DASSERT(cfg.lsb_in_token == 2);
138
        return EstimateTokenCostImpl<3, 1, 2>(values, len, out);
139
      }
140
    } else {
141
      JXL_DASSERT(cfg.msb_in_token == 2);
142
      if (cfg.lsb_in_token == 0) {
143
        return EstimateTokenCostImpl<3, 2, 0>(values, len, out);
144
      } else {
145
        JXL_DASSERT(cfg.lsb_in_token == 1);
146
        return EstimateTokenCostImpl<3, 2, 1>(values, len, out);
147
      }
148
    }
149
  } else if (cfg.split_exponent == 4) {
150
    if (cfg.msb_in_token == 1) {
151
      if (cfg.lsb_in_token == 0) {
152
        return EstimateTokenCostImpl<4, 1, 0>(values, len, out);
153
      } else if (cfg.lsb_in_token == 2) {
154
        return EstimateTokenCostImpl<4, 1, 2>(values, len, out);
155
      } else {
156
        JXL_DASSERT(cfg.lsb_in_token == 3);
157
        return EstimateTokenCostImpl<4, 1, 3>(values, len, out);
158
      }
159
    } else {
160
      JXL_DASSERT(cfg.msb_in_token == 2);
161
      if (cfg.lsb_in_token == 0) {
162
        return EstimateTokenCostImpl<4, 2, 0>(values, len, out);
163
      } else if (cfg.lsb_in_token == 1) {
164
        return EstimateTokenCostImpl<4, 2, 1>(values, len, out);
165
      } else {
166
        JXL_DASSERT(cfg.lsb_in_token == 2);
167
        return EstimateTokenCostImpl<4, 2, 2>(values, len, out);
168
      }
169
    }
170
  } else if (cfg.split_exponent == 5) {
171
    if (cfg.msb_in_token == 1) {
172
      if (cfg.lsb_in_token == 0) {
173
        return EstimateTokenCostImpl<5, 1, 0>(values, len, out);
174
      } else if (cfg.lsb_in_token == 2) {
175
        return EstimateTokenCostImpl<5, 1, 2>(values, len, out);
176
      } else {
177
        JXL_DASSERT(cfg.lsb_in_token == 4);
178
        return EstimateTokenCostImpl<5, 1, 4>(values, len, out);
179
      }
180
    } else {
181
      JXL_DASSERT(cfg.msb_in_token == 2);
182
      if (cfg.lsb_in_token == 0) {
183
        return EstimateTokenCostImpl<5, 2, 0>(values, len, out);
184
      } else if (cfg.lsb_in_token == 1) {
185
        return EstimateTokenCostImpl<5, 2, 1>(values, len, out);
186
      } else if (cfg.lsb_in_token == 2) {
187
        return EstimateTokenCostImpl<5, 2, 2>(values, len, out);
188
      } else {
189
        JXL_DASSERT(cfg.lsb_in_token == 3);
190
        return EstimateTokenCostImpl<5, 2, 3>(values, len, out);
191
      }
192
    }
193
  } else if (cfg.split_exponent == 6) {
194
    if (cfg.msb_in_token == 0) {
195
      JXL_DASSERT(cfg.lsb_in_token == 0);
196
      return EstimateTokenCostImpl<6, 0, 0>(values, len, out);
197
    } else if (cfg.msb_in_token == 1) {
198
      JXL_DASSERT(cfg.lsb_in_token == 5);
199
      return EstimateTokenCostImpl<6, 1, 5>(values, len, out);
200
    } else {
201
      JXL_DASSERT(cfg.msb_in_token == 2);
202
      JXL_DASSERT(cfg.lsb_in_token == 4);
203
      return EstimateTokenCostImpl<6, 2, 4>(values, len, out);
204
    }
205
  } else if (cfg.split_exponent >= 7 && cfg.split_exponent <= 12) {
206
    JXL_DASSERT(cfg.msb_in_token == 0);
207
    JXL_DASSERT(cfg.lsb_in_token == 0);
208
    if (cfg.split_exponent == 7) {
209
      return EstimateTokenCostImpl<7, 0, 0>(values, len, out);
210
    } else if (cfg.split_exponent == 8) {
211
      return EstimateTokenCostImpl<8, 0, 0>(values, len, out);
212
    } else if (cfg.split_exponent == 9) {
213
      return EstimateTokenCostImpl<9, 0, 0>(values, len, out);
214
    } else if (cfg.split_exponent == 10) {
215
      return EstimateTokenCostImpl<10, 0, 0>(values, len, out);
216
    } else if (cfg.split_exponent == 11) {
217
      return EstimateTokenCostImpl<11, 0, 0>(values, len, out);
218
    } else {
219
      return EstimateTokenCostImpl<12, 0, 0>(values, len, out);
220
    }
221
  } else {
222
    JXL_DASSERT(false);
223
  }
224
  return ~0;
225
#endif
226
0
}
227
228
// NOLINTNEXTLINE(google-readability-namespace-comments)
229
}  // namespace HWY_NAMESPACE
230
}  // namespace jxl
231
HWY_AFTER_NAMESPACE();
232
233
#if HWY_ONCE
234
namespace jxl {
235
236
HWY_EXPORT(EstimateTokenCost);
237
238
uint32_t EstimateTokenCost(uint32_t* JXL_RESTRICT values, size_t len,
239
0
                           HybridUintConfig cfg, AlignedMemory& tokens) {
240
0
  JXL_DASSERT(cfg.lsb_in_token + cfg.msb_in_token <= cfg.split_exponent);
241
0
  return HWY_DYNAMIC_DISPATCH(EstimateTokenCost)(values, len, cfg, tokens);
242
0
}
243
244
}  // namespace jxl
245
#endif