Re: iPhoto returns a real number as a photo id
Re: iPhoto returns a real number as a photo id
- Subject: Re: iPhoto returns a real number as a photo id
- From: Christopher Nebel <email@hidden>
- Date: Tue, 15 Jun 2010 10:03:09 -0700
On Jun 15, 2010, at 4:41 AM, Shane Stanley wrote:
> On 15/6/10 9:19 PM, "Tommy Bollman" <email@hidden> wrote:
>
>> The 62 bit integers resides in 64 bit AS
>
> As far as I can see, an AS integer is limited to 2^29-1 in both 32-bit and
> 64-bit mode. Is there some evidence that it gets stored in 64 bits in 64-bit
> mode, or this 62-bit integer some new class?
Why is it that the really long threads are usually from people speculating about implementation details of AppleScript?
Short version: there's only one "integer" class in AppleScript no matter what architecture you're running, and it has a range of -2^29...2^29-1, as documented in the AppleScript Language Guide. Numbers outside that range are stored as "real" objects, which are stored in IEEE 754 double-precision format.
The details of "integer" storage, which as someone observed shouldn't be considered except as a curiosity, are not all that complicated, and are certainly not private since the technique is quite old, having been originally developed for Lisp machines in the 60s. (I'm sure I've explained this at least once before, too.)
AppleScript uses "tagged pointers" (see <http://en.wikipedia.org/wiki/Tagged_pointer>) to store everything. Essentially, everything is considered to be an object which is referred to by a pointer. However, because of architecture restrictions (imposed either by the machine or your own implementation), any actual pointer will always have some zero bits either at the high or the low end. (In AppleScript's case, it's the lower three bits, or lower four in 64-bit.) You can therefore re-purpose those bits to serve as a "tag" that tells you what kind of thing is being pointed to, and one of the possibilities is that the pointed-to object is so simple that it can be stored as immediate data in the pointer itself -- such as an integer.
At this point, a 32-bit pointer minus three tag bits would give you 29 bits for your integer, but AppleScript does an additional little trick here, which is to use *two* tags for integers -- one for even ones, one for odd. This gives you effectively a 30-bit integer, which is handled as twos-complement same as pretty much every computer architecture these days uses, with a range of -2^29...2^29-1. In 64-bit, we could have increased the size of immediate integers to 62 bits (64 bits minus two for the tags), but didn't for backward compatibility reasons -- we kept finding scripts that relied on the exact range -- so the range is still the same.
As for iPhoto, yes, it's returning a large integer as the id, which AppleScript represents as a real since it won't fit into the "integer" range. They should stop doing that, since it (a) looks weird and (b) is not entirely reliable in 64-bit if they're just re-using the pointer value as I suspect they are.
--Chris Nebel
AppleScript Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden