Raymond Toy pushed to branch rtoy-print-using-ryu at cmucl / cmucl

Commits:

2 changed files:

Changes:

  • src/code/float.lisp
    ... ... @@ -1152,26 +1152,74 @@
    1152 1152
     			  (assert (= len (the fixnum (1+ digits))))
    
    1153 1153
     			  (multiple-value-bind (f0)
    
    1154 1154
     			      (floatit (ash bits -1))
    
    1155
    -			    #+nil
    
    1156
    -			    (progn
    
    1157
    -                              (format t "x = ~A~%" x)
    
    1158
    -			      (format t "1: f0, f1 = ~A~%" f0)
    
    1159
    -			      (format t "   scale = ~A~%" (1+ scale)))
    
    1160
    -			    
    
    1161 1155
     			    (scale-float f0 (1+ scale))))
    
    1162 1156
     			 (t
    
    1163 1157
     			  (multiple-value-bind (f0)
    
    1164 1158
     			      (floatit bits)
    
    1165
    -			    #+nil
    
    1166
    -			    (progn
    
    1167
    -			      (format t "2: f0, f1 = ~A~%" f0)
    
    1168
    -			      (format t "   scale = ~A~%" scale)
    
    1169
    -			      (format t "scale-float f0 = ~A~%" (scale-float f0 scale)))
    
    1170
    -			    ;; SCALE-FLOAT-MAYBE-UNDERFLOW correctly rounds
    
    1171
    -			    ;; to nearest when constructing a denormal result,
    
    1172
    -			    ;; so just call SCALE-FLOAT here without any
    
    1173
    -			    ;; boundary-case fixup.
    
    1174 1159
     			    (scale-float f0 scale))))))
    
    1160
    +	       (denormal-excess ()
    
    1161
    +		 ;; How many bits of precision the result loses by being
    
    1162
    +		 ;; denormal instead of normal.  A normal-precision return
    
    1163
    +		 ;; would be BITS*2^(SCALE-DIGITS) with BITS having DIGITS
    
    1164
    +		 ;; bits.  Once it's known that this representation will
    
    1165
    +		 ;; produce a denormal -- equivalently, that SCALE-FLOAT-
    
    1166
    +		 ;; MAYBE-UNDERFLOW would take the underflow branch --
    
    1167
    +		 ;; (1 - BIAS) - SCALE bits of the mantissa fall below the
    
    1168
    +		 ;; denormal's narrower storage and must be rounded off.
    
    1169
    +		 ;; Zero in the normal range.
    
    1170
    +		 (let ((bias
    
    1171
    +			(ecase format
    
    1172
    +			  (single-float vm:single-float-bias)
    
    1173
    +			  (double-float vm:double-float-bias))))
    
    1174
    +		   (declare (fixnum bias))
    
    1175
    +		   (max 0 (the fixnum
    
    1176
    +				(- (the fixnum (- 1 bias))
    
    1177
    +				   scale)))))
    
    1178
    +	       (round-denormal (fraction-and-guard rem excess)
    
    1179
    +		 ;; FRACTION-AND-GUARD has (1+ DIGITS) bits with one guard
    
    1180
    +		 ;; bit; round it to (- DIGITS EXCESS) bits using round-to-
    
    1181
    +		 ;; nearest, ties to even, with REM as the sticky tail.
    
    1182
    +		 ;; Drops EXCESS+1 low bits in a single step.  This is the
    
    1183
    +		 ;; one rounding the denormal result undergoes; no
    
    1184
    +		 ;; subsequent SCALE-FLOAT call is needed, so there is no
    
    1185
    +		 ;; double rounding.
    
    1186
    +		 (declare (type unsigned-byte fraction-and-guard rem)
    
    1187
    +			  (fixnum excess))
    
    1188
    +		 (let* ((shift (1+ excess))
    
    1189
    +			(low (ldb (byte shift 0) fraction-and-guard))
    
    1190
    +			(quot (ash fraction-and-guard (- shift)))
    
    1191
    +			(halfway (ash 1 excess)))
    
    1192
    +		   (declare (fixnum shift))
    
    1193
    +		   (cond ((< low halfway) quot)
    
    1194
    +			 ((> low halfway) (1+ quot))
    
    1195
    +			 ((not (zerop rem)) (1+ quot))
    
    1196
    +			 ((oddp quot) (1+ quot))
    
    1197
    +			 (t quot))))
    
    1198
    +	       (denormal-from-bits (mantissa excess)
    
    1199
    +		 ;; MANTISSA has at most (- DIGITS EXCESS) bits and is the
    
    1200
    +		 ;; stored significand of a denormal result.  Denormal
    
    1201
    +		 ;; storage holds (1- DIGITS) bits, so rounding can carry
    
    1202
    +		 ;; into the smallest normal only when EXCESS = 1, in
    
    1203
    +		 ;; which case MANTISSA can be exactly (ASH 1 (1- DIGITS)).
    
    1204
    +		 (declare (fixnum excess))
    
    1205
    +		 (let ((sign (if plusp 0 1)))
    
    1206
    +		   (case format
    
    1207
    +		     (single-float
    
    1208
    +		      (cond ((and (= excess 1)
    
    1209
    +				  (= mantissa
    
    1210
    +				     (ash 1 (1- vm:single-float-digits))))
    
    1211
    +			     (single-from-bits
    
    1212
    +			      sign vm:single-float-normal-exponent-min 0))
    
    1213
    +			    (t
    
    1214
    +			     (single-from-bits sign 0 mantissa))))
    
    1215
    +		     (double-float
    
    1216
    +		      (cond ((and (= excess 1)
    
    1217
    +				  (= mantissa
    
    1218
    +				     (ash 1 (1- vm:double-float-digits))))
    
    1219
    +			     (double-from-bits
    
    1220
    +			      sign vm:double-float-normal-exponent-min 0))
    
    1221
    +			    (t
    
    1222
    +			     (double-from-bits sign 0 mantissa)))))))
    
    1175 1223
     	       (floatit (bits)
    
    1176 1224
     		 (let ((sign (if plusp 0 1)))
    
    1177 1225
     		   (case format
    
    ... ... @@ -1189,16 +1237,36 @@
    1189 1237
     	      (declare (fixnum extra))
    
    1190 1238
     	      (cond ((/= extra 1)
    
    1191 1239
     		     (assert (> extra 1)))
    
    1192
    -		    ((oddp fraction-and-guard)
    
    1193
    -		     (return
    
    1194
    -		      (if (zerop rem)
    
    1195
    -			  (float-and-scale
    
    1196
    -			   (if (zerop (logand fraction-and-guard 2))
    
    1197
    -			       fraction-and-guard
    
    1198
    -			       (1+ fraction-and-guard)))
    
    1199
    -			  (float-and-scale (1+ fraction-and-guard)))))
    
    1200 1240
     		    (t
    
    1201
    -		     (return (float-and-scale fraction-and-guard)))))
    
    1241
    +		     (return
    
    1242
    +		      (let ((excess (denormal-excess)))
    
    1243
    +			(cond
    
    1244
    +			  ((zerop excess)
    
    1245
    +			   ;; Normal result: original odd/even tie-break.
    
    1246
    +			   (cond ((oddp fraction-and-guard)
    
    1247
    +				  (if (zerop rem)
    
    1248
    +				      (float-and-scale
    
    1249
    +				       (if (zerop
    
    1250
    +					    (logand fraction-and-guard 2))
    
    1251
    +					   fraction-and-guard
    
    1252
    +					   (1+ fraction-and-guard)))
    
    1253
    +				      (float-and-scale
    
    1254
    +				       (1+ fraction-and-guard))))
    
    1255
    +				 (t
    
    1256
    +				  (float-and-scale fraction-and-guard))))
    
    1257
    +			  (t
    
    1258
    +			   ;; Denormal result: re-round directly to the
    
    1259
    +			   ;; denormal's narrower precision so the only
    
    1260
    +			   ;; rounding step happens here.  Rounding to
    
    1261
    +			   ;; DIGITS first and re-rounding via
    
    1262
    +			   ;; SCALE-FLOAT-MAYBE-UNDERFLOW would double-
    
    1263
    +			   ;; round (e.g. 7.290983e-39 would land on an
    
    1264
    +			   ;; artifical tie at the 24-bit boundary).
    
    1265
    +			   (let ((mantissa
    
    1266
    +				  (round-denormal fraction-and-guard rem
    
    1267
    +						  excess)))
    
    1268
    +			     (declare (type unsigned-byte mantissa))
    
    1269
    +			     (denormal-from-bits mantissa excess)))))))))
    
    1202 1270
     	    (setq shifted-num (ash shifted-num -1))
    
    1203 1271
     	    (incf scale)))))))
    
    1204 1272
     
    

  • tests/float.lisp
    ... ... @@ -853,3 +853,176 @@
    853 853
       (ext:with-float-traps-masked (:underflow :inexact)
    
    854 854
         (assert-equal 0 (kernel:double-float-high-bits 1.1d-322))
    
    855 855
         (assert-equal #x16 (kernel:double-float-low-bits 1.1d-322))))
    
    856
    +
    
    857
    +(define-test float-ratio-float.denormal-double-rounding.single
    
    858
    +    (:tag :issues)
    
    859
    +  ;; Regression for the double-rounding bug exposed by reading
    
    860
    +  ;; "7.290983e-39".  The exact rational lies at 5203019.332 * 2^-149,
    
    861
    +  ;; below halfway between denormals #x4f644b and #x4f644c.  An earlier
    
    862
    +  ;; fix rounded to 24 bits first (lifting it to the artificial tie
    
    863
    +  ;; 5203019.5 * 2^-149) and then re-rounded ties-to-even to #x4f644c.
    
    864
    +  ;; FLOAT-RATIO-FLOAT now re-rounds directly to denormal precision in a
    
    865
    +  ;; single step, yielding the correctly-rounded #x4f644b.
    
    866
    +  (assert-equal #x4f644b
    
    867
    +		(kernel:single-float-bits 7.290983e-39)))
    
    868
    +
    
    869
    +(define-test float-ratio-float.denormal-low-below-halfway.single
    
    870
    +    (:tag :issues)
    
    871
    +  ;; Exercise ROUND-DENORMAL's `low < halfway' branch via FLOAT-RATIO-
    
    872
    +  ;; FLOAT.  Value 11/10 * least-positive is 1.1 denormal-units; the
    
    873
    +  ;; loop sees several bits of low and the comparison must take the
    
    874
    +  ;; round-down path so the mantissa stays at 1.
    
    875
    +  (let ((x (* 11/10 (expt 2 -149))))
    
    876
    +    (assert-equal #x00000001
    
    877
    +		  (kernel:single-float-bits
    
    878
    +		   (kernel::float-ratio-float x 'single-float))
    
    879
    +		  x)))
    
    880
    +
    
    881
    +(define-test float-ratio-float.denormal-low-above-halfway.single
    
    882
    +    (:tag :issues)
    
    883
    +  ;; ROUND-DENORMAL's `low > halfway' branch: 17/10 * least-positive
    
    884
    +  ;; (= 1.7 denormal-units), past halfway between 1 and 2, rounds up.
    
    885
    +  (let ((x (* 17/10 (expt 2 -149))))
    
    886
    +    (assert-equal #x00000002
    
    887
    +		  (kernel:single-float-bits
    
    888
    +		   (kernel::float-ratio-float x 'single-float))
    
    889
    +		  x)))
    
    890
    +
    
    891
    +(define-test float-ratio-float.denormal-tie-to-even.single
    
    892
    +    (:tag :issues)
    
    893
    +  ;; ROUND-DENORMAL's tie path, REM = 0: exact halfway between two
    
    894
    +  ;; denormals rounds to even.  3/2 * least-positive ties between
    
    895
    +  ;; denormals 1 and 2; the even neighbour is 2.
    
    896
    +  (let ((x (* 3/2 (expt 2 -149))))
    
    897
    +    (assert-equal #x00000002
    
    898
    +		  (kernel:single-float-bits
    
    899
    +		   (kernel::float-ratio-float x 'single-float))
    
    900
    +		  x))
    
    901
    +  ;; 5/2 * least-positive ties between 2 and 3; even is 2.
    
    902
    +  (let ((x (* 5/2 (expt 2 -149))))
    
    903
    +    (assert-equal #x00000002
    
    904
    +		  (kernel:single-float-bits
    
    905
    +		   (kernel::float-ratio-float x 'single-float))
    
    906
    +		  x))
    
    907
    +  ;; 7/2 * least-positive ties between 3 and 4; even is 4.
    
    908
    +  (let ((x (* 7/2 (expt 2 -149))))
    
    909
    +    (assert-equal #x00000004
    
    910
    +		  (kernel:single-float-bits
    
    911
    +		   (kernel::float-ratio-float x 'single-float))
    
    912
    +		  x))
    
    913
    +  ;; 1/2 * least-positive ties between 0 and 1; even is 0.
    
    914
    +  (let ((x (* 1/2 (expt 2 -149))))
    
    915
    +    (assert-equal #x00000000
    
    916
    +		  (kernel:single-float-bits
    
    917
    +		   (kernel::float-ratio-float x 'single-float))
    
    918
    +		  x)))
    
    919
    +
    
    920
    +(define-test float-ratio-float.denormal-tie-with-sticky.single
    
    921
    +    (:tag :issues)
    
    922
    +  ;; ROUND-DENORMAL's tie path with REM != 0: when the loop's
    
    923
    +  ;; FRACTION-AND-GUARD reaches an exact halfway pattern but the
    
    924
    +  ;; division has a nonzero remainder, the rounding must go up because
    
    925
    +  ;; the original rational is strictly above halfway.  3/2 * least-
    
    926
    +  ;; positive + a tiny fraction of least-positive lands just past tie 1-2,
    
    927
    +  ;; rounds up to 2.
    
    928
    +  (let ((x (+ (* 3/2 (expt 2 -149))
    
    929
    +	      (* (expt 2 -149) 1/1000000000))))
    
    930
    +    (assert-equal #x00000002
    
    931
    +		  (kernel:single-float-bits
    
    932
    +		   (kernel::float-ratio-float x 'single-float))
    
    933
    +		  x))
    
    934
    +  ;; 1/2 + tiny: just past tie 0-1, rounds up to 1.
    
    935
    +  (let ((x (+ (* 1/2 (expt 2 -149))
    
    936
    +	      (* (expt 2 -149) 1/1000000000))))
    
    937
    +    (assert-equal #x00000001
    
    938
    +		  (kernel:single-float-bits
    
    939
    +		   (kernel::float-ratio-float x 'single-float))
    
    940
    +		  x)))
    
    941
    +
    
    942
    +(define-test float-ratio-float.denormal-excess.single
    
    943
    +    (:tag :issues)
    
    944
    +  ;; Exercise DENORMAL-EXCESS over a range of magnitudes.  EXCESS
    
    945
    +  ;; varies inversely with the result's magnitude; each case has the
    
    946
    +  ;; result land on a chosen denormal so an off-by-one in the EXCESS
    
    947
    +  ;; computation would shift the result by a factor of two.
    
    948
    +  ;; EXCESS = 1: value near smallest normal; pick (2^23 - 1) * 2^-149,
    
    949
    +  ;; the largest denormal.
    
    950
    +  (let ((x (* (1- (expt 2 23)) (expt 2 -149))))
    
    951
    +    (assert-equal #x007fffff
    
    952
    +		  (kernel:single-float-bits
    
    953
    +		   (kernel::float-ratio-float x 'single-float))
    
    954
    +		  x))
    
    955
    +  ;; EXCESS = 4: small denormal, mantissa 2^19.
    
    956
    +  (let ((x (expt 2 -130)))
    
    957
    +    (assert-equal (ash 1 19)
    
    958
    +		  (kernel:single-float-bits
    
    959
    +		   (kernel::float-ratio-float x 'single-float))
    
    960
    +		  x))
    
    961
    +  ;; EXCESS = 21: very small denormal, mantissa 8.
    
    962
    +  (let ((x (* 8 (expt 2 -149))))
    
    963
    +    (assert-equal #x00000008
    
    964
    +		  (kernel:single-float-bits
    
    965
    +		   (kernel::float-ratio-float x 'single-float))
    
    966
    +		  x))
    
    967
    +  ;; EXCESS = 23 (the maximum): only the bottom three denormals are
    
    968
    +  ;; reachable.  3 * least-positive gives mantissa 3.
    
    969
    +  (let ((x (* 3 (expt 2 -149))))
    
    970
    +    (assert-equal #x00000003
    
    971
    +		  (kernel:single-float-bits
    
    972
    +		   (kernel::float-ratio-float x 'single-float))
    
    973
    +		  x)))
    
    974
    +
    
    975
    +(define-test float-ratio-float.denormal-carry-to-normal.single
    
    976
    +    (:tag :issues)
    
    977
    +  ;; DENORMAL-FROM-BITS's carry-into-smallest-normal branch.  Rounding
    
    978
    +  ;; promotes the denormal mantissa to 2^(DIGITS-1), which doesn't fit
    
    979
    +  ;; in the denormal's stored mantissa width; the result must be the
    
    980
    +  ;; smallest normal (stored exponent = 1, mantissa = 0).
    
    981
    +  ;;
    
    982
    +  ;; (2^24 - 1)/2 * 2^-149 is an exact tie between the largest denormal
    
    983
    +  ;; (mantissa 2^23 - 1) and the smallest normal (2^-126); the latter
    
    984
    +  ;; has stored mantissa 0 which is even, so the tie rounds to it.
    
    985
    +  (let ((x (* (1- (expt 2 24)) 1/2 (expt 2 -149))))
    
    986
    +    (assert-equal #x00800000
    
    987
    +		  (kernel:single-float-bits
    
    988
    +		   (kernel::float-ratio-float x 'single-float))
    
    989
    +		  x))
    
    990
    +  ;; Just past that tie, also rounds up to smallest normal.
    
    991
    +  (let ((x (+ (* (1- (expt 2 24)) 1/2 (expt 2 -149))
    
    992
    +	      (* (expt 2 -149) 1/1000))))
    
    993
    +    (assert-equal #x00800000
    
    994
    +		  (kernel:single-float-bits
    
    995
    +		   (kernel::float-ratio-float x 'single-float))
    
    996
    +		  x))
    
    997
    +  ;; Just below that tie, rounds down to the largest denormal.
    
    998
    +  (let ((x (- (* (1- (expt 2 24)) 1/2 (expt 2 -149))
    
    999
    +	      (* (expt 2 -149) 1/1000))))
    
    1000
    +    (assert-equal #x007fffff
    
    1001
    +		  (kernel:single-float-bits
    
    1002
    +		   (kernel::float-ratio-float x 'single-float))
    
    1003
    +		  x)))
    
    1004
    +
    
    1005
    +(define-test float-ratio-float.denormal-double-rounding.double
    
    1006
    +    (:tag :issues)
    
    1007
    +  ;; Double-float equivalents.  Pick a rational that lands strictly
    
    1008
    +  ;; below halfway between two denormals after 53-bit rounding would
    
    1009
    +  ;; otherwise lift to the halfway position.  Verify a few denormal
    
    1010
    +  ;; cases also work end-to-end through FLOAT-RATIO-FLOAT.
    
    1011
    +  ;;
    
    1012
    +  ;; 1.1d-322 -> mantissa #x16 (= 22).
    
    1013
    +  (assert-equal 0 (kernel:double-float-high-bits 1.1d-322))
    
    1014
    +  (assert-equal #x16 (kernel:double-float-low-bits 1.1d-322))
    
    1015
    +  ;; Tie-to-even, double: 3/2 * least-positive ties between denormals
    
    1016
    +  ;; 1 and 2; even neighbour is 2.
    
    1017
    +  (let ((x (* 3/2 (expt 2 -1074))))
    
    1018
    +    (assert-equal 0 (kernel:double-float-high-bits
    
    1019
    +		     (kernel::float-ratio-float x 'double-float)))
    
    1020
    +    (assert-equal 2 (kernel:double-float-low-bits
    
    1021
    +		     (kernel::float-ratio-float x 'double-float))))
    
    1022
    +  ;; Carry into smallest normal: (2^53 - 1)/2 * 2^-1074 exactly halfway
    
    1023
    +  ;; between the largest double denormal and the smallest normal;
    
    1024
    +  ;; rounds up to the smallest normal #x00100000_00000000.
    
    1025
    +  (let* ((x (* (1- (expt 2 53)) 1/2 (expt 2 -1074)))
    
    1026
    +	 (r (kernel::float-ratio-float x 'double-float)))
    
    1027
    +    (assert-equal #x00100000 (kernel:double-float-high-bits r) x)
    
    1028
    +    (assert-equal 0 (kernel:double-float-low-bits r) x)))