Coverage Report

Created: 2025-06-22 08:04

/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
4.55M
    JXL_NO_SANITIZE("unsigned-integer-overflow") {
20
4.55M
  return (static_cast<uint32_t>(value) << 1) ^
21
4.55M
         ((static_cast<uint32_t>(~value) >> 31) - 1);
22
4.55M
}
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
constexpr intptr_t UnpackSigned(size_t value)
28
720M
    JXL_NO_SANITIZE("unsigned-integer-overflow") {
29
720M
  return static_cast<intptr_t>((value >> 1) ^ (((~value) & 1) - 1));
30
720M
}
31
32
}  // namespace jxl
33
34
#endif  // LIB_JXL_PACK_H_