Add constexpr_clamp_cast<> in folly/ConstexprMath.h
Summary: constexpr_clamp_cast<> provides sane numeric conversions from float point numbers to integral numbers, and between different types of integral numbers. When doing clamp_cast<DstT>(value), if `value` is in valid range of DstT, it will give correct result in DstT, equal to `value`. If `value` is outside the representable range of DstT, it will be clamped to MAX or MIN in DstT. Float NaNs are converted to 0 in integral type. For comparison, static_cast<> will return garbage results when SrcT is float point number and it's outside DstT valid range. This is because float-cast-overflow is undefined behavior. Reviewed By: yfeldblum Differential Revision: D7716736 fbshipit-source-id: 5899e4b20cdd997d0c7b121ddaababbc8c748301
Showing
Please register or sign in to comment