| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef IGA_FLOATS_HPP |
| 10 |
#define IGA_FLOATS_HPP |
| 11 |
|
| 12 |
#include <cmath> |
| 13 |
#include <cstdint> |
| 14 |
#include <iostream> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
// Provides utilities for dealing with floating point numbers including some |
| 18 |
// minimal fp16 support. |
| 19 |
|
| 20 |
#if !defined(_WIN32) || (_MSC_VER >= 1800) |
| 21 |
// GCC and VS2013 and higher support these |
| 22 |
#define IS_NAN(X) std::isnan(X) |
| 23 |
#define IS_INF(X) std::isinf(X) |
| 24 |
#else |
| 25 |
#define IS_NAN(X) ((X) != (X)) |
| 26 |
#define IS_INF(X) (!IS_NAN(X) && IS_NAN((X) - (X))) |
| 27 |
#endif |
| 28 |
|
| 29 |
namespace iga { |
| 30 |
|
| 31 |
// formats a floating point value in decimal if possible |
| 32 |
// otherwise it falls back to hex |
| 33 |
void FormatFloat(std::ostream &os, double d); |
| 34 |
void FormatFloat(std::ostream &os, float f); |
| 35 |
void FormatFloat(std::ostream &os, uint16_t h); |
| 36 |
void FormatFloat(std::ostream &os, uint8_t q); // GEN's 8-bit restricted float |
| 37 |
|
| 38 |
// These functions exist since operations on NaN values might change the NaN |
| 39 |
// payload. E.g. An sNan might convert to a qNan during a cast |
| 40 |
float ConvertDoubleToFloat(double d); |
| 41 |
uint32_t ConvertDoubleToFloatBits(double d); |
| 42 |
uint16_t ConvertFloatToHalf(float f); |
| 43 |
static inline uint16_t ConvertDoubleToHalf(double d) { |
| 44 |
return ConvertFloatToHalf(ConvertDoubleToFloat(d)); |
| 45 |
} |
| 46 |
float ConvertHalfToFloat(uint16_t u16); |
| 47 |
double ConvertFloatToDouble(float f32); |
| 48 |
|
| 49 |
// This expands Intel GEN's restricted 8-bit format |
| 50 |
float ConvertQuarterToFloatGEN(uint8_t u8); |
| 51 |
|
| 52 |
// Various raw accessors to convert between bits and float |
| 53 |
static inline uint16_t FloatToBits(uint16_t f) { return f; } |
| 54 |
static inline uint64_t FloatToBits(double f) { |
| 55 |
union { |
| 56 |
double f; |
| 57 |
uint64_t i; |
| 58 |
} u; |
| 59 |
u.f = f; |
| 60 |
return u.i; |
| 61 |
} |
| 62 |
static inline uint32_t FloatToBits(float f) { |
| 63 |
union { |
| 64 |
float f; |
| 65 |
uint32_t i; |
| 66 |
} u; |
| 67 |
u.f = f; |
| 68 |
return u.i; |
| 69 |
} |
| 70 |
|
| 71 |
static inline uint16_t FloatFromBits(uint16_t f) { return f; } |
| 72 |
static inline float FloatFromBits(uint32_t f) { |
| 73 |
union { |
| 74 |
float f; |
| 75 |
uint32_t i; |
| 76 |
} u; |
| 77 |
u.i = f; |
| 78 |
return u.f; |
| 79 |
} |
| 80 |
static inline double FloatFromBits(uint64_t f) { |
| 81 |
union { |
| 82 |
double f; |
| 83 |
uint64_t i; |
| 84 |
} u; |
| 85 |
u.i = f; |
| 86 |
return u.f; |
| 87 |
} |
| 88 |
|
| 89 |
bool IsNaN(uint16_t u16); |
| 90 |
bool IsInf(uint16_t u16); |
| 91 |
|
| 92 |
static const uint64_t F64_SIGN_BIT = 0x8000000000000000ull; |
| 93 |
static const uint64_t F64_EXP_MASK = 0x7FF0000000000000ull; |
| 94 |
static const uint64_t F64_MANT_MASK = 0x000FFFFFFFFFFFFFull; |
| 95 |
static const uint64_t F64_QNAN_BIT = 0x0008000000000000ull; |
| 96 |
static const uint32_t F32_SIGN_BIT = 0x80000000; |
| 97 |
static const uint32_t F32_EXP_MASK = 0x7F800000; |
| 98 |
static const uint32_t F32_MANT_MASK = 0x007FFFFF; |
| 99 |
static const uint32_t F32_QNAN_BIT = 0x00400000; |
| 100 |
static const uint16_t F16_SIGN_BIT = 0x8000; |
| 101 |
static const uint16_t F16_EXP_MASK = 0x7C00; |
| 102 |
static const uint16_t F16_MANT_MASK = 0x03FF; |
| 103 |
static const uint16_t F16_QNAN_BIT = 0x0200; |
| 104 |
|
| 105 |
// Parses the lexical FLTLIT pattern to into a double |
| 106 |
bool ParseFLTLIT(const std::string &string, double &d); |
| 107 |
|
| 108 |
} // namespace iga |
| 109 |
|
| 110 |
#endif // IGA_FLOATS_HPP |