BasicTypeSizes.h
// BasicTypeSizes.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013
#pragma once
#ifndef Ceda_cxUtils_BasicTypeSizes_H
#define Ceda_cxUtils_BasicTypeSizes_H
#include <stdint.h>
#include <climits>
namespace ceda
{
/* usage
void CompileTimeAssertions(void)
{
cxAssertMinBitSize(char, 8);
cxAssertMinBitSize(int, 16);
cxAssertExactBitSize(long, 32);
cxAssertExactBitSize(uint32, 33); // will fail to compile
}
*/
#define cxAssertMinBitSize(type, size) \
static_assert(sizeof(type) * CHAR_BIT >= size, "min bit size");
#define cxAssertExactBitSize(type, size) \
static_assert(sizeof(type) * CHAR_BIT == size, "bit size");
// Number of bits for types char,short,int,long,long long,float,double
#define CEDA_NUM_CHAR_BITS CHAR_BIT
#if SHRT_MAX == INT8_MAX
#define CEDA_NUM_SHORT_BITS 8
#elif SHRT_MAX == INT16_MAX
#define CEDA_NUM_SHORT_BITS 16
#elif SHRT_MAX == INT32_MAX
#define CEDA_NUM_SHORT_BITS 32
#elif SHRT_MAX == INT64_MAX
#define CEDA_NUM_SHORT_BITS 64
#endif
#if INT_MAX == INT8_MAX
#define CEDA_NUM_INT_BITS 8
#elif INT_MAX == INT16_MAX
#define CEDA_NUM_INT_BITS 16
#elif INT_MAX == INT32_MAX
#define CEDA_NUM_INT_BITS 32
#elif INT_MAX == INT64_MAX
#define CEDA_NUM_INT_BITS 64
#endif
#if LONG_MAX == INT8_MAX
#define CEDA_NUM_LONG_BITS 8
#elif LONG_MAX == INT16_MAX
#define CEDA_NUM_LONG_BITS 16
#elif LONG_MAX == INT32_MAX
#define CEDA_NUM_LONG_BITS 32
#elif LONG_MAX == INT64_MAX
#define CEDA_NUM_LONG_BITS 64
#endif
#if LLONG_MAX == INT8_MAX
#define CEDA_NUM_LONG_LONG_BITS 8
#elif LLONG_MAX == INT16_MAX
#define CEDA_NUM_LONG_LONG_BITS 16
#elif LLONG_MAX == INT32_MAX
#define CEDA_NUM_LONG_LONG_BITS 32
#elif LLONG_MAX == INT64_MAX
#define CEDA_NUM_LONG_LONG_BITS 64
#endif
#define CEDA_NUM_FLOAT_BITS 32
#define CEDA_NUM_DOUBLE_BITS 64
} // namespace ceda
#endif // include guard