diff options
author | Guilherme Andrade <[email protected]> | 2017-03-22 22:15:15 +0000 |
---|---|---|
committer | Guilherme Andrade <[email protected]> | 2017-03-22 22:15:15 +0000 |
commit | c84e541b78cb9ee63a02db2240903ddd6131793a (patch) | |
tree | 245382e7310e5bcbec9a98a0aa3b7b4b7dc4b008 /lib/crypto/src | |
parent | ec1e5bc7ff8101b7db4fa5d67bbbed652a9238c8 (diff) | |
download | otp-c84e541b78cb9ee63a02db2240903ddd6131793a.tar.gz otp-c84e541b78cb9ee63a02db2240903ddd6131793a.tar.bz2 otp-c84e541b78cb9ee63a02db2240903ddd6131793a.zip |
Attempt faster approach to strong random floats
Diffstat (limited to 'lib/crypto/src')
-rw-r--r-- | lib/crypto/src/crypto.erl | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl index 777b5cd1c6..d8bc13b537 100644 --- a/lib/crypto/src/crypto.erl +++ b/lib/crypto/src/crypto.erl @@ -49,6 +49,9 @@ %% This should correspond to the similar macro in crypto.c -define(MAX_BYTES_TO_NIF, 20000). %% Current value is: erlang:system_info(context_reductions) * 10 +%% Used by strong_rand_float/0 +-define(HALF_DBL_EPSILON, 1.1102230246251565e-16). % math:pow(2, -53) + %%-type ecdsa_digest_type() :: 'md5' | 'sha' | 'sha256' | 'sha384' | 'sha512'. -type crypto_integer() :: binary() | integer(). %%-type ec_named_curve() :: atom(). @@ -342,14 +345,8 @@ strong_rand_range(BinRange) when is_binary(BinRange) -> strong_rand_range_nif(_BinRange) -> ?nif_stub. strong_rand_float() -> - % This could be optimized by having its own NIF - Sign = 0, % positive - Exponent = 1023, % on the interval [1.0, 2.0[ - BinFraction = strong_rand_range(1 bsl 52), % the whole interval above - Fraction = bin_to_int(BinFraction), - <<Value:64/big-float>> = <<Sign:1, Exponent:11, Fraction:52>>, - Value - 1.0. - + WholeRange = strong_rand_range(1 bsl 53), + ?HALF_DBL_EPSILON * bytes_to_integer(WholeRange). rand_uniform(From,To) when is_binary(From), is_binary(To) -> case rand_uniform_nif(From,To) of |