Index: rtl/inc/real2str.inc
===================================================================
--- rtl/inc/real2str.inc	(revision 20755)
+++ rtl/inc/real2str.inc	(working copy)
@@ -20,6 +20,10 @@
   );
   { corresponding to single   double   extended   fixed      comp for i386 }
 
+{$if not declared(mul_by_power10)}
+  function mul_by_power10 (x : ValReal; power : integer) : ValReal; forward;
+{$endif}
+
 Procedure str_real (len,f : longint; d : ValReal; real_type :treal_type; out s : string);
 {$ifdef SUPPORT_EXTENDED}
 type
@@ -31,6 +35,7 @@
   end;
 const
   maxDigits = 17;
+  bin_exponent_offset = $3FFF;
 {$else}
 {$ifdef SUPPORT_DOUBLE}
 type
@@ -42,6 +47,7 @@
   end;
 const
   maxDigits = 15;
+  bin_exponent_offset = $3FF;
 {$else}
 {$ifdef SUPPORT_SINGLE}
 type
@@ -53,6 +59,7 @@
   end;
 const
   maxDigits = 9;
+  bin_exponent_offset = $7F;
 {$endif SUPPORT_SINGLE}
 {$endif SUPPORT_DOUBLE}
 {$endif SUPPORT_EXTENDED}
@@ -63,6 +70,7 @@
 
 var
   roundCorr, corrVal, factor : valReal;
+  high_exp10_reduced,
   spos, endpos, fracCount: longint;
   correct, currprec: longint;
   temp : string;
@@ -181,6 +189,65 @@
 {$endif DEBUG_NASM}
   end;
 
+  function reduce_exponent (d : ValReal; out scaled : ValReal) : longint;
+  { Returns decimal exponent which was used for scaling, and a scaled value out }
+  var
+    bin_exponent, scale_more, scale_total : longint;
+  begin
+    reduce_exponent := 0;
+    scale_total := 0;
+    if d<>0 then
+      // loop may be needed for denormals
+      //(should not take more than 2.5 iterations)
+      repeat
+        // extract binary exponent
+        {$ifdef SUPPORT_EXTENDED}
+         {$ifndef ENDIAN_BIG}
+          bin_exponent := longint(TSplitExtended(d).words[4] and $7FFF);
+         {$else ENDIAN_BIG}
+          bin_exponent := longint(TSplitExtended(d).words[0] and $7FFF);
+         {$endif ENDIAN_BIG}
+        {$else SUPPORT_EXTENDED}
+         {$ifdef SUPPORT_DOUBLE}
+          {$ifndef ENDIAN_BIG}
+           {$ifdef FPC_DOUBLE_HILO_SWAPPED}
+            bin_exponent := longint(TSplitDouble(d).cards[0] and $7FF00000) shr 20;
+           {$else}
+            bin_exponent := longint(TSplitDouble(d).cards[1] and $7FF00000) shr 20;
+           {$endif}
+          {$else ENDIAN_BIG}
+           bin_exponent := longint(TSplitDouble(d).cards[0] and $7FF00000) shr 20;
+          {$endif ENDIAN_BIG}
+         {$else SUPPORT_DOUBLE}
+          {$ifdef SUPPORT_SINGLE}
+           bin_exponent := longint(TSplitSingle(d).cards[0] and $7F800000) shr 23;
+          {$else SUPPORT_SINGLE}
+           {$error Unsupported ValReal type in real2str::reduce_exponent}
+          {$endif SUPPORT_SINGLE}
+         {$endif SUPPORT_DOUBLE}
+        {$endif SUPPORT_EXTENDED}
+        // log10(2) [approx] = 19728/65536
+        scale_more := (bin_exponent - bin_exponent_offset) * 19728 div 65536;
+        if scale_more > maxDigits+1 then
+          dec(scale_more,maxDigits)
+        else if scale_more < -(maxDigits+1) then
+          inc(scale_more,maxDigits)
+        else
+          break;
+        // do some scale-up/down
+        d := mul_by_power10(d,-scale_more);
+        if d = 0 then
+          exit; // sanity; no changes were made [should never get here]
+        inc(scale_total,scale_more);
+      until false;
+    scaled := d;
+    reduce_exponent := scale_total;
+  end;
+
 begin
   case real_type of
     rt_s32real :
@@ -330,9 +397,34 @@
       temp := ' 0';
       { position in the temporary output string }
       spos := 2;
+
+      // workaround to make follow-up things go somewhat faster
+      high_exp10_reduced := 0;
+      case real_type of
+      // blacklist, in order of increasing headache:
+//?     rt_s32real :;   
+          // ? needs additional testing to ensure any reasonable benefit
+          //   without lost of accuracy due to an extra conversion
+        rt_c64bit, rt_currency :;
+          // no much sense to touch them -- they are already almost 64-bit
+          // integers with relatively low exponent, and the CURRENCY one
+          // is not a subject to change its current behavior even slightly
+          // without additional pain
+      else
+      // acceptable:
+      // ? rt_s32real [see above]
+      //   rt_s64real
+      //   rt_s80real, rt_sc80real
+      // ? rt_s128real [have not tried]
+        high_exp10_reduced := reduce_exponent(d,d);
+      end;
+  
       { get the integer part }
       correct := 0;
       GetIntPart(d);
+
+      inc(correct,high_exp10_reduced); // end of workaround (?misplaced?)
+
       { now process the fractional part }
       if d > 1.0- roundCorr then
         d := frac(d);
