Commit 26956f1d authored by Umang Yadav's avatar Umang Yadav
Browse files

Remove NOLINTS

parent dfb35a68
...@@ -55,23 +55,23 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -55,23 +55,23 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
uint32_t sign = 0; uint32_t sign = 0;
if constexpr(sizeof(T) == 4) if constexpr(sizeof(T) == 4)
{ {
head = x & 0xFF800000; // NOLINT head = x & 0xFF800000;
mantissa = x & 0x7FFFFF; // NOLINT mantissa = x & 0x7FFFFF;
exponent = (head >> 23) & 0xFF; // NOLINT exponent = (head >> 23) & 0xFF;
sign = head >> 31; // NOLINT sign = head >> 31;
bias = 127; bias = 127;
} }
else else
{ {
head = x & 0xFC00; // NOLINT head = x & 0xFC00;
mantissa = x & 0x3FF; // NOLINT mantissa = x & 0x3FF;
exponent = (head >> 10) & 0x1F; // NOLINT exponent = (head >> 10) & 0x1F;
sign = head >> 15; // NOLINT sign = head >> 15;
bias = 15; bias = 15;
} }
uint32_t signed_inf = (sign << 7) + (((1 << We) - 1) << Wm); // NOLINT uint32_t signed_inf = (sign << 7) + (((1 << We) - 1) << Wm);
uint32_t signed_all_ones = (sign << 7) + ((((1 << We) - 1) << Wm) + ((1 << Wm) - 1)); // NOLINT uint32_t signed_all_ones = (sign << 7) + ((((1 << We) - 1) << Wm) + ((1 << Wm) - 1));
// Calcualte maximum singed value FLT_MAX, FLT_MIN // Calcualte maximum singed value FLT_MAX, FLT_MIN
uint32_t signed_max = signed_all_ones; uint32_t signed_max = signed_all_ones;
...@@ -81,8 +81,8 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -81,8 +81,8 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
// Deal with inf and NaNs // Deal with inf and NaNs
if(NegativeZeroNan) // For the FNUZ cases, it is simple just return NaNs if(NegativeZeroNan) // For the FNUZ cases, it is simple just return NaNs
{ {
if((sizeof(T) == 4 and ((x & 0x7F800000) == 0x7F800000)) or // NOLINT if((sizeof(T) == 4 and ((x & 0x7F800000) == 0x7F800000)) or
(sizeof(T) == 2 and ((x & 0x7C00) == 0x7C00))) // NOLINT (sizeof(T) == 2 and ((x & 0x7C00) == 0x7C00)))
return 0x80; return 0x80;
} }
else else
...@@ -91,10 +91,10 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -91,10 +91,10 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
uint32_t nan_mantissa = 1; uint32_t nan_mantissa = 1;
for(auto i = 1; i < Wm; ++i) for(auto i = 1; i < Wm; ++i)
{ {
nan_mantissa |= (nan_mantissa << 1); // NOLINT nan_mantissa |= (nan_mantissa << 1);
} }
if((sizeof(T) == 4 and ((x & 0x7F800000) == 0x7F800000)) or // NOLINT if((sizeof(T) == 4 and ((x & 0x7F800000) == 0x7F800000)) or
(sizeof(T) == 2 and ((x & 0x7C00) == 0x7C00))) // NOLINT (sizeof(T) == 2 and ((x & 0x7C00) == 0x7C00)))
{ {
// infinity // infinity
if(mantissa == 0) if(mantissa == 0)
...@@ -124,7 +124,7 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -124,7 +124,7 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
exponent and mantissa again*/ exponent and mantissa again*/
// For IEEE bias mode, the bias is 2^(k-1) -1 where k is the width of exponent bits // For IEEE bias mode, the bias is 2^(k-1) -1 where k is the width of exponent bits
const int f8_bias = (1 << (We - 1u)) - 1 + (NegativeZeroNan ? 1 : 0); // NOLINT const int f8_bias = (1 << (We - 1u)) - 1 + (NegativeZeroNan ? 1 : 0);
const int f8_denormal_act_exponent = 1 - f8_bias; // actual exponent of f8 denormal const int f8_denormal_act_exponent = 1 - f8_bias; // actual exponent of f8 denormal
/* act_exponent is the actual exponent of fp32/fp16 (after subtracting bias) /* act_exponent is the actual exponent of fp32/fp16 (after subtracting bias)
f8_exponent is the converted f8 exponent with bias encoding f8_exponent is the converted f8 exponent with bias encoding
...@@ -166,9 +166,9 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -166,9 +166,9 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
} }
mantissa += (1u << mfmt); // Add the implicit 1 into mantissa mantissa += (1u << mfmt); // Add the implicit 1 into mantissa
} }
// NOLINTNEXTLINE
bool midpoint = (mantissa & ((1 << (mfmt - Wm + exponent_diff)) - 1)) == bool midpoint = (mantissa & ((1 << (mfmt - Wm + exponent_diff)) - 1)) ==
(1 << (mfmt - Wm + exponent_diff - 1)); // NOLINT (1 << (mfmt - Wm + exponent_diff - 1));
/* This part is a bit tricky. The judgment of whether it is a tie needs to be done before we /* This part is a bit tricky. The judgment of whether it is a tie needs to be done before we
shift right as shift right could rip off some residual part and make something not midpoint look shift right as shift right could rip off some residual part and make something not midpoint look
like midpoint. For example, the fp16 number 0x1002 (0 00100 0000000010), it is larger than like midpoint. For example, the fp16 number 0x1002 (0 00100 0000000010), it is larger than
...@@ -176,36 +176,35 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -176,36 +176,35 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
*/ */
if(exponent_diff > 0) if(exponent_diff > 0)
mantissa >>= exponent_diff; // NOLINT mantissa >>= exponent_diff;
else if(exponent_diff == -1) else if(exponent_diff == -1)
mantissa <<= -exponent_diff; // NOLINT mantissa <<= -exponent_diff;
bool implicit_one = mantissa & (1 << mfmt); // NOLINT bool implicit_one = mantissa & (1 << mfmt);
// if there is no implict 1, it means the f8 is denormal and need to adjust to denorm exponent // if there is no implict 1, it means the f8 is denormal and need to adjust to denorm exponent
f8_exponent = f8_exponent =
(act_exponent + exponent_diff) /*actual f8 exponent*/ + f8_bias - (implicit_one ? 0 : 1); (act_exponent + exponent_diff) /*actual f8 exponent*/ + f8_bias - (implicit_one ? 0 : 1);
// Now we have the exponent and mantissa adjusted // Now we have the exponent and mantissa adjusted
uint32_t drop_mask = (1u << (mfmt - Wm)) - 1; // NOLINT uint32_t drop_mask = (1u << (mfmt - Wm)) - 1;
bool odd = bool odd =
mantissa & (1u << (mfmt - Wm)); // if the least significant bit that is not truncated is 1 mantissa & (1u << (mfmt - Wm)); // if the least significant bit that is not truncated is 1
mantissa += (stoch ? rng : (midpoint ? (odd ? mantissa : mantissa - 1) : mantissa)) & // NOLINT mantissa += (stoch ? rng : (midpoint ? (odd ? mantissa : mantissa - 1) : mantissa)) & drop_mask;
drop_mask; // NOLINT
// Now we deal with overflow // Now we deal with overflow
if(f8_exponent == 0 and ((1 << mfmt) & mantissa)) // NOLINT if(f8_exponent == 0 and ((1 << mfmt) & mantissa))
{ {
f8_exponent = 1; // denormal overflow to become normal, promote exponent f8_exponent = 1; // denormal overflow to become normal, promote exponent
} }
else if((1 << (mfmt + 1)) & mantissa) // NOLINT else if((1 << (mfmt + 1)) & mantissa)
{ {
mantissa >>= 1; // NOLINT mantissa >>= 1;
f8_exponent++; f8_exponent++;
} }
mantissa >>= (mfmt - Wm); // NOLINT mantissa >>= (mfmt - Wm);
// above range: quantize to maximum possible float of the same sign // above range: quantize to maximum possible float of the same sign
const int max_exp = (1 << We) - (NegativeZeroNan ? 1 : 2); // NOLINT const int max_exp = (1 << We) - (NegativeZeroNan ? 1 : 2);
if(f8_exponent > max_exp) if(f8_exponent > max_exp)
{ {
if(Clip) if(Clip)
...@@ -221,9 +220,9 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0) ...@@ -221,9 +220,9 @@ constexpr uint8_t cast_to_f8(T f_x, bool stoch = false, uint32_t rng = 0)
} }
if(f8_exponent == 0 and mantissa == 0) if(f8_exponent == 0 and mantissa == 0)
return NegativeZeroNan ? 0 : (sign << 7); // NOLINT return NegativeZeroNan ? 0 : (sign << 7);
mantissa &= (1 << Wm) - 1; // NOLINT mantissa &= (1 << Wm) - 1;
return (sign << 7) | (f8_exponent << Wm) | mantissa; // NOLINT return (sign << 7) | (f8_exponent << Wm) | mantissa;
} }
// NOLINTEND // NOLINTEND
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment