Subject: Re: low-level hacking [was Re: Calling All Noobs... ]
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 11 Jan 2006 06:04:28 -0600
Newsgroups: comp.lang.lisp
Message-ID: <RZydnUG_dO3RaVnenZ2dnUVZ_tednZ2d@speakeasy.net>
John Thingstad <john.thingstad@chello.no> wrote:
+---------------
| Rob Warnock <rpw3@rpw3.org> wrote:
| > (defun w32 (addr &rest values)
| >   (declare (optimize (speed 3) (debug 0) (safety 0)))
| >   (loop for i fixnum from 0 by 4
| > 	    and v of-type (unsigned-byte 32) in values
| >     do (setf (system:sap-ref-32 (system:int-sap addr) i) v))
| >     (values))
| 
| Speaking of which the type declarations in the loop variable
| really interested me. If fills a whole which declare can't cover.
| But it dosn't seem to work in LspWorks. Is this a SBCL spesific addition?
+---------------

Nope, it's right in the CLHS:

    http://www.lispworks.com/documentation/HyperSpec/Body/06_ai.htm
    6.1.9 Notes about Loop

    Types can be supplied for loop variables. It is not necessary to
    supply a type for any variable, but supplying the type can ensure
    that the variable has a correctly typed initial value, and it can
    also enable compiler optimizations (depending on the implementation). 

I'm using CMUCL, but I suspect SBCL is similar. And CLISP has no
problem with it.

Various sub-forms can take a TYPE-SPEC, though the "for-as-arithmetic"
sub-form is probably the most common usage:

    http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm#loop
    ...
    with-clause::= with var1 [type-spec] [= form1]
		     {and var2 [type-spec] [= form2]}* 
    ...
    numeric-accumulation::= {count | counting | sum | summing | } 
			     maximize | maximizing | minimize | minimizing
			     {form | it} [into simple-var] [type-spec] 
    ...
    for-as-arithmetic::= var [type-spec] for-as-arithmetic-subclause
    ...
    for-as-in-list::= var [type-spec] in form1 [by step-fun] 

    for-as-on-list::= var [type-spec] on form1 [by step-fun] 

    for-as-equals-then::= var [type-spec] = form1 [then form2] 

    for-as-across::= var [type-spec] across vector 

UNSIGNED-BYTE is also standard:

    http://www.lispworks.com/documentation/HyperSpec/Body/t_unsgn_.htm

What Lispworks *might* not have is (UNSIGNED-BYTE 32) as a type
you can use in LOOP. Or it might require the optional OF-TYPE in
the FIXNUM declaration: Try replacing the "FOR I FIXNUM ..." with
"FOR I OF-TYPE FIXNUM ...". If that doesn't fix it, I'd file a bug.


-Rob

p.s.: The SYSTEM:SAP-REF-32 & SYSTEM:INT-SAP functions above
*are* unique to CMUCL, but that wasn't what you asked about.

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607