Subject: Re: EQ and FIXNUMS
From: Erik Naggum <erik@naggum.net>
Date: Mon, 24 Sep 2001 10:51:37 GMT
Newsgroups: comp.lang.lisp
Message-ID: <3210317497232745@naggum.net>

* "Vladimir V. Zolotych" <gsmith@eurocom.od.ua>
> I've accustom myself to think of EQ as doing pointer comparison.  Once
> interned a particular symbol remains so symbols that print the same
> should be EQed. What take place with numbers and chars in implementation
> where EQ works for them ? Does it mean that all objects representing
> fixnums, for example, have "standard" places in Lisp ?

  You can think of eq as a one-instruction machine-word comparison.  For
  this explanation to be adequate, you need to know how Lisp represents
  pointers to its objects _and_ fixnums and characters in the same machine
  word.  Let me take Allegro CL as an example

(inspect #\a)
character #\a [#x0000030e] char-code #x0061

(inspect #x0061)
fixnum 97 [#x00000184]

  The binary representation reveals the internals

(write #x30e :base 2)
1100001110

(write #x184 :base 2)
110000100

(write #x61 :base 2)
1100001

  We see that a character has three extra bits (110) and fixnums two (00),
  and these bits work as type bits.  (Actually, fixnums also have three,
  but the least significant bit doubles as a type bit, so both 100 and 000
  identify a fixnum.)  We notice that on 32-bit hardware, no pointers can
  have non-zero values in the least two significant bits, so they are free
  for other uses, and the hardware support for adding small integer value
  sto pointers at deferencing time, coupled with good hardware support for
  unaligned access traps, we get type-checking for free, too.  Thus we see
  that eq can use machine-word comparison instructions directly.

  If you wish, you can consider the machine-word representation of fixnums
  and characters as fixed, virtual addresses of constant objects.

  eql, on the other hand, will have to check for two bignums and compare
  them numerically.

///
-- 
  Why did that stupid George W. Bush turn to Christian fundamentalism to
  fight Islamic fundamentalism?  Why use terms like "crusade", which only
  invokes fear of a repetition of that disgraceful period of Christianity
  with its _sustained_ terrorist attacks on Islam?  He is _such_ an idiot.