Raymond Toy pushed to branch rtoy-ryu-printf at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • src/code/print.lisp
    ... ... @@ -2236,6 +2236,39 @@ radix-R. If you have a power-list then pass it in as PL."
    2236 2236
            ;; Finish with ".0"
    
    2237 2237
            (write-string ".0" stream)))))
    
    2238 2238
     
    
    2239
    +(declaim (inline pad-overflow))
    
    2240
    +(defun pad-overflow (stream field-len w overflowchar padchar field-writer)
    
    2241
    +  "Apply width/pad/overflow rules.  FIELD-WRITER is a thunk of zero
    
    2242
    +   arguments that writes the field characters to STREAM."
    
    2243
    +  (declare (function field-writer))
    
    2244
    +  (cond
    
    2245
    +    ((null w)
    
    2246
    +     (funcall field-writer))
    
    2247
    +    ((and (> field-len w)
    
    2248
    +	  overflowchar)
    
    2249
    +     (loop repeat w do
    
    2250
    +       (write-char overflowchar stream)))
    
    2251
    +    ((> field-len w)
    
    2252
    +     (funcall field-writer))
    
    2253
    +    (t
    
    2254
    +     (let ((pad (or padchar #\space)))
    
    2255
    +       (loop repeat (- w field-len)
    
    2256
    +             do (write-char pad stream)))
    
    2257
    +     (funcall field-writer))))
    
    2258
    +
    
    2259
    +(declaim (inline get-sign-and-absolute-value))
    
    2260
    +(defun get-sign-and-absolute-value (value)
    
    2261
    +  "Returns two values for the float VALUE: T if the value is negative and
    
    2262
    +  the double-float absolute value of VALUE."
    
    2263
    +  (declare (type (or single-float double-float) value))
    
    2264
    +  (etypecase value
    
    2265
    +    (double-float
    
    2266
    +     (values (minusp (float-sign value))
    
    2267
    +	     (abs value)))
    
    2268
    +    (single-float
    
    2269
    +     (values (minusp (float-sign value))
    
    2270
    +	     (abs (float (value 1d0)))))))
    
    2271
    +     
    
    2239 2272
     (defun d2exp-precision (d k)
    
    2240 2273
       ;; Compute precision for d2exp when CL requests D digits and the
    
    2241 2274
       ;; scale factor is K."
    
    ... ... @@ -2294,13 +2327,7 @@ radix-R. If you have a power-list then pass it in as PL."
    2294 2327
                (type (or null (and unsigned-byte fixnum)) w d e)
    
    2295 2328
                (optimize (speed 3)))
    
    2296 2329
       (multiple-value-bind (is-negative-p abs-value)
    
    2297
    -      (etypecase value
    
    2298
    -	(double-float
    
    2299
    -	 (values (minusp (float-sign value))
    
    2300
    -		 (abs value)))
    
    2301
    -	(single-float
    
    2302
    -	 (values (minusp (float-sign value))
    
    2303
    -		 (abs (float value 1d0)))))
    
    2330
    +      (get-sign-and-absolute-value value)
    
    2304 2331
         (with-output-to-string (stream)
    
    2305 2332
           (cond
    
    2306 2333
             (d
    
    ... ... @@ -2337,26 +2364,6 @@ radix-R. If you have a power-list then pass it in as PL."
    2337 2364
                                            nil))))))))))))
    
    2338 2365
     
    
    2339 2366
     ;;; Ryu ~F
    
    2340
    -(declaim (inline pad-overflow))
    
    2341
    -(defun pad-overflow (stream field-len w overflowchar padchar field-writer)
    
    2342
    -  "Apply width/pad/overflow rules.  FIELD-WRITER is a thunk of zero
    
    2343
    -   arguments that writes the field characters to STREAM."
    
    2344
    -  (declare (function field-writer))
    
    2345
    -  (cond
    
    2346
    -    ((null w)
    
    2347
    -     (funcall field-writer))
    
    2348
    -    ((and (> field-len w)
    
    2349
    -	  overflowchar)
    
    2350
    -     (loop repeat w do
    
    2351
    -       (write-char overflowchar stream)))
    
    2352
    -    ((> field-len w)
    
    2353
    -     (funcall field-writer))
    
    2354
    -    (t
    
    2355
    -     (let ((pad (or padchar #\space)))
    
    2356
    -       (loop repeat (- w field-len)
    
    2357
    -             do (write-char pad stream)))
    
    2358
    -     (funcall field-writer))))
    
    2359
    -
    
    2360 2367
     (defun format-f-fixed (stream abs-value is-negative-p w d
    
    2361 2368
                            overflowchar padchar at-sign-p)
    
    2362 2369
       (declare (double-float abs-value))
    
    ... ... @@ -2370,7 +2377,7 @@ radix-R. If you have a power-list then pass it in as PL."
    2370 2377
                        (at-sign-p     (write-char #\+ stream)))
    
    2371 2378
                  (write-string raw-string stream)
    
    2372 2379
                  (when need-dot (write-char #\. stream))))
    
    2373
    -      (declare (dynamic-extent #'writ-field))
    
    2380
    +      (declare (dynamic-extent #'write-field))
    
    2374 2381
           (pad-overflow stream field-len w overflowchar padchar #'write-field))))
    
    2375 2382
     	       
    
    2376 2383
     (defun reshape-fixed-length (digit-count exponent)
    
    ... ... @@ -2430,9 +2437,8 @@ radix-R. If you have a power-list then pass it in as PL."
    2430 2437
                     (emit-shortest stream mantissa exponent is-negative-p w
    
    2431 2438
                                    overflowchar padchar at-sign-p))))
    
    2432 2439
             (t
    
    2433
    -	 (let ((abs-value (etypecase value
    
    2434
    -			    (double-float (abs value))
    
    2435
    -			    (single-float (abs value)))))
    
    2440
    +	 (multiple-value-bind (is-negative-p abs-value)
    
    2441
    +	     (get-sign-and-absolute-value value)
    
    2436 2442
                (format-f-fixed stream abs-value is-negative-p w d-fit
    
    2437 2443
                                overflowchar padchar at-sign-p)))))))
    
    2438 2444
     
    
    ... ... @@ -2441,13 +2447,7 @@ radix-R. If you have a power-list then pass it in as PL."
    2441 2447
     	   (fixnum k)
    
    2442 2448
     	   (type (or null (and unsigned-byte fixnum)) w d))
    
    2443 2449
       (multiple-value-bind (is-negative-p abs-value)
    
    2444
    -      (etypecase value
    
    2445
    -	(double-float
    
    2446
    -	 (values (minusp (float-sign value))
    
    2447
    -		 (abs value)))
    
    2448
    -	(single-float
    
    2449
    -	 (values (minusp (float-sign value))
    
    2450
    -		 (abs (float value 1d0)))))
    
    2450
    +      (get-sign-and-absolute-value value)
    
    2451 2451
         (with-output-to-string (s)
    
    2452 2452
           (cond ((not (zerop k))
    
    2453 2453
     	     ;;  Complex case that doesn't fit with what d2s and d2fixed