Program Listing for File utils.h

Return to documentation for file (rcsw/utils/utils.h)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/rcsw.h"
#include "rcsw/common/fpc.h"

/*******************************************************************************
 * Bit Manipulation Macros
 ******************************************************************************/
#define RCSW_M32U16(v) ((uint32_t)((v) & 0xFFFF0000))

#define RCSW_M32L16(v) ((uint32_t)((v) & 0x0000FFFF))

#define RCSW_M64U32(v) ((uint64_t)(((v) & 0xFFFFFFFF00000000)))

#define RCSW_M64L32(v) (((uint64_t)((v) & 0x00000000FFFFFFFF)))

#define RCSW_REV8(v)                                                         \
    ((uint8_t)(                                                         \
        ((((v) * 0x0802LU & 0x22110LU) | ((v) * 0x8020LU & 0x88440LU)) * \
         0x10101LU >>                                                   \
         16)))
#define RCSW_REV16(v) ((uint16_t)((RCSW_REV8(((v) & 0xFF)) << 8) | \
                                  RCSW_REV8((((v) >> 8) & 0xFF))))
#define RCSW_REV32(v)((uint32_t)(((RCSW_REV16(((v) & 0xFFFF))) << 16) | \
                                 (RCSW_REV16((((v) >> 16) & 0xFFFF)))))

#define RCSW_REVL8(v) ((uint8_t)(rcsw_util_revtable[v]))
#define RCSW_REVL16(v) ((uint16_t)((RCSW_REVL8(((v) & 0xFF)) << 8) | \
                                   RCSW_REVL8((((v) >> 8) & 0xFF))))
#define RCSW_REVL32(v) ((uint32_t)(((RCSW_REVL16(((v) & 0xFFFF))) << 16) | \
                                   (RCSW_REVL16((((v) >> 16) & 0xFFFF)))))

#define RCSW_REFL8(v) ((uint8_t)reflect(((v)), 8))
#define RCSW_REFL16(v) ((uint16_t)reflect(((v)), 16))
#define RCSW_REFL32(v) ((uint32_t)reflect(((v)), 32))

#define RCSW_BIN8(b) (RCSW_BIN8_HEXIFY((uint8_t)RCSW_BIN8_HEXIFY(b)))

#define RCSW_BIN16(b1, b0)                                              \
  (((BIN8_HEXIFY((uint8_t)RCSW_BIN8_HEXIFY(b1))) << 8) + RCSW_HEXIFY(b0))

#define RCSW_BIN32(b3, b4, b1, b0)                                      \
  ((((RCSW_BIN8_HEXIFY((uint8_t)RCSW_BIN8_HEXIFY(b4))) << 8) +          \
    RCSW_BIN8(b3))(((RCSW_BIN8_HEXIFY((uint8_t)RCSW_BIN8_HEXIFY(b1))) << 8) + RCSW_BIN8(b0)))

/* \cond INTERNAL */
#define RCSW_HEXIFY (n)(0x##n##LU)

#define RCSW_BIN8_HEXIFY(x)                                             \
  ((((x) & 0x0000000FLU) ? 1 : 0) + (((x) & 0x000000F0LU) ? 2 : 0) +    \
   (((x) & 0x00000F00LU) ? 4 : 0) + (((x) & 0x0000F000LU) ? 8 : 0) +    \
   (((x) & 0x000F0000LU) ? 16 : 0) + (((x) & 0x00F00000LU) ? 32 : 0) +  \
   (((x) & 0x0F000000LU) ? 64 : 0) + (((x) & 0xF0000000LU) ? 128 : 0))

/* \endcond */


#define RCSW_BIT_WIDTH(t) (sizeof(t) * 8)
#define RCSW_TOPBIT(t) (1 << (BIT_WIDTH(t) - 1))

#define RCSW_GEN_M32(name, num) name = (1 << (num)),
#define RCSW_GEN_M64(name, num) name = ((long long int)1 << (num)),

/*******************************************************************************
 * Alignment Macros
 ******************************************************************************/
#define RCSW_IS_MEM_ALIGNED(p, byte_count) ((((uintptr_t)(p)) % (byte_count)) == 0)

#define RCSW_IS_SIZE_ALIGNED(size, power_of_two) (((size) & ((power_of_two)-1)) == 0)

#define RCSW_ALIGN_SIZE(size, power_of_two)                  \
    (((size) + (power_of_two)-1) & ~((power_of_two)-1))

/*******************************************************************************
 * Endianness Macros
 ******************************************************************************/
#define RCSW_IS_LITTLE_ENDIAN() (((*(short *)"21") & 0xFF) == '2')

#define RCSW_IS_BIG_ENDIAN() (((*(short *)"21") & 0xFF) == '1')

#define RCSW_BSWAP16(w16) ((((w16)&0xFF00) >> 8) | (((w16)&0xFF) << 8))

#define RCSW_BSWAP32(w32)                                            \
    ((((w32)&0xFF000000) >> 24) | (((w32)&0xFF0000) >> 8) |     \
     (((w32)&0xFF00) << 8) | (((w32)&0xFF) << 24))

#define RCSW_BSWAP64(w64)                                                    \
    (((uint64_t)RCSW_BSWAP32(RCSW_M64L32(w64)) << 32) | \
     ((uint64_t)(RCSW_BSWAP32(RCSW_M64U32(w64) >> 32))))

#define RCSW_BSWAP32_16(w32) ((((w32)&0xFFFF0000) >> 16) | (((w32)&0xFFFF) << 16))

/*******************************************************************************
 * Global Variables
 ******************************************************************************/
BEGIN_C_DECLS

RCSW_API extern const uint8_t rcsw_util_revtable[];

/*******************************************************************************
 * API Functions
 ******************************************************************************/
RCSW_PURE static inline float utils_clamp_f255(float v) {
    if (v < 0) {
        return 0.0F;
    }
    if (v > 255.0F) {
      return 255.0F;
    }
    return v;
} /* utils_clam_f255() */

RCSW_API void arr8_reverse(void* arr, size_t size);

RCSW_API void arr32_permute(uint32_t *arr, size_t size, size_t start,
                            void (*fp)(uint32_t * elt));

RCSW_API void arr32_elt_swap(uint32_t * v, size_t i, size_t j);

RCSW_API status_t util_string_gen(char * buf, size_t len);

RCSW_API uint32_t util_reflect32(uint32_t data, size_t n_bits) RCSW_CONST;

RCSW_API bool_t util_zchk(void *elt, size_t elt_size);

END_C_DECLS