[erlang-questions] Pattern matching of literal small/big/bignum integers and binary search
Sun Jun 16 10:39:40 CEST 2019
On Sun, 16 Jun 2019 18:27:58 +1000, Edmond Begumisa
> Hello all,
> I have some questions about how pattern matches on integer literals are
> evaluated by the emulator.
> As an example, say I have the following function which expects an
> integer that's a power of 2 and returns corresponding position of the
> lone "on" bit...
> single_cardinalty_to_pos(2#1) -> 1;
> single_cardinalty_to_pos(2#10) -> 2;
> single_cardinalty_to_pos(2#100) -> 3;
> single_cardinalty_to_pos(2#100...0) -> 128.
> I'm trying to gauge whether or not I've correctly understood the
> resultant select_val BEAM assembly instruction w.r.t gen_select_val 
> in beam_load.c (without even being at all certain that gen_select_val is
> even relevant).
> I could use some help to determine if the following assumptions are
> A) That rather than brute forcing, the emulator will perform a
> binary search  for the function argument against all the literals
> from all the clauses which is the purpose of quick-sorting the
> select_val list, and as such,
> B) If I want to increase the upper limit that the function can
> handle, I can simply add more clauses rather than trying to implement a
> binary search for the "on" bit myself using various masks and bit shifts.
> C) That the emulator will be smart about whether on not the function
> argument is an 8 bit, 32/64 bit integer versus a bignum integer and thus
> know what candidates can be excluded from the binary search so there's
> no benefit to splitting the function to handle different ranges.
> D) That it's not unreasonable in this case to have even say, 1024
> clauses in order to handle bignum integers of up to 1024 bits. The only
> downside will be longer compile times and possibly longer loading of the
Provided I don't go overboard with the size of the bignums due to the
potential impact on runtime responsiveness (in addition to pattern
matches, I believe the bignum arith operations are not broken-up for
> If these assumptions are correct, then I could have the emulator do a
> lot of work for me with a number of similar functions and avoid writing
> a lot code.
> Thanks in advance.
> - Edmond -
> Using Opera's mail client: http://www.opera.com/mail/
Using Opera's mail client: http://www.opera.com/mail/
More information about the erlang-questions