1#!/usr/bin/env escript 2%% -*- erlang -*- 3%%! +A0 4%% 5%% %CopyrightBegin% 6%% 7%% Copyright Ericsson AB 2017-2019. All Rights Reserved. 8%% 9%% Licensed under the Apache License, Version 2.0 (the "License"); 10%% you may not use this file except in compliance with the License. 11%% You may obtain a copy of the License at 12%% 13%% http://www.apache.org/licenses/LICENSE-2.0 14%% 15%% Unless required by applicable law or agreed to in writing, software 16%% distributed under the License is distributed on an "AS IS" BASIS, 17%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18%% See the License for the specific language governing permissions and 19%% limitations under the License. 20%% 21%% %CopyrightEnd% 22%% 23 24-mode(compile). 25 26-define(MOD, "io_lib_format_ryu_table"). 27 28-define(TABLE_SIZE, 326). 29-define(INV_TABLE_SIZE, 342). 30 31-define(POW5_BITCOUNT, 125). 32-define(POW5_INV_BITCOUNT, 125). 33 34main(_) -> 35 Values = [ values(X) || X <- lists:seq(0, ?TABLE_SIZE - 1)], 36 InvValues = [ inv_values(X) || X <- lists:seq(0, ?INV_TABLE_SIZE - 1)], 37 38 %% Make module 39 {ok, Out} = file:open("../src/" ++ ?MOD ++ ".erl", [write]), 40 gen_file(Out, Values, InvValues), 41 ok = file:close(Out), 42 ok. 43 44%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 45 46inv_values(X) -> 47 Pow = pow5(X), 48 Pow5len = log2floor(Pow), 49 J = Pow5len + ?POW5_INV_BITCOUNT - 1, 50 Inv = ((1 bsl J) div Pow) + 1, 51 {X, Inv}. 52 53values(X) -> 54 Pow = pow5(X), 55 Pow5len = log2floor(Pow), 56 Pow5 = Pow bsr (Pow5len - ?POW5_BITCOUNT), 57 {X, Pow5}. 58 59pow5(0) -> 60 1; 61pow5(1) -> 62 5; 63pow5(X) -> 64 5 * pow5(X - 1). 65 66log2floor(Int) when is_integer(Int), Int > 0 -> 67 log2floor(Int, 0). 68 69log2floor(0, N) -> 70 N; 71log2floor(Int, N) -> 72 log2floor(Int bsr 1, 1 + N). 73 74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 75 76gen_file(Fd, Values, InvValues) -> 77 gen_header(Fd), 78 gen_pow5_static(Fd), 79 gen_table(Fd, Values), 80 gen_inv_table(Fd, InvValues), 81 ok. 82 83gen_header(Fd) -> 84 io:put_chars(Fd, "%%\n%% this file is generated do not modify\n"), 85 io:put_chars(Fd, "%% see ../script/generate_ryu_table.escript\n\n"), 86 io:put_chars(Fd, "-module(" ++ ?MOD ++").\n"), 87 io:put_chars(Fd, "-export([pow5_bitcount/0, pow5_inv_bitcount/0, value/1, inv_value/1]).\n\n"), 88 ok. 89 90gen_pow5_static(Fd) -> 91 io:put_chars(Fd, "-spec pow5_bitcount() -> integer().\n"), 92 io:format(Fd, "pow5_bitcount() -> ~p.~n~n", [?POW5_BITCOUNT]), 93 io:put_chars(Fd, "-spec pow5_inv_bitcount() -> integer().\n"), 94 io:format(Fd, "pow5_inv_bitcount() -> ~p.~n~n", [?POW5_INV_BITCOUNT]), 95 ok. 96 97gen_table(Fd, Values) -> 98 io:put_chars(Fd, "-spec value(integer()) -> integer().\n"), 99 [io:format(Fd, "value(~p) -> ~p;~n", [Key, Val]) || {Key,Val} <- Values], 100 io:put_chars(Fd, "value(_) -> error(function_clause).\n\n"), 101 ok. 102 103gen_inv_table(Fd, Values) -> 104 io:put_chars(Fd, "-spec inv_value(integer()) -> integer().\n"), 105 [io:format(Fd, "inv_value(~p) -> ~p;~n", [Key, Val]) || {Key,Val} <- Values], 106 io:put_chars(Fd, "inv_value(_) -> error(function_clause).\n"), 107 ok. 108 109