Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/generate-ten-powers.scm

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

Powered by Google App Engine
This is Rietveld 408576698