-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(in-package #:quaviver.math) | ||
|
||
(declaim (ftype (function ((integer 2 36) unsigned-byte) (unsigned-byte 22)) | ||
count-digits) | ||
(inline count-digits)) | ||
|
||
(defvar *count-digits-table* | ||
(compute-count-digits +min-base+ +max-base+ | ||
(quaviver:arithmetic-size #+quaviver/long-float | ||
'long-float | ||
#-quaviver/long-float | ||
'double-float))) | ||
|
||
(defun count-digits (base value) | ||
(declare (optimize speed)) | ||
(cond ((zerop value) | ||
1) | ||
((eql base 2) | ||
(integer-length value)) | ||
((= (logcount base) 1) | ||
(values (ceiling (integer-length value) | ||
(1- (integer-length base))))) | ||
(t | ||
(let* ((value-length (integer-length value)) | ||
(lower (ceiling-log-expt base 2 (1- value-length))) | ||
(upper (ceiling-log-expt base 2 value-length))) | ||
(if (or (= lower upper) | ||
(< value | ||
(let ((table (svref *count-digits-table* (- base +min-base+)))) | ||
(if (< lower (length table)) | ||
(svref table lower) | ||
(cl:expt base lower))))) | ||
lower | ||
upper))))) | ||
|
||
(define-compiler-macro count-digits (&whole whole base value) | ||
(cond ((not (constantp base)) | ||
whole) | ||
((eql base 2) | ||
`(integer-length ,value)) | ||
((= (logcount base) 1) | ||
`(values (ceiling (integer-length ,value) | ||
,(1- (integer-length base))))) | ||
(t | ||
`(let ((value ,value)) | ||
(declare (optimize speed)) | ||
(if (zerop value) | ||
1 | ||
(let* ((value-length (integer-length value)) | ||
(lower (ceiling-log-expt ,base 2 (1- value-length))) | ||
(upper (ceiling-log-expt ,base 2 value-length))) | ||
(if (or (= lower upper) | ||
(< value | ||
(let ((table ,(svref *count-digits-table* (- base +min-base+)))) | ||
(if (< lower (length table)) | ||
(svref table lower) | ||
(cl:expt ,base lower))))) | ||
lower | ||
upper))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters