3.3 Using type-specific operations
3.3.5 Compiling fast floating-point operations You can increase the speed of floating-point operations by compiling code so that it explicitly uses the floating-point hardware on your system. Calls to hardware-supported floating-point operations are compiled in line and are thus faster because the function-calling overhead is eliminated. In addition, local variables that are declared as floating-point types are allocated either in registers or in a special block on the stack rather than in dynamic memory. Thus, these variables never need to be reclaimed by garbage collection.
To get the fastest results from the floating-point hardware, you must do the following:
- Declare the explicit types of all floating-point variables.
- To declare floating-point variables, use
type declarations or the special form
the to declare the explicit types of all floating-point quantities. This allows the Compiler to allocate variables in registers or on the stack.
- When compiled, the following example produces extremely efficient code that requires no dynamic storage allocation:
(defun 2d-matrix-add (a b c)
(declare (type (simple-array float (* *)) a b c))
(let ((m (array-dimension a 1))
(n (array-dimension a 2)))
(declare (fixnum m n))
(dotimes (i m)
(dotimes (j n)
(setf (aref c i j) (+ (aref a i j) (aref b i j)))))))
- Use functions coded in line for the computationally intensive portions of your code whenever possible. This avoids the allocation of arguments and return values in dynamic storage and allows floating-point local variables to remain in registers.
- Represent aggregates of floating-point data as simple floating-point arrays whenever possible. Compiled code that moves data between simple floating-point arrays and floating-point local variables does not require dynamic allocation.
- For example, the following code defines a data structure
(x-position 0.0 :type float)
(y-position 0.0 :type float))
- The following definitions provide the same functionality as the previous definition and increase the efficiency of
ship by using floating-point arrays, which allow the Compiler to generate more efficient access code:
:initial-contents '(0.0 0.0))
:type (simple-array float 2)))
(defsubst ship-x (s) (aref (ship-position s) 0))
(defsubst ship-y (s) (aref (ship-position s) 1))
The Advanced User's Guide - 9 SEP 1996
Generated with Harlequin WebMaker