chromium/third_party/skia/src/base/SkEndian.h

/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkEndian_DEFINED
#define SkEndian_DEFINED

#include "include/private/base/SkAssert.h"
#include "include/private/base/SkFeatures.h"

#include <cstdint>

/** \file SkEndian.h

    Macros and helper functions for handling 16 and 32 bit values in
    big and little endian formats.
*/

#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN)
    #error "can't have both LENDIAN and BENDIAN defined"
#endif

#if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN)
    #error "need either LENDIAN or BENDIAN defined"
#endif

/** Swap the two bytes in the low 16bits of the parameters.
    e.g. 0x1234 -> 0x3412
*/
static inline uint16_t SkEndianSwap16(uint16_t value) {}

template<uint16_t N> struct SkTEndianSwap16 {};

/** Vector version of SkEndianSwap16(), which swaps the
    low two bytes of each value in the array.
*/
static inline void SkEndianSwap16s(uint16_t array[], int count) {}

/** Reverse all 4 bytes in a 32bit value.
    e.g. 0x12345678 -> 0x78563412
*/
static constexpr uint32_t SkEndianSwap32(uint32_t value) {}

template<uint32_t N> struct SkTEndianSwap32 {};

/** Vector version of SkEndianSwap32(), which swaps the
    bytes of each value in the array.
*/
static inline void SkEndianSwap32s(uint32_t array[], int count) {}

/** Reverse all 8 bytes in a 64bit value.
    e.g. 0x1122334455667788 -> 0x8877665544332211
*/
static inline uint64_t SkEndianSwap64(uint64_t value) {}
template<uint64_t N> struct SkTEndianSwap64 {};

/** Vector version of SkEndianSwap64(), which swaps the
    bytes of each value in the array.
*/
static inline void SkEndianSwap64s(uint64_t array[], int count) {}

// Static casts are used here for otherwise no-op macros to make sure they return the same type as
// SkEndianSwap* functions.
#ifdef SK_CPU_LENDIAN
    #define SkEndian_SwapBE16(n)
    #define SkEndian_SwapBE32(n)
    #define SkEndian_SwapBE64(n)
    #define SkEndian_SwapLE16(n)
    #define SkEndian_SwapLE32(n)
    #define SkEndian_SwapLE64(n)

    #define SkTEndian_SwapBE16(n)
    #define SkTEndian_SwapBE32(n)
    #define SkTEndian_SwapBE64(n)
    #define SkTEndian_SwapLE16(n)
    #define SkTEndian_SwapLE32(n)
    #define SkTEndian_SwapLE64(n)
#else   // SK_CPU_BENDIAN
    #define SkEndian_SwapBE16
    #define SkEndian_SwapBE32
    #define SkEndian_SwapBE64
    #define SkEndian_SwapLE16
    #define SkEndian_SwapLE32
    #define SkEndian_SwapLE64

    #define SkTEndian_SwapBE16
    #define SkTEndian_SwapBE32
    #define SkTEndian_SwapBE64
    #define SkTEndian_SwapLE16
    #define SkTEndian_SwapLE32
    #define SkTEndian_SwapLE64
#endif

// When a bytestream is embedded in a 32-bit word, how far we need to
// shift the word to extract each byte from the low 8 bits by anding with 0xff.
#ifdef SK_CPU_LENDIAN
    #define SkEndian_Byte0Shift
    #define SkEndian_Byte1Shift
    #define SkEndian_Byte2Shift
    #define SkEndian_Byte3Shift
#else   // SK_CPU_BENDIAN
    #define SkEndian_Byte0Shift
    #define SkEndian_Byte1Shift
    #define SkEndian_Byte2Shift
    #define SkEndian_Byte3Shift
#endif


#if defined(SK_UINT8_BITFIELD_LENDIAN) && defined(SK_UINT8_BITFIELD_BENDIAN)
    #error "can't have both bitfield LENDIAN and BENDIAN defined"
#endif

#if !defined(SK_UINT8_BITFIELD_LENDIAN) && !defined(SK_UINT8_BITFIELD_BENDIAN)
    #ifdef SK_CPU_LENDIAN
        #define SK_UINT8_BITFIELD_LENDIAN
    #else
        #define SK_UINT8_BITFIELD_BENDIAN
    #endif
#endif

#ifdef SK_UINT8_BITFIELD_LENDIAN
    #define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7)
#else
    #define SK_UINT8_BITFIELD
#endif

#endif