1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package "PROTO-IMPL")
14 ;;; Optimized fixnum arithmetic
16 (eval-when (:compile-toplevel :load-toplevel :execute)
18 (defparameter $optimize-default '(optimize (speed 1) (safety 3) (debug 3))
19 "Compiler optimization settings for safe, debuggable code.")
20 (defparameter $optimize-fast-unsafe '(optimize (speed 3) (safety 0) (debug 0))
21 "Compiler optimization settings for fast, unsafe, hard-to-debug code.")
26 (defmacro i+ (&rest fixnums)
27 `(the fixnum (+ ,@(loop for n in fixnums collect `(the fixnum ,n)))))
29 (defmacro i- (number &rest fixnums)
30 `(the fixnum (- (the fixnum ,number) ,@(loop for n in fixnums collect `(the fixnum ,n)))))
32 (defmacro i* (&rest fixnums)
33 `(the fixnum (* ,@(loop for n in fixnums collect `(the fixnum ,n)))))
35 (defmacro i= (&rest fixnums)
36 `(= ,@(loop for n in fixnums collect `(the fixnum ,n))))
38 (defmacro i< (&rest fixnums)
39 `(< ,@(loop for n in fixnums collect `(the fixnum ,n))))
41 (defmacro i<= (&rest fixnums)
42 `(<= ,@(loop for n in fixnums collect `(the fixnum ,n))))
44 (defmacro i> (&rest fixnums)
45 `(> ,@(loop for n in fixnums collect `(the fixnum ,n))))
47 (defmacro i>= (&rest fixnums)
48 `(>= ,@(loop for n in fixnums collect `(the fixnum ,n))))
50 (defmacro iash (value count)
51 `(the fixnum (ash (the fixnum ,value) (the fixnum ,count))))
53 (defmacro ilogior (&rest fixnums)
55 `(the fixnum (logior (the fixnum ,(car fixnums))
57 `(ilogior ,@(cdr fixnums))
58 `(the fixnum ,(cadr fixnums)))))
59 `(the fixnum ,(car fixnums))))
61 (defmacro ilogand (&rest fixnums)
63 `(the fixnum (logand (the fixnum ,(car fixnums))
65 `(ilogand ,@(cdr fixnums))
66 `(the fixnum ,(cadr fixnums)))))
67 `(the fixnum ,(car fixnums))))
69 (define-modify-macro iincf (&optional (delta 1)) i+)
70 (define-modify-macro idecf (&optional (delta 1)) i-)
72 (defmacro ildb (bytespec value)
73 `(the fixnum (ldb ,bytespec (the fixnum ,value))))
78 (defun starts-with (string prefix &key (start 0))
79 "Returns true if 'string' starts with the prefix 'prefix' (case insensitive)."
80 (and (i>= (length string) (i+ start (length prefix)))
81 (string-equal string prefix :start1 start :end1 (i+ start (length prefix)))
84 (defun ends-with (string suffix &key (end (length string)))
85 "Returns true if 'string' ends with the prefix 'prefix' (case insensitive)."
86 (and (i>= end (length suffix))
87 (string-equal string suffix :start1 (i- end (length suffix)) :end1 end)
90 (defun strcat (&rest strings)
91 "Concatenate a bunch of strings."
92 (declare (dynamic-extent strings))
93 (apply #'concatenate 'string strings))
96 ;; (camel-case "camel-case") => "CamelCase"
97 (defun camel-case (string &optional (separators '(#\-)))
98 "Take a hyphen-separated string and turn it into a camel-case string."
99 (let ((words (split-string string :separators separators)))
100 (format nil "~{~@(~A~)~}" words)))
102 ;; (camel-case-but-one "camel-case") => "camelCase"
103 (defun camel-case-but-one (string &optional (separators '(#\-)))
104 "Take a hyphen-separated string and turn its tail into a camel-case string."
105 (let ((words (split-string string :separators separators)))
106 (format nil "~(~A~)~{~@(~A~)~}" (car words) (cdr words))))
109 ;; (uncamel-case "CamelCase") => "CAMEL-CASE"
110 ;; (uncamel-case "TCPConnection") => "TCP-CONNECTION"
111 ;; (uncamel-case "NewTCPConnection") => "NEW-TCP-CONNECTION"
112 ;; (uncamel-case "new_RPC_LispService") => "NEW-RPC-LISP-SERVICE"
113 ;; (uncamel-case "RPC_LispServiceRequest_get_request") => "RPC-LISP-SERVICE-REQUEST-GET-REQUEST"
114 ;; (uncamel-case "TCP2Name3") => "TCP2-NAME3"
115 (defun uncamel-case (name)
116 "Take a camel-case string and turn it into a hyphen-separated string."
117 ;; We need a whole state machine to get this right
118 (labels ((uncamel (chars state result)
119 (let ((ch (first chars)))
123 (uncamel (rest chars) 'upper
126 ;; "TCPConnection" => "TCP-CONNECTION"
127 (if (and (second chars) (lower-case-p (second chars)))
128 (list* ch #\- result)
130 ((lower digit) (list* ch #\- result))
131 (otherwise (cons ch result)))))
133 (uncamel (rest chars) 'lower
134 (cons (char-upcase ch) result)))
136 (uncamel (rest chars) 'digit
138 ((or (eql ch #\-) (eql ch #\_))
139 (uncamel (rest chars) 'dash
142 (uncamel (rest chars) 'dot
145 (error "Invalid name character: ~A" ch))))))
146 (strcat (nreverse (uncamel (concatenate 'list name) nil ())))))
149 (defun split-string (line &key (start 0) (end (length line)) (separators '(#\-)))
150 "Given a string 'string', splits it at each of the separators.
151 Returns a list of the string pieces, with empty pieces removed."
152 (unless (i= start end)
153 (loop for this fixnum = start then (i+ next 1)
154 for next fixnum = (or (position-if #'(lambda (ch) (member ch separators)) line
155 :start this :end end)
157 for piece = (string-right-trim '(#\space) (subseq line this next))
158 when (not (i= (length piece) 0))
160 until (i>= next end))))
165 (defmacro with-gensyms ((&rest bindings) &body body)
166 `(let ,(mapcar #'(lambda (b) `(,b (gensym ,(string b)))) bindings)
169 (defun make-lisp-symbol (string)
170 "Intern a string of the 'package:string' and return the symbol."
171 (let* ((string (string string))
172 (colon (position #\: string))
173 (pkg (if colon (subseq string 0 colon) "KEYWORD"))
174 (sym (if colon (subseq string (+ colon 1)) string)))
177 (defun fintern (format-string &rest format-args)
178 "Interns a new symbol in the current package."
179 (declare (dynamic-extent format-args))
180 (intern (nstring-upcase (apply #'format nil format-string format-args))))
182 (defun kintern (format-string &rest format-args)
183 "Interns a new symbol in the keyword package."
184 (declare (dynamic-extent format-args))
185 (intern (nstring-upcase (apply #'format nil format-string format-args)) "KEYWORD"))
187 (defun keywordify (x)
188 "Given a symbol designator 'x', return a keyword whose name is 'x'.
189 If 'x' is nil, this returns nil."
190 (check-type x (or string symbol null))
193 ((symbolp x) (keywordify (symbol-name x)))
194 ((zerop (length x)) nil)
195 ((string-not-equal x "nil")
196 (intern (string-upcase x) (find-package "KEYWORD")))
202 (defmacro with-collectors ((&rest collection-descriptions) &body body)
203 "'collection-descriptions' is a list of clauses of the form (coll function).
204 The body can call each 'function' to add a value to 'coll'. 'function'
205 runs in constant time, regardless of the length of the list."
206 (let ((let-bindings ())
210 (dolist (description collection-descriptions)
211 (destructuring-bind (place name) description
212 (let ((vtail (make-symbol (format nil "~A-TAIL" place))))
213 (setq dynamic-extents
214 (nconc dynamic-extents `(#',name)))
222 (setq ,vtail (if ,vtail
223 (setf (cdr ,vtail) (list ,vobj))
224 (setf ,place (list ,vobj)))))))))))
225 `(let (,@let-bindings)
226 (flet (,@flet-bindings)
227 ,@(and dynamic-extents
228 `((declare (dynamic-extent ,@dynamic-extents))))
231 (defmacro with-prefixed-accessors (names (prefix object) &body body)
232 `(with-accessors (,@(loop for name in names
233 collect `(,name ,(fintern "~A~A" prefix name))))
237 (defmacro dovector ((var vector &optional value) &body body)
238 "Like 'dolist', but iterates over the vector 'vector'."
239 (with-gensyms (vidx vlen vvec)
240 `(let* ((,vvec ,vector)
241 (,vlen (length ,vvec)))
242 (loop for ,vidx fixnum from 0 below ,vlen
243 as ,var = (aref ,vvec ,vidx)
245 finally (return ,value)))))
247 (defmacro doseq ((var sequence &optional value) &body body)
248 "Iterates over a sequence, using 'dolist' or 'dovector' depending on
249 the type of the sequence. In optimized code, this turns out to be
250 faster than (map () #'f sequence).
251 Note that the body gets expanded twice!"
253 `(let ((,vseq ,sequence))
255 (dovector (,var ,vseq ,value)
257 (dolist (,var ,vseq ,value)
261 ;;; Functional programming, please
263 (defun curry (function &rest args)
264 "Returns a function that applies 'function' to 'args', plus any
265 additional arguments given at the call site."
266 (if (and args (null (cdr args))) ;fast test for length = 1
267 (let ((arg (car args)))
268 #'(lambda (&rest more-args)
269 (apply function arg more-args)))
270 #'(lambda (&rest more-args)
271 (apply function (append args more-args)))))
273 (define-compiler-macro curry (&whole form function &rest args &environment env)
274 (declare (ignore env))
275 (if (and (listp function)
276 (eq (first function) 'function)
277 (symbolp (second function))
278 (and args (null (cdr args))))
279 `#'(lambda (&rest more-args)
280 (apply ,function ,(car args) more-args))
286 ;; A parameterized list type for repeated fields
287 ;; The elements aren't type-checked
288 (deftype list-of (type)
289 (if (eq type 'nil) ; a list that cannot have any element (element-type nil) is null.
293 ;; The same, but use a (stretchy) vector
294 (deftype vector-of (type)
295 (if (eq type 'nil); an array that cannot have any element (element-type nil) is of size 0.
297 '(array * (*)))) ;an 1-dimensional array of any type
299 ;; This corresponds to the :bytes Protobufs type
300 (deftype byte-vector () '(array (unsigned-byte 8) (*)))
302 (defun make-byte-vector (size)
303 (make-array size :element-type '(unsigned-byte 8)))
305 ;; The Protobufs integer types
306 (deftype int32 () '(signed-byte 32))
307 (deftype int64 () '(signed-byte 64))
308 (deftype uint32 () '(unsigned-byte 32))
309 (deftype uint64 () '(unsigned-byte 64))
310 (deftype sint32 () '(signed-byte 32))
311 (deftype sint64 () '(signed-byte 64))
312 (deftype fixed32 () '(signed-byte 32))
313 (deftype fixed64 () '(signed-byte 64))
314 (deftype sfixed32 () '(signed-byte 32))
315 (deftype sfixed64 () '(signed-byte 64))
318 ;;; Code generation utilities
320 (defvar *proto-name-separators* '(#\- #\_ #\/ #\space))
321 (defvar *camel-case-field-names* nil)
323 (defun find-proto-package (name)
324 "A very fuzzy definition of 'find-package'."
327 ;; Try looking under the given name and the all-uppercase name
328 (or (find-package (string name))
329 (find-package (string-upcase (string name)))))
331 ;; If 'name' is a list, it's actually a fully-qualified path
332 (or (find-proto-package (first name))
333 (find-proto-package (format nil "~{~A~^.~}" name))))))
335 ;; "class-name" -> "ClassName", ("ClassName")
336 ;; "outer-class.inner-class" -> "InnerClass", ("OuterClass" "InnerClass")
337 (defun class-name->proto (x)
338 "Given a Lisp class name, returns a Protobufs message or enum name.
339 The second value is the fully qualified name, as a list."
340 (let* ((xs (split-string (string x) :separators '(#\.)))
341 (ns (loop for x in (butlast xs)
342 collect (remove-if-not #'alphanumericp
343 (camel-case (format nil "~A" x) *proto-name-separators*))))
345 (name (remove-if-not #'alphanumericp (camel-case nx *proto-name-separators*))))
346 (values name (append ns (list name))
347 ;; This might be the name of a package, too
348 (format nil "~{~A~^.~}" (butlast xs)))))
350 ;; "enum-value" -> "ENUM_VALUE", ("ENUM_VALUE")
351 ;; "class-name.enum-value" -> "ENUM_VALUE", ("ClassName" "ENUM_VALUE")
352 (defun enum-name->proto (x &optional prefix)
353 "Given a Lisp enum value name, returns a Protobufs enum value name.
354 The second value is the fully qualified name, as a list."
355 (let* ((xs (split-string (string x) :separators '(#\.)))
356 (ns (loop for x in (butlast xs)
357 collect (remove-if-not #'alphanumericp
358 (camel-case (format nil "~A" x) *proto-name-separators*))))
359 (nx (string-upcase (car (last xs))))
360 (nx (if (and prefix (starts-with nx prefix)) (subseq nx (length prefix)) nx))
361 ;; Keep underscores, they are standards separators in Protobufs enum names
362 (name (remove-if-not #'(lambda (x) (or (alphanumericp x) (eql x #\_)))
363 (format nil "~{~A~^_~}"
364 (split-string nx :separators *proto-name-separators*)))))
365 (values name (append ns (list name))
366 (format nil "~{~A~^.~}" (butlast xs)))))
368 ;; "slot-name" -> "slot_name", ("slot_name") or "slotName", ("slotName")
369 ;; "class-name.slot-name" -> "Class.slot_name", ("ClassName" "slot_name")
370 (defun slot-name->proto (x)
371 "Given a Lisp slot name, returns a Protobufs field name.
372 The second value is the fully qualified name, as a list."
373 (let* ((xs (split-string (string x) :separators '(#\.)))
374 (ns (loop for x in (butlast xs)
375 collect (remove-if-not #'alphanumericp
376 (camel-case (format nil "~A" x) *proto-name-separators*))))
377 (nx (string-downcase (car (last xs))))
378 (name (if *camel-case-field-names*
379 (remove-if-not #'alphanumericp
380 (camel-case-but-one (format nil "~A" nx) *proto-name-separators*))
381 ;; Keep underscores, they are standards separators in Protobufs field names
382 (remove-if-not #'(lambda (x) (or (alphanumericp x) (eql x #\_)))
383 (format nil "~{~A~^_~}"
384 (split-string nx :separators *proto-name-separators*))))))
385 (values name (append ns (list name))
386 (format nil "~{~A~^.~}" (butlast xs)))))
389 ;; "ClassName" -> 'class-name
390 ;; "cl-user.ClassName" -> 'cl-user::class-name
391 ;; "cl-user.OuterClass.InnerClass" -> 'cl-user::outer-class.inner-class
392 (defun proto->class-name (x &optional package)
393 "Given a Protobufs message or enum type name, returns a Lisp class or type name.
394 This resolves Protobufs qualified names as best as it can."
395 (let* ((xs (split-string (substitute #\- #\_ (uncamel-case x))
397 (pkg1 (and (cdr xs) (find-proto-package (first xs))))
398 (pkgn (and (cdr xs) (find-proto-package (butlast xs))))
399 (package (or pkg1 pkgn package))
400 (name (format nil "~{~A~^.~}" (if pkg1 (cdr xs) (if pkgn (last xs) xs)))))
401 (values (if package (intern name package) (make-symbol name)) package xs
402 ;; This might be the name of a package, too
403 (format nil "~{~A~^.~}" (butlast xs)))))
405 ;; "ENUM_VALUE" -> :enum-value
406 ;; "cl-user.ENUM_VALUE" -> :enum-value
407 ;; "cl-user.OuterClass.ENUM_VALUE" -> :enum-value
408 (defun proto->enum-name (x &optional package)
409 "Given a Protobufs enum value name, returns a Lisp enum value name.
410 This resolves Protobufs qualified names as best as it can."
411 (let* ((xs (split-string (substitute #\- #\_ (uncamel-case x))
413 (pkg1 (and (cdr xs) (find-proto-package (first xs))))
414 (pkgn (and (cdr xs) (find-proto-package (butlast xs))))
415 (package (or pkg1 pkgn package))
416 (name (format nil "~{~A~^.~}" (if pkg1 (cdr xs) (if pkgn (last xs) xs)))))
417 (values (kintern name) package xs
418 (format nil "~{~A~^.~}" (butlast xs)))))
420 ;; "slot_name" or "slotName" -> 'slot-name
421 ;; "cl-user.slot_name" or "cl-user.slotName" -> 'cl-user::slot-name
422 ;; "cl-user.OuterClass.slot_name" -> 'cl-user::outer-class.slot-name
423 (defun proto->slot-name (x &optional package)
424 "Given a Protobufs field value name, returns a Lisp slot name.
425 This resolves Protobufs qualified names as best as it can."
426 (let* ((xs (split-string (substitute #\- #\_ (uncamel-case x))
428 (pkg1 (and (cdr xs) (find-proto-package (first xs))))
429 (pkgn (and (cdr xs) (find-proto-package (butlast xs))))
430 (package (or pkg1 pkgn package))
431 (name (format nil "~{~A~^.~}" (if pkg1 (cdr xs) (if pkgn (last xs) xs)))))
432 (values (if package (intern name package) (make-symbol name)) package xs
433 (format nil "~{~A~^.~}" (butlast xs)))))
438 (define-condition protobufs-warning (warning simple-condition) ())
440 (defun protobufs-warn (format-control &rest format-arguments)
441 (warn 'protobufs-warning
442 :format-control format-control
443 :format-arguments format-arguments))
446 #-(or allegro lispworks)
447 (defmacro without-redefinition-warnings (() &body body)
451 (defmacro without-redefinition-warnings (() &body body)
452 `(excl:without-redefinition-warnings ,@body))
455 (defmacro without-redefinition-warnings (() &body body)
456 `(let ((dspec:*redefinition-action* :quiet)) ,@body))
459 ;;; Portable floating point utilities
461 #+(or abcl allegro ccl cmu sbcl lispworks)
462 (defun single-float-bits (x)
463 (declare (type single-float x))
464 #+abcl (system:single-float-bits x)
465 #+allegro (multiple-value-bind (high low)
466 (excl:single-float-to-shorts x)
467 (declare (type (unsigned-byte 16) high low))
468 (logior (ash high 16) low))
469 #+ccl (ccl::single-float-bits x)
470 #+cmu (kernel:single-float-bits x)
471 #+sbcl (sb-kernel:single-float-bits x)
472 #+lispworks (lispworks-float:single-float-bits x))
474 #-(or abcl allegro ccl cmu sbcl lispworks)
475 (defun single-float-bits (x)
476 (declare (type single-float x))
477 (assert (= (float-radix x) 2))
479 (if (eql x 0.0f0) 0 #x-80000000)
480 (multiple-value-bind (lisp-significand lisp-exponent lisp-sign)
481 (integer-decode-float x)
482 (assert (plusp lisp-significand))
483 (let* ((significand lisp-significand)
484 (exponent (+ lisp-exponent 23 127))
486 (if (plusp exponent) ;if not obviously denormalized
489 ;; Special termination case for denormalized float number
491 ;; Denormalized numbers have exponent one greater than
492 ;; in the exponent field
493 (return (ash significand -1)))
494 ;; Ordinary termination case
495 ((>= significand (expt 2 23))
496 (assert (< 0 significand (expt 2 24)))
497 ;; Exponent 0 is reserved for denormalized numbers,
498 ;; and 255 is reserved for specials like NaN
499 (assert (< 0 exponent 255))
500 (return (logior (ash exponent 23)
501 (logand significand (1- (ash 1 23))))))
503 ;; Shift as necessary to set bit 24 of significand
504 (setq significand (ash significand 1)
505 exponent (1- exponent)))))
506 (do () ((zerop exponent)
507 ;; Denormalized numbers have exponent one greater than
508 ;; the exponent field
509 (ash significand -1))
510 (unless (zerop (logand significand 1))
511 (warn "Denormalized '~S' losing bits in ~D" 'single-float-bits x))
512 (setq significand (ash significand -1)
513 exponent (1+ exponent))))))
515 ((1) unsigned-result)
516 ((-1) (logior unsigned-result (- (expt 2 31)))))))))
519 #+(or abcl allegro ccl cmu sbcl lispworks)
520 (defun double-float-bits (x)
521 (declare (type double-float x))
522 #+abcl (values (system:double-float-low-bits x)
523 (system:double-float-high-bits x))
524 #+allegro (multiple-value-bind (us3 us2 us1 us0)
525 (excl:double-float-to-shorts x)
526 (logior (ash us1 16) us0)
527 (logior (ash us3 16) us2))
528 #+ccl (multiple-value-bind (high low)
529 (ccl::double-float-bits x)
531 #+cmu (values (kernel:double-float-low-bits x)
532 (kernel:double-float-high-bits x))
533 #+sbcl (values (sb-kernel:double-float-low-bits x)
534 (sb-kernel:double-float-high-bits x))
535 #+lispworks (let ((bits (lispworks-float:double-float-bits x)))
536 (values (logand #xffffffff bits)
539 #-(or abcl allegro ccl cmu sbcl lispworks)
540 (defun double-float-bits (x)
541 (declare (type double-float x))
542 (assert (= (float-radix x) 2))
544 (if (eql x 0.0d0) 0 #x-8000000000000000)
545 (multiple-value-bind (lisp-significand lisp-exponent lisp-sign)
546 (integer-decode-float x)
547 (assert (plusp lisp-significand))
548 (let* ((significand lisp-significand)
549 (exponent (+ lisp-exponent 52 1023))
551 (if (plusp exponent) ;if not obviously denormalized
554 ;; Special termination case for denormalized float number
556 ;; Denormalized numbers have exponent one greater than
557 ;; in the exponent field
558 (return (ash significand -1)))
559 ;; Ordinary termination case
560 ((>= significand (expt 2 52))
561 (assert (< 0 significand (expt 2 53)))
562 ;; Exponent 0 is reserved for denormalized numbers,
563 ;; and 2047 is reserved for specials like NaN
564 (assert (< 0 exponent 2047))
565 (return (logior (ash exponent 52)
566 (logand significand (1- (ash 1 52))))))
568 ;; Shift as necessary to set bit 53 of significand
569 (setq significand (ash significand 1)
570 exponent (1- exponent)))))
571 (do () ((zerop exponent)
572 ;; Denormalized numbers have exponent one greater than
573 ;; the exponent field
574 (ash significand -1))
575 (unless (zerop (logand significand 1))
576 (warn "Denormalized '~S' losing bits in ~D" 'double-float-bits x))
577 (setq significand (ash significand -1)
578 exponent (1+ exponent))))))
581 ((1) unsigned-result)
582 ((-1) (logior unsigned-result (- (expt 2 63)))))))
583 ;; Return the low bits and the high bits
584 (values (logand #xffffffff result) (ash result -32)))))))
587 #+(or abcl allegro ccl cmu sbcl lispworks)
588 (defun make-single-float (bits)
589 (declare (type (signed-byte 32) bits))
590 #+abcl (system:make-single-float bits)
591 #+allegro (excl:shorts-to-single-float (ldb (byte 16 16) bits)
592 (ldb (byte 16 0) bits))
593 #+ccl (ccl::host-single-float-from-unsigned-byte-32 bits)
594 #+cmu (kernel:make-single-float bits)
595 #+sbcl (sb-kernel:make-single-float bits)
596 #+lispworks (lispworks-float:make-single-float bits))
598 #-(or abcl allegro ccl cmu sbcl lispworks)
599 (defun make-single-float (bits)
600 (declare (type (signed-byte 32) bits))
602 ;; IEEE float special cases
604 ((= bits #x-80000000) -0.0)
606 (let* ((sign (ecase (ldb (byte 1 31) bits)
609 (iexpt (ldb (byte 8 23) bits))
610 (exponent (if (zerop iexpt) ;denormalized
613 (mantissa (* (logior (ldb (byte 23 0) bits)
614 (if (zerop iexpt) 0 (ash 1 23)))
616 (* sign (expt 2.0 exponent) mantissa)))))
619 #+(or abcl allegro ccl cmu sbcl lispworks)
620 (defun make-double-float (low high)
621 (declare (type (unsigned-byte 32) low)
622 (type (signed-byte 32) high))
623 #+abcl (system:make-double-float (logior (ash high 32) low))
624 #+allegro (excl:shorts-to-double-float (ldb (byte 16 16) high)
625 (ldb (byte 16 0) high)
626 (ldb (byte 16 16) low)
627 (ldb (byte 16 0) low))
628 #+ccl (ccl::double-float-from-bits high low)
629 #+cmu (kernel:make-double-float high low)
630 #+sbcl (sb-kernel:make-double-float high low)
631 #+lispworks (lispworks-float:make-double-float high low))
633 #-(or abcl allegro ccl cmu sbcl lispworks)
634 (defun make-double-float (low high)
635 (declare (type (unsigned-byte 32) low)
636 (type (signed-byte 32) high))
638 ;; IEEE float special cases
639 ((and (zerop high) (zerop low)) 0.0d0)
640 ((and (= high #x-80000000)
643 (let* ((bits (logior (ash high 32) low))
644 (sign (ecase (ldb (byte 1 63) bits)
647 (iexpt (ldb (byte 11 52) bits))
648 (exponent (if (zerop iexpt) ;denormalized
651 (mantissa (* (logior (ldb (byte 52 0) bits)
652 (if (zerop iexpt) 0 (ash 1 52)))
654 (* sign (expt 2.0d0 exponent) mantissa)))))