libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

pack_signed.h (1020B)


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