New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small miniAOD improvements #20367
Small miniAOD improvements #20367
Changes from all commits
863334d
68f737b
78b19b4
9f31d22
d5d893e
c5e1951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
#define libminifloat_h | ||
#include "FWCore/Utilities/interface/thread_safety_macros.h" | ||
#include <cstdint> | ||
#include <cassert> | ||
#include <algorithm> | ||
|
||
// ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf | ||
class MiniFloatConverter { | ||
|
@@ -54,27 +56,52 @@ class MiniFloatConverter { | |
return conv.flt; | ||
} | ||
|
||
class ReduceMantissaToNbitsRounding { | ||
public: | ||
ReduceMantissaToNbitsRounding(int bits) : | ||
shift(23-bits), mask((0xFFFFFFFF >> (shift)) << (shift)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like this duplicates the functionality of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the solution in 9f31d22 ok for you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it looks OK. |
||
test(1 << (shift-1)), maxn((1<<bits)-2) { | ||
assert(bits <= 23); // "max mantissa size is 23 bits" | ||
} | ||
float operator()(float f) const { | ||
constexpr uint32_t low23 = (0x007FFFFF); // mask to keep lowest 23 bits = mantissa | ||
constexpr uint32_t hi9 = (0xFF800000); // mask to keep highest 9 bits = the rest | ||
union { float flt; uint32_t i32; } conv; | ||
conv.flt=f; | ||
if (conv.i32 & test) { // need to round | ||
uint32_t mantissa = (conv.i32 & low23) >> shift; | ||
if (mantissa < maxn) mantissa++; | ||
conv.i32 = (conv.i32 & hi9) | (mantissa << shift); | ||
} else { | ||
conv.i32 &= mask; | ||
} | ||
return conv.flt; | ||
} | ||
private: | ||
const int shift; | ||
const uint32_t mask, test, maxn; | ||
}; | ||
|
||
template<int bits> | ||
inline static float reduceMantissaToNbitsRounding(const float &f) | ||
{ | ||
static_assert(bits <= 23,"max mantissa size is 23 bits"); | ||
constexpr int shift = (23-bits); // bits I throw away | ||
constexpr uint32_t mask = (0xFFFFFFFF >> (shift)) << (shift); // mask for truncation | ||
constexpr uint32_t test = 1 << (shift-1); // most significant bit I throw away | ||
constexpr uint32_t low23 = (0x007FFFFF); // mask to keep lowest 23 bits = mantissa | ||
constexpr uint32_t hi9 = (0xFF800000); // mask to keep highest 9 bits = the rest | ||
constexpr uint32_t maxn = (1<<bits)-2; // max number I can increase before overflowing | ||
union { float flt; uint32_t i32; } conv; | ||
conv.flt=f; | ||
if (conv.i32 & test) { // need to round | ||
uint32_t mantissa = (conv.i32 & low23) >> shift; | ||
if (mantissa < maxn) mantissa++; | ||
conv.i32 = (conv.i32 & hi9) | (mantissa << shift); | ||
} else { | ||
conv.i32 &= mask; | ||
} | ||
return conv.flt; | ||
static const ReduceMantissaToNbitsRounding reducer(bits); | ||
return reducer(f); | ||
} | ||
|
||
|
||
|
||
inline static float reduceMantissaToNbitsRounding(float f, int bits) | ||
{ | ||
return ReduceMantissaToNbitsRounding(bits)(f); | ||
} | ||
|
||
template<typename InItr, typename OutItr> | ||
static void reduceMantissaToNbitsRounding(int bits, InItr begin, InItr end, OutItr out) | ||
{ | ||
std::transform(begin, end, out, ReduceMantissaToNbitsRounding(bits)); | ||
} | ||
|
||
|
||
inline static float max() { | ||
union { float flt; uint32_t i32; } conv; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make a backport for this part to 93X and 92X.