Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are fixnums in Emacs only 29 bits?

Tags:

emacs

lisp

elisp

And why don't they change it?

Edit: The reason ask is because I'm new to emacs and I would like to use Emacs as a "programmer calculator". So, I can manipulate 32-bit & 64-bit integers and have them behave as they would on the native machine.

like image 468
hyperlogic Avatar asked Sep 20 '08 00:09

hyperlogic


2 Answers

Emacs-Lisp is a dynamically-typed language. This means that you need type tags at runtime. If you wanted to work with numbers, you would therefore normally have to pack them into some kind of tagged container that you can point to (i.e. “box” them), as there is no way of distinguishing a pointer from a machine integer at runtime without some kind of tagging scheme.

For efficiency reasons, most Lisp implementations do therefore not use raw pointers but what I think is called descriptors. These descriptors are usually a single machine word that may represent a pointer, an unboxed number (a so-called fixnum), or one of various other hard-coded data structures (it's often worth encoding NIL and cons cells specially, too, for example).

Now, obviously, if you add the type tag, you don't have the full 32 bits left for the number, so you're left with 26 bits as in MIT Scheme or 29 bits as in Emacs or any other number of bits that you didn't use up for tagging.

Some implementations of various dynamic languages reserve multiple tags for fixnums so that they can give you 30-bit or even 31-bit fixnums. SBCL is one implementation of Common Lisp that does this. I don't think the complication that this causes is worth it for Emacs, though. How often do you need fast 30-bit fixnum arithmetic as opposed to 29-bit fixnum arithmetic in a text editor that doesn't even compile its Lisp code into machine code (or does it? I don't remember, actually)? Are you writing a distributed.net client in Emacs-Lisp? Better switch to Common Lisp, then! ;)

like image 85
Matthias Benkard Avatar answered Oct 04 '22 01:10

Matthias Benkard


The remaining 3 bits are used as flags by the Lisp interpreter. (You can get bigger integers by compiling Emacs for a 64-bit machine.)

like image 37
cjm Avatar answered Oct 04 '22 00:10

cjm