lykos
Representation of big integer in heap - connection between the header and data words
Hey all,
I was looking into the representation of big integers as boxed terms in heap. I understood how the header word and data words contain information in bits. But I couldn’t figure out how the header word is connected to the data words. Will the immediate term containing the pointer in stack refer to both header word and the first data word or will the data words be always contiguously stored after the header word. Any help would be appreciated.
Thank you.
Marked As Solved
garazdawi
The data words will always be contiguously stored after the header word. This is the way it works for all boxed terms.
Also Liked
al2o3cr
The code stores “digits” the size of a machine word, and keeps the count for those in the upper bits of the header.
Some good places to read in the OTP source:
otp/erts/emulator/beam/erl_term.h: a nice diagram of how tags are used in a term’s header. A bignum’s sign is stored in the tag.otp/erts/emulator/beam/big.h: some C macros that access different parts of a bignum:- the sign is in the lowest bit of the header
- the digits all immediately follow the header (the
+1inBIG_V) - the arity (number of digits) is stored in the header
otp/erts/emulator/beam/big.c: the implementation of most bignum functions. The link goes toI_compwhich tells us about how digits are stored- the first two cases dispose of the easy parts, when one number is more digits than the other
- the final case does the digit-by-digit comparison, starting from the digit farthest from the header as indicated by setting up the loop with
x += (xl-1)
One final thing - normally I’d point people towards the BEAM Book which has an epic amount of internal detail, but alas the section on bignum representation is marked TODO ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









