Coverage Report

Created: 2025-06-16 07:00

/src/libjxl/lib/jxl/pack_signed.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_PACK_H_
7
#define LIB_JXL_PACK_H_
8
9
// Pack/UnpackSigned utilities.
10
11
#include <cstddef>
12
#include <cstdint>
13
14
#include "lib/jxl/base/compiler_specific.h"
15
16
namespace jxl {
17
// Encodes non-negative (X) into (2 * X), negative (-X) into (2 * X - 1)
18
constexpr uint32_t PackSigned(int32_t value)
19
33.7M
    JXL_NO_SANITIZE("unsigned-integer-overflow") {
20
33.7M
  return (static_cast<uint32_t>(value) << 1) ^
21
33.7M
         ((static_cast<uint32_t>(~value) >> 31) - 1);
22
33.7M
}
23
24
// Reverse to PackSigned, i.e. UnpackSigned(PackSigned(X)) == X.
25
// (((~value) & 1) - 1) is either 0 or 0xFF...FF and it will have an expected
26
// unsigned-integer-overflow.
27
// NB: semantically `value` should have type `uint32_t`, but for efficiency and
28
// convenience its type is `size_t` (i.e. `uint32_t` or `uint64_t`).
29
constexpr int32_t UnpackSigned(size_t value)
30
107M
    JXL_NO_SANITIZE("unsigned-integer-overflow") {
31
  // TODO(Ivan): fails in C++11 mode, restore with a guard?
32
  // JXL_DASSERT((value & 0xFFFFFFFF) == value);  // no-op in 32-bit build
33
107M
  return static_cast<int32_t>((value >> 1) ^ (((~value) & 1) - 1));
34
107M
}
35
36
}  // namespace jxl
37
38
#endif  // LIB_JXL_PACK_H_