【C言語】便利なマクロ集

naohiro19
記事: 256
登録日時: 14年前
住所: 愛知県

【C言語】便利なマクロ集

投稿記事 by naohiro19 » 9年前

C/C++ 関数・マクロ集 ((ほぼ?) 処理系・OS 非依存)から拝借しました。

CODE:

#pragma once

/**********************
*    UtilityMacros.h
***********************/

#include 

#define ValuesInOrder3(value1, value2, value3) \
  (((value1)  0))

#define IsPowerOf2_(uintValue)  ((((uintValue) - 1U) & (uintValue)) == 0)

#define OCTDIGITS(nBits)    ICEIL(nBits, 3U)

#define DECDIGITS(nBits)   ((((nBits) * 1233U) >> 12) + 1U)

#define BitSizeOf(type)     (sizeof(type) * CHAR_BIT)

#define ArrayEnd(array)         ((array) + ArraySizeOf(array))

#define MSBMASK(uintType) \
  ((uintType)((uintType)~(uintType)0 - ((uintType)~(uintType)0 >> 1)))

#define RoundHalfUp(dividend, divisor) \
  (((dividend) + (divisor) / 2) / (divisor))

#define IntegerMin(intType) \
  (IntegerIsSigned(intType) \
   ? (intType)((intType)1 << (BitSizeOf(intType) - 1)) : (intType)0)

#define IntegerIsSignAndAbs(intType) \
  (IntegerIsSigned(intType) && \
   ((intType)((((intType)1 << (BitSizeOf(intType) - 2)) + 1) << 1) \
    == (intType)(-2)))

#define IntegerIsOnesComplement(intType) \
  (IntegerIsSigned(intType) && \
   ((intType)((((intType)1 << (BitSizeOf(intType) - 2)) - 1) << 2) \
    == (intType)(-3)))

#define ENDIAN_CODE(byte0, byte1, byte2, byte3) \
  ((uint32_t)(((((((byte0) << CHAR_BIT) | (byte1)) \
                 << CHAR_BIT) | (byte2)) << CHAR_BIT) | (byte3)))

#define BIG_ENDIAN    ENDIAN_CODE(1, 2, 3, 4)
#define LITTLE_ENDIAN ENDIAN_CODE(4, 3, 2, 1)
#define NUXI_ENDIAN   ENDIAN_CODE(2, 1, 4, 3)
#define IXUN_ENDIAN   ENDIAN_CODE(3, 4, 1, 2)
#define UNXI_ENDIAN   ENDIAN_CODE(1, 2, 4, 3)
#define UINX_ENDIAN   ENDIAN_CODE(1, 3, 2, 4)
#define UIXN_ENDIAN   ENDIAN_CODE(1, 3, 4, 2)
#define UXNI_ENDIAN   ENDIAN_CODE(1, 4, 2, 3)
#define UXIN_ENDIAN   ENDIAN_CODE(1, 4, 3, 2)
#define NUIX_ENDIAN   ENDIAN_CODE(2, 1, 3, 4)
#define NIUX_ENDIAN   ENDIAN_CODE(2, 3, 1, 4)
#define NIXU_ENDIAN   ENDIAN_CODE(2, 3, 4, 1) // 実在しました!! (゚д゚ )
#define NXUI_ENDIAN   ENDIAN_CODE(2, 4, 1, 3)
#define NXIU_ENDIAN   ENDIAN_CODE(2, 4, 3, 1)
#define IUNX_ENDIAN   ENDIAN_CODE(3, 1, 2, 4)
#define IUXN_ENDIAN   ENDIAN_CODE(3, 1, 4, 2)
#define INUX_ENDIAN   ENDIAN_CODE(3, 2, 1, 4)
#define INXU_ENDIAN   ENDIAN_CODE(3, 2, 4, 1)
#define IXNU_ENDIAN   ENDIAN_CODE(3, 4, 2, 1)
#define XUNI_ENDIAN   ENDIAN_CODE(4, 1, 2, 3)
#define XUIN_ENDIAN   ENDIAN_CODE(4, 1, 3, 2)
#define XNUI_ENDIAN   ENDIAN_CODE(4, 2, 1, 3)
#define XNIU_ENDIAN   ENDIAN_CODE(4, 2, 3, 1)
#define XIUN_ENDIAN   ENDIAN_CODE(4, 3, 1, 2)

コメントはまだありません。