309 lines
6.3 KiB
C
309 lines
6.3 KiB
C
#ifndef BASE_UTILS_INCLUDE_CELOXICA_MATH_H_
|
|
#define BASE_UTILS_INCLUDE_CELOXICA_MATH_H_
|
|
|
|
/*
|
|
* celoxica_math.h
|
|
*
|
|
* Created on: Jun 30, 2016
|
|
* Author: syouness
|
|
*/
|
|
|
|
#ifndef __STDC_LIMIT_MACROS
|
|
#define __STDC_LIMIT_MACROS
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include "celoxica_defs.h"
|
|
|
|
static const uint64_t FACTORS_U64[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL,
|
|
1000ULL,
|
|
10000ULL,
|
|
100000ULL,
|
|
1000000ULL,
|
|
10000000ULL,
|
|
100000000ULL,
|
|
1000000000ULL,
|
|
10000000000ULL,
|
|
100000000000ULL,
|
|
1000000000000ULL,
|
|
10000000000000ULL,
|
|
100000000000000ULL,
|
|
1000000000000000ULL,
|
|
10000000000000000ULL,
|
|
100000000000000000ULL,
|
|
1000000000000000000ULL,
|
|
10000000000000000000ULL,
|
|
|
|
/* MAX possible factor
|
|
* 10,000,000,000,000,000,000ULL,
|
|
* 18,446,744,073,709,551,615
|
|
* */
|
|
};
|
|
|
|
static const int64_t FACTORS_S64[]
|
|
CLX_UNUSED = {
|
|
1LL,
|
|
10LL,
|
|
100LL,
|
|
1000LL,
|
|
10000LL,
|
|
100000LL,
|
|
1000000LL,
|
|
10000000LL,
|
|
100000000LL,
|
|
1000000000LL,
|
|
10000000000LL,
|
|
100000000000LL,
|
|
1000000000000LL,
|
|
10000000000000LL,
|
|
100000000000000LL,
|
|
1000000000000000LL,
|
|
10000000000000000LL,
|
|
100000000000000000LL
|
|
|
|
/* MAX possible factor
|
|
* 0,100,000,000,000,000,000ULL,
|
|
* 9,223,372,036,854,775,807
|
|
* */
|
|
};
|
|
|
|
static const uint32_t FACTORS_U32[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL,
|
|
1000ULL,
|
|
10000ULL,
|
|
100000ULL,
|
|
1000000ULL,
|
|
10000000ULL,
|
|
100000000ULL,
|
|
1000000000ULL
|
|
/* MAX possible factor
|
|
* 1,000,000,000ULL,
|
|
* 4,294,967,295
|
|
* */
|
|
};
|
|
|
|
static const int32_t FACTORS_S32[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL,
|
|
1000ULL,
|
|
10000ULL,
|
|
100000ULL,
|
|
1000000ULL,
|
|
10000000ULL,
|
|
100000000ULL,
|
|
1000000000ULL
|
|
/* MAX possible factor
|
|
* 1,000,000,000ULL,
|
|
* 2,147,483,647
|
|
* */
|
|
};
|
|
|
|
static const uint16_t FACTORS_U16[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL,
|
|
1000ULL,
|
|
10000ULL
|
|
/* MAX possible factor
|
|
* 10,000ULL,
|
|
* 65,535
|
|
* */
|
|
};
|
|
|
|
static const int16_t FACTORS_S16[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL,
|
|
1000ULL,
|
|
10000ULL
|
|
/* MAX possible factor
|
|
* 10,000ULL,
|
|
* 32,767
|
|
* */
|
|
};
|
|
|
|
static const uint8_t FACTORS_U8[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL
|
|
/* MAX possible factor
|
|
* 100ULL,
|
|
* 255
|
|
* */
|
|
};
|
|
|
|
static const int8_t FACTORS_S8[]
|
|
CLX_UNUSED = {
|
|
1ULL,
|
|
10ULL,
|
|
100ULL
|
|
/* MAX possible factor
|
|
* 100ULL,
|
|
* 128
|
|
* */
|
|
};
|
|
|
|
|
|
static inline uint64_t Pow10U64(uint64_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U64)))
|
|
{
|
|
return UINT64_MAX;
|
|
}
|
|
return mant * FACTORS_U64[exp];
|
|
}
|
|
|
|
static inline uint64_t Frac10U64(uint64_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U64)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant / FACTORS_U64[exp];
|
|
}
|
|
|
|
|
|
static inline int64_t Pow10S64(int64_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S64)))
|
|
{
|
|
return INT64_MAX;
|
|
}
|
|
return mant * FACTORS_S64[exp];
|
|
}
|
|
|
|
static inline int64_t Frac10S64(int64_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S64)))
|
|
{
|
|
return INT64_MIN;
|
|
}
|
|
return mant / FACTORS_S64[exp];
|
|
}
|
|
|
|
static inline uint32_t Pow10U32(uint32_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U32)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant * FACTORS_U32[exp];
|
|
}
|
|
|
|
static inline uint32_t Frac10U32(uint32_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U32)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant / FACTORS_U32[exp];
|
|
}
|
|
|
|
|
|
static inline int32_t Pow10S32(int32_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S32)))
|
|
{
|
|
return INT32_MAX;
|
|
}
|
|
return mant * FACTORS_S32[exp];
|
|
}
|
|
|
|
static inline int32_t Frac10S32(int32_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S32)))
|
|
{
|
|
return INT32_MIN;
|
|
}
|
|
return mant / FACTORS_S32[exp];
|
|
}
|
|
|
|
static inline uint16_t Pow10U16(uint16_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U16)))
|
|
{
|
|
return UINT16_MAX;
|
|
}
|
|
return mant * FACTORS_U16[exp];
|
|
}
|
|
|
|
static inline uint16_t Frac10U16(uint16_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U16)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant / FACTORS_U16[exp];
|
|
}
|
|
|
|
|
|
static inline int16_t Pow10S16(int16_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S16)))
|
|
{
|
|
return INT16_MAX;
|
|
}
|
|
return mant * FACTORS_S16[exp];
|
|
}
|
|
|
|
static inline int16_t Frac10S16(int16_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S16)))
|
|
{
|
|
return INT16_MIN;
|
|
}
|
|
return mant / FACTORS_S16[exp];
|
|
}
|
|
|
|
static inline uint8_t Pow10U8(uint8_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U8)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant * FACTORS_U8[exp];
|
|
}
|
|
|
|
static inline uint16_t Frac10U8(uint8_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_U8)))
|
|
{
|
|
return 0;
|
|
}
|
|
return mant / FACTORS_U8[exp];
|
|
}
|
|
|
|
|
|
static inline int8_t Pow10S8(int8_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S8)))
|
|
{
|
|
return INT8_MAX;
|
|
}
|
|
return mant * FACTORS_S8[exp];
|
|
}
|
|
|
|
static inline int8_t Frac10S8(int8_t mant, uint8_t exp)
|
|
{
|
|
if (CLX_UNLIKELY(exp >= CLX_ARRAY_COUNT(FACTORS_S8)))
|
|
{
|
|
return INT8_MIN;
|
|
}
|
|
return mant / FACTORS_S8[exp];
|
|
}
|
|
|
|
|
|
#endif /* BASE_UTILS_INCLUDE_CELOXICA_MATH_H_ */
|