log 2 compat

Need to support math:log2 in early versions of OTP (for Riak)
This commit is contained in:
Martin Sumner 2018-10-24 15:42:21 +01:00
parent 6d62803567
commit f4c65597ea
3 changed files with 41 additions and 1 deletions

View file

@ -1,8 +1,10 @@
{erl_opts, [warnings_as_errors, {erl_opts, [warnings_as_errors,
{platform_define, "^2[0-1]{1}", fsm_deprecated}, {platform_define, "^2[0-1]{1}", fsm_deprecated},
{platform_define, "^1[7-8]{1}", old_rand}, {platform_define, "^1[7-8]{1}", old_rand},
{platform_define, "^17", no_log2},
{platform_define, "^R", no_sync}, {platform_define, "^R", no_sync},
{platform_define, "^R", old_rand}, {platform_define, "^R", old_rand},
{platform_define, "^R", no_log2},
{platform_define, "^R", slow_test}]}. {platform_define, "^R", slow_test}]}.
{xref_checks, [undefined_function_calls,undefined_functions]}. {xref_checks, [undefined_function_calls,undefined_functions]}.

38
src/leveled_math.erl Normal file
View file

@ -0,0 +1,38 @@
%% Handle missing log2 prior to OTP18
-module(leveled_math).
%% API
-export([
log2/1
]).
-include_lib("eunit/include/eunit.hrl").
%%%===================================================================
%%% Use log2
%%%===================================================================
-ifndef(no_log2).
log2(X) ->
math:log2(X).
-else.
%%%===================================================================
%%% Old (r18) random style functions
%%%===================================================================
log2(X) ->
math:log(X) / 0.6931471805599453.
-endif.
-ifdef(TEST).
log2_test() ->
?assertMatch(8, round(log2(256))),
?assertMatch(16, round(log2(65536))).
-endif.

View file

@ -413,7 +413,7 @@ adjust_segmentmatch_list(SegmentList, CompareSize, StoreSize) ->
StoreSizeI = get_size(StoreSize), StoreSizeI = get_size(StoreSize),
if CompareSizeI =< StoreSizeI -> if CompareSizeI =< StoreSizeI ->
ExpItems = StoreSizeI div CompareSizeI - 1, ExpItems = StoreSizeI div CompareSizeI - 1,
ShiftFactor = trunc(math:log2(CompareSizeI * ?L2_CHUNKSIZE)), ShiftFactor = round(leveled_math:log2(CompareSizeI * ?L2_CHUNKSIZE)),
ExpList = ExpList =
lists:map(fun(X) -> X bsl ShiftFactor end, lists:seq(1, ExpItems)), lists:map(fun(X) -> X bsl ShiftFactor end, lists:seq(1, ExpItems)),
UpdSegmentList = UpdSegmentList =