1cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; Copyright 2010 the V8 project authors. All rights reserved.
2cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; Redistribution and use in source and binary forms, with or without
3cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; modification, are permitted provided that the following conditions are
4cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; met:
5cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;
6cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;     * Redistributions of source code must retain the above copyright
7cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       notice, this list of conditions and the following disclaimer.
8cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;     * Redistributions in binary form must reproduce the above
9cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       copyright notice, this list of conditions and the following
10cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       disclaimer in the documentation and/or other materials provided
11cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       with the distribution.
12cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;     * Neither the name of Google Inc. nor the names of its
13cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       contributors may be used to endorse or promote products derived
14cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;       from this software without specific prior written permission.
15cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;
16cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
28cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; This is a Scheme script for the Bigloo compiler. Bigloo must be compiled with
29cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; support for bignums. The compilation of the script can be done as follows:
30cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;   bigloo -static-bigloo -o generate-ten-powers generate-ten-powers.scm
31cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;
32cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; Generate approximations of 10^k.
33cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
34cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(module gen-ten-powers
35cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (static (class Cached-Fast
36cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	      v::bignum
37cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	      e::bint
38cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	      exact?::bool))
39cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (main my-main))
40cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
41cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
42cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;----------------bignum shifts -----------------------------------------------
43cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (bit-lshbx::bignum x::bignum by::bint)
44cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (if ( by 0)
45cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       #z0
46cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (*bx x (exptbx #z2 (fixnum->bignum by)))))
47cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
48cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (bit-rshbx::bignum x::bignum by::bint)
49cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (if ( by 0)
50cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       #z0
51cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (/bx x (exptbx #z2 (fixnum->bignum by)))))
52cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
53cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;----------------the actual power generation -------------------------------
54cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
55cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;; e should be an indication. it might be too small.
56cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (round-n-cut n e nb-bits)
57cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (define max-container (- (bit-lshbx #z1 nb-bits) 1))
58cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (define (round n)
59cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (case *round*
60cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 ((down) n)
61cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 ((up)
62cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (+bx n
63cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       ;; with the -1 it will only round up if the cut off part is
64cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       ;; non-zero
65cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (-bx (bit-lshbx #z1
66cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			       (-fx (+fx e nb-bits) 1))
67cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		    #z1)))
68cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 ((round)
69cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (+bx n
70cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (bit-lshbx #z1
71cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			  (-fx (+fx e nb-bits) 2))))))
72cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (let* ((shift (-fx (+fx e nb-bits) 1))
73cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (cut (bit-rshbx (round n) shift))
74cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (exact? (=bx n (bit-lshbx cut shift))))
75cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (if (<=bx cut max-container)
76cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (values cut e exact?)
77cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (round-n-cut n (+fx e 1) nb-bits))))
78cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
79cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (rounded-/bx x y)
80cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (case *round*
81cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ((down)  (/bx x y))
82cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ((up)    (+bx (/bx x y) #z1))
83cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ((round) (let ((tmp (/bx (*bx #z2 x) y)))
84cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		  (if (zerobx? (remainderbx tmp #z2))
85cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		      (/bx tmp #z2)
86cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		      (+bx (/bx tmp #z2) #z1))))))
87cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
88cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (generate-powers from to mantissa-size)
89cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (let* ((nb-bits mantissa-size)
90cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (offset (- from))
91cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (nb-elements (+ (- from) to 1))
92cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (vec (make-vector nb-elements))
93cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (max-container (- (bit-lshbx #z1 nb-bits) 1)))
94cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; the negative ones. 10^-1, 10^-2, etc.
95cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; We already know, that we can't be exact, so exact? will always be #f.
96cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; Basically we will have a ten^i that we will *10 at each iteration. We
97cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; want to create the matissa of 1/ten^i. However the mantissa must be
98cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; normalized (start with a 1). -> we have to shift the number.
99cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; We shift by multiplying with two^e. -> We encode two^e*(1/ten^i) ==
100cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;;  two^e/ten^i.
101cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (let loop ((i 1)
102cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (ten^i #z10)
103cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (two^e #z1)
104cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (e 0))
105cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (unless (< (- i) from)
106cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (if (>bx (/bx (*bx #z2 two^e) ten^i) max-container)
107cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		;; another shift would make the number too big. We are
108cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		;; hence normalized now.
109cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		(begin
110cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		   (vector-set! vec (-fx offset i)
111cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				(instantiate::Cached-Fast
112cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				   (v (rounded-/bx two^e ten^i))
113cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				   (e (negfx e))
114cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				   (exact? #f)))
115cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		   (loop (+fx i 1) (*bx ten^i #z10) two^e e))
116cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		(loop i ten^i (bit-lshbx two^e 1) (+fx e 1)))))
117cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; the positive ones 10^0, 10^1, etc.
118cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; start with 1.0. mantissa: 10...0 (1 followed by nb-bits-1 bits)
119cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;;      -> e = -(nb-bits-1)
120cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ;; exact? is true when the container can still hold the complete 10^i
121cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (let loop ((i 0)
122cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (n (bit-lshbx #z1 (-fx nb-bits 1)))
123cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (e (-fx 1 nb-bits)))
124cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (when (<= i to)
125cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (receive (cut e exact?)
126cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (round-n-cut n e nb-bits)
127cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (vector-set! vec (+fx i offset)
128cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			    (instantiate::Cached-Fast
129cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			       (v cut)
130cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			       (e e)
131cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			       (exact? exact?)))
132cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (loop (+fx i 1) (*bx n #z10) e))))
133cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      vec))
134cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
135cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (print-c powers from to struct-type
136cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 cache-name max-distance-name offset-name macro64)
137cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (define (display-power power k)
138cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (with-access::Cached-Fast power (v e exact?)
139cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (let ((tmp-p (open-output-string)))
140cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    ;; really hackish way of getting the digits
141cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (display (format "~x" v) tmp-p)
142cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (let ((str (close-output-port tmp-p)))
143cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (printf "  {~a(0x~a, ~a), ~a, ~a},\n"
144cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		       macro64
145cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		       (substring str 0 8)
146cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		       (substring str 8 16)
147cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		       e
148cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		       k)))))
149cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (define (print-powers-reduced n)
150cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (print "static const " struct-type " " cache-name
151cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "(" n ")"
152cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "[] = {")
153cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (let loop ((i 0)
154cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (nb-elements 0)
155cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (last-e 0)
156cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 (max-distance 0))
157cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (cond
158cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    ((>= i (vector-length powers))
159cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (print "  };")
160cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (print "static const int " max-distance-name "(" n ") = "
161cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		 max-distance ";")
162cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (print "// nb elements (" n "): " nb-elements))
163cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (else
164cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (let* ((power (vector-ref powers i))
165cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		    (e (Cached-Fast-e power)))
166cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (display-power power (+ i from))
167cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (loop (+ i n)
168cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		   (+ nb-elements 1)
169cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		   e
170cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		   (cond
171cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		      ((=fx i 0) max-distance)
172cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		      ((> (- e last-e) max-distance) (- e last-e))
173cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		      (else max-distance))))))))
174cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print "// Copyright 2010 the V8 project authors. All rights reserved.")
175cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print "// ------------ GENERATED FILE ----------------")
176cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print "// command used:")
177cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print "// "
178cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  (apply string-append (map (lambda (str)
179cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				       (string-append " " str))
180cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org				    *main-args*))
181cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	  "  // NOLINT")
182cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print)
183cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print
184cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "// This file is intended to be included inside another .h or .cc files\n"
185cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "// with the following defines set:\n"
186cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//  GRISU_CACHE_STRUCT: should expand to the name of a struct that will\n"
187cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   hold the cached powers of ten. Each entry will hold a 64-bit\n"
188cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   significand, a 16-bit signed binary exponent, and a 16-bit\n"
189cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   signed decimal exponent. Each entry will be constructed as follows:\n"
190cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//      { significand, binary_exponent, decimal_exponent }.\n"
191cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//  GRISU_CACHE_NAME(i): generates the name for the different caches.\n"
192cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   The parameter i will be a number in the range 1-20. A cache will\n"
193cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   hold every i'th element of a full cache. GRISU_CACHE_NAME(1) will\n"
194cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   thus hold all elements. The higher i the fewer elements it has.\n"
195cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   Ideally the user should only reference one cache and let the\n"
196cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   compiler remove the unused ones.\n"
197cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//  GRISU_CACHE_MAX_DISTANCE(i): generates the name for the maximum\n"
198cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   binary exponent distance between all elements of a given cache.\n"
199cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//  GRISU_CACHE_OFFSET: is used as variable name for the decimal\n"
200cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   exponent offset. It is equal to -cache[0].decimal_exponent.\n"
201cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//  GRISU_UINT64_C: used to construct 64-bit values in a platform\n"
202cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   independent way. In order to encode 0x123456789ABCDEF0 the macro\n"
203cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org    "//   will be invoked as follows: GRISU_UINT64_C(0x12345678,9ABCDEF0).\n")
204cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print)
205cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 1)
206cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 2)
207cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 3)
208cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 4)
209cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 5)
210cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 6)
211cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 7)
212cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 8)
213cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 9)
214cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 10)
215cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 11)
216cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 12)
217cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 13)
218cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 14)
219cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 15)
220cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 16)
221cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 17)
222cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 18)
223cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 19)
224cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print-powers-reduced 20)
225cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (print "static const int GRISU_CACHE_OFFSET = " (- from) ";"))
226cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
227cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org;;----------------main --------------------------------------------------------
228cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *main-args* #f)
229cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *mantissa-size* #f)
230cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *dest* #f)
231cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *round* #f)
232cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *from* #f)
233cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define *to* #f)
234cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
235cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org(define (my-main args)
236cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (set! *main-args* args)
237cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (args-parse (cdr args)
238cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (section "Help")
239cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("?") (args-parse-usage #f))
240cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      ((("-h" "--help") (help "?, -h, --help" "This help message"))
241cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (args-parse-usage #f))
242cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (section "Misc")
243cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("-o" ?file (help "The output file"))
244cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (set! *dest* file))
245cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("--mantissa-size" ?size (help "Container-size in bits"))
246cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (set! *mantissa-size* (string->number size)))
247cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("--round" ?direction (help "Round bignums (down, round or up)"))
248cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (set! *round* (string->symbol direction)))
249cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("--from" ?from (help "start at 10^from"))
250cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (set! *from* (string->number from)))
251cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (("--to" ?to (help "go up to 10^to"))
252cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (set! *to* (string->number to)))
253cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (else
254cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (print "Illegal argument `" else "'. Usage:")
255cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org       (args-parse-usage #f)))
256cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (when (not *from*)
257cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (error "generate-ten-powers"
258cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "Missing from"
259cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     #f))
260cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (when (not *to*)
261cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (error "generate-ten-powers"
262cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "Missing to"
263cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     #f))
264cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (when (not *mantissa-size*)
265cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (error "generate-ten-powers"
266cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "Missing mantissa size"
267cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     #f))
268cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (when (not (memv *round* '(up down round)))
269cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (error "generate-ten-powers"
270cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     "Missing round-method"
271cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     *round*))
272cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org
273cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org   (let ((dividers (generate-powers *from* *to* *mantissa-size*))
274cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (p (if (not *dest*)
275cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		(current-output-port)
276cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org		(open-output-file *dest*))))
277cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org      (unwind-protect
278cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (with-output-to-port p
279cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	    (lambda ()
280cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	       (print-c dividers *from* *to*
281cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			"GRISU_CACHE_STRUCT" "GRISU_CACHE_NAME"
282cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			"GRISU_CACHE_MAX_DISTANCE" "GRISU_CACHE_OFFSET"
283cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			"GRISU_UINT64_C"
284cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org			)))
285cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	 (if *dest*
286cec079d8ed1f0920a0ea3dc9a3e81966013287c1whesse@chromium.org	     (close-output-port p)))))
287