Skip to content

Commit

Permalink
Fix compilation in strict C++11/C++14 mode
Browse files Browse the repository at this point in the history
Hexadecimal floating-point literals is a C99 feature.
gcc/icc/clang support hex-float literals in C++ as an extension, and fail to compile such code in strict C++11 or C++14 mode (i.e. -std=c++11)
This patch adds a work-around for when modes where hex-float literals are not supported..
  • Loading branch information
Marat Dukhan committed Sep 23, 2018
1 parent 4b37bd3 commit 5e2bd58
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/fp16/fp16.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ static inline float fp16_ieee_to_fp32_value(uint16_t h) {
* operate on denormal inputs, and do not produce denormal results.
*/
const uint32_t exp_offset = UINT32_C(0xE0) << 23;
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const float exp_scale = 0x1.0p-112f;
#else
const float exp_scale = fp32_from_bits(UINT32_C(0x7800000));
#endif
const float normalized_value = fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;

/*
Expand Down Expand Up @@ -207,8 +211,13 @@ static inline float fp16_ieee_to_fp32_value(uint16_t h) {
* floating-point operations and bitcasts between integer and floating-point variables.
*/
static inline uint16_t fp16_ieee_from_fp32_value(float f) {
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const float scale_to_inf = 0x1.0p+112f;
const float scale_to_zero = 0x1.0p-110f;
#else
const float scale_to_inf = fp32_from_bits(UINT32_C(0x77800000));
const float scale_to_zero = fp32_from_bits(UINT32_C(0x08800000));
#endif
float base = (fabsf(f) * scale_to_inf) * scale_to_zero;

const uint32_t w = fp32_to_bits(f);
Expand Down
12 changes: 12 additions & 0 deletions include/fp16/psimd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ PSIMD_INTRINSIC psimd_f32 fp16_ieee_to_fp32_psimd(psimd_u16 half) {
const psimd_u32 shr3_nonsign = (word + word) >> psimd_splat_u32(4);

const psimd_u32 exp_offset = psimd_splat_u32(UINT32_C(0x70000000));
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const psimd_f32 exp_scale = psimd_splat_f32(0x1.0p-112f);
#else
const psimd_f32 exp_scale = psimd_splat_f32(fp32_from_bits(UINT32_C(0x7800000)));
#endif
const psimd_f32 norm_nonsign = psimd_mul_f32((psimd_f32) (shr3_nonsign + exp_offset), exp_scale);

const psimd_u16 magic_mask = psimd_splat_u16(UINT16_C(0x3E80));
Expand All @@ -41,7 +45,11 @@ PSIMD_INTRINSIC psimd_f32x2 fp16_ieee_to_fp32x2_psimd(psimd_u16 half) {
const psimd_u32 shr3_nonsign_hi = (word_hi + word_hi) >> psimd_splat_u32(4);

const psimd_u32 exp_offset = psimd_splat_u32(UINT32_C(0x70000000));
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const psimd_f32 exp_scale = psimd_splat_f32(0x1.0p-112f);
#else
const psimd_f32 exp_scale = psimd_splat_f32(fp32_from_bits(UINT32_C(0x7800000)));
#endif
const psimd_f32 norm_nonsign_lo = psimd_mul_f32((psimd_f32) (shr3_nonsign_lo + exp_offset), exp_scale);
const psimd_f32 norm_nonsign_hi = psimd_mul_f32((psimd_f32) (shr3_nonsign_hi + exp_offset), exp_scale);

Expand Down Expand Up @@ -77,7 +85,11 @@ PSIMD_INTRINSIC psimd_f32 fp16_alt_to_fp32_psimd(psimd_u16 half) {
#else
const psimd_u32 exp_offset = psimd_splat_u32(UINT32_C(0x38000000));
const psimd_f32 nonsign = (psimd_f32) (shr3_nonsign + exp_offset);
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) || defined(__GNUC__) && !defined(__STRICT_ANSI__)
const psimd_f32 denorm_bias = psimd_splat_f32(0x1.0p-14f);
#else
const psimd_f32 denorm_bias = psimd_splat_f32(fp32_from_bits(UINT32_C(0x38800000)));
#endif
return (psimd_f32) (sign | (psimd_s32) psimd_sub_f32(psimd_add_f32(nonsign, nonsign), psimd_max_f32(nonsign, denorm_bias)));
#endif
}
Expand Down

0 comments on commit 5e2bd58

Please sign in to comment.