From 46c7a85d70ee6d060a393ca4b22b06eef6fbb324 Mon Sep 17 00:00:00 2001 From: Stanislav Mayorov Date: Thu, 13 Dec 2018 11:39:16 +0700 Subject: Implement integer_to_list/2 and integer_to_binary/2 as CIFs This makes them roughly as fast as integer_to_list/1 and integer_to_binary/1. --- erts/emulator/beam/bif.c | 33 +++++++++++++++++++++++++++++++++ erts/emulator/beam/bif.tab | 7 +++++++ erts/emulator/beam/binary.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) (limited to 'erts/emulator') diff --git a/erts/emulator/beam/bif.c b/erts/emulator/beam/bif.c index 45b75a4e31..f1fac7f8d0 100644 --- a/erts/emulator/beam/bif.c +++ b/erts/emulator/beam/bif.c @@ -2883,6 +2883,39 @@ BIF_RETTYPE integer_to_list_1(BIF_ALIST_1) return res; } +BIF_RETTYPE integer_to_list_2(BIF_ALIST_2) +{ + Eterm res; + int base; + + if (is_not_integer(BIF_ARG_1) || is_not_integer(BIF_ARG_2)) { + BIF_ERROR(BIF_P, BADARG); + } + + base = unsigned_val(BIF_ARG_2); + if (base < 2 || base > 36) { + BIF_ERROR(BIF_P, BADARG); + } + + res = integer_to_list(BIF_P, BIF_ARG_1, base); + + if (is_non_value(res)) { + Eterm args[2]; + args[0] = BIF_ARG_1; + args[1] = BIF_ARG_2; + return erts_schedule_bif(BIF_P, + args, + BIF_I, + integer_to_list_2, + ERTS_SCHED_DIRTY_CPU, + am_erlang, + am_integer_to_list, + 2); + } + + return res; +} + /**********************************************************************/ /* diff --git a/erts/emulator/beam/bif.tab b/erts/emulator/beam/bif.tab index 0e218811d8..c96278b10c 100644 --- a/erts/emulator/beam/bif.tab +++ b/erts/emulator/beam/bif.tab @@ -731,3 +731,10 @@ bif erts_internal:counters_info/1 # bif erts_internal:spawn_system_process/3 + +# +# New in 21.3 +# + +bif erlang:integer_to_list/2 +bif erlang:integer_to_binary/2 diff --git a/erts/emulator/beam/binary.c b/erts/emulator/beam/binary.c index 982bf087b0..5f090e89d7 100644 --- a/erts/emulator/beam/binary.c +++ b/erts/emulator/beam/binary.c @@ -387,6 +387,39 @@ BIF_RETTYPE integer_to_binary_1(BIF_ALIST_1) return res; } +BIF_RETTYPE integer_to_binary_2(BIF_ALIST_2) +{ + Eterm res; + int base; + + if (is_not_integer(BIF_ARG_1) || is_not_integer(BIF_ARG_2)) { + BIF_ERROR(BIF_P, BADARG); + } + + base = unsigned_val(BIF_ARG_2); + if (base < 2 || base > 36) { + BIF_ERROR(BIF_P, BADARG); + } + + res = integer_to_binary(BIF_P, BIF_ARG_1, base); + + if (is_non_value(res)) { + Eterm args[2]; + args[0] = BIF_ARG_1; + args[1] = BIF_ARG_2; + return erts_schedule_bif(BIF_P, + args, + BIF_I, + integer_to_binary_2, + ERTS_SCHED_DIRTY_CPU, + am_erlang, + am_integer_to_binary, + 2); + } + + return res; +} + #define ERTS_B2L_BYTES_PER_REDUCTION 256 typedef struct { -- cgit v1.2.3