HomePhabricator

[compiler-rt] Add ConsumeProbability and ConsumeFloatingPoint methods to FDP.

Authored by Dor1s on Aug 8 2019, 12:49 PM.

Description

[compiler-rt] Add ConsumeProbability and ConsumeFloatingPoint methods to FDP.

Summary:
Also slightly cleaned up the comments and changed the header's extension
back to .h as per comments on https://reviews.llvm.org/D65812.

New methods added:

  • ConsumeProbability returns [0.0, 1.0] by consuming an unsigned integer value from the input data and dividing that value by the integer's max value.
  • ConsumeFloatingPointInRange returns a floating point value in the given range. Relies on ConsumeProbability method. This method does not have the limitation of std::uniform_real_distribution that requires the given range to be <= the floating point type's max. If the range is too large, this implementation will additionally call ConsumeBool to decide whether the result will be in the first or the second half of the range.
  • ConsumeFloatingPoint returns a floating point value in the range [std::numeric_limits<T>::lowest(), std::numeric_limits<T>::min()].

Tested on Linux, Mac, Windows.

Reviewers: morehouse

Reviewed By: morehouse

Subscribers: kubamracek, mgorny, dberris, delcypher, #sanitizers, llvm-commits

Tags: #llvm, #sanitizers

Differential Revision: https://reviews.llvm.org/D65905

llvm-svn: 368331