diff --git a/gcc/ada/libgnat/s-imagef.adb b/gcc/ada/libgnat/s-imagef.adb index 3f6bfa20cb2..6194a3163de 100644 --- a/gcc/ada/libgnat/s-imagef.adb +++ b/gcc/ada/libgnat/s-imagef.adb @@ -307,6 +307,9 @@ package body System.Image_F is YY : Int := Y; -- First two operands of the scaled divide + J : Natural; + -- Loop index + begin -- Set the first character like Image @@ -317,59 +320,61 @@ package body System.Image_F is Ndigs := 0; end if; - for J in 1 .. N loop - exit when XX = 0; + -- First round of scaled divide + if XX /= 0 then + Scaled_Divide (XX, YY, Z, Q, R => XX, Round => False); + if Q /= 0 then + Set_Image_Integer (Q, Digs, Ndigs); + end if; + + Scale := Scale + D; + + -- Prepare for next round, if any + + YY := 10**Maxdigs; + end if; + + J := 2; + while J <= N and then XX /= 0 loop Scaled_Divide (XX, YY, Z, Q, R => XX, Round => False); - if J = 1 then + pragma Assert (-10**Maxdigs < Q and then Q < 10**Maxdigs); + + Len := 0; + Set_Image_Integer (abs Q, Buf, Len); + + pragma Assert (1 <= Len and then Len <= Maxdigs); + + -- If no character but the space has been written, write the + -- minus if need be, since Set_Image_Integer did not do it. + + if Ndigs <= 1 then if Q /= 0 then - Set_Image_Integer (Q, Digs, Ndigs); - end if; - - Scale := Scale + D; - - -- Prepare for next round, if any - - YY := 10**Maxdigs; - - else - pragma Assert (-10**Maxdigs < Q and then Q < 10**Maxdigs); - - Len := 0; - Set_Image_Integer (abs Q, Buf, Len); - - pragma Assert (1 <= Len and then Len <= Maxdigs); - - -- If no character but the space has been written, write the - -- minus if need be, since Set_Image_Integer did not do it. - - if Ndigs <= 1 then - if Q /= 0 then - if Ndigs = 0 then - Digs (1) := '-'; - end if; - - Digs (2 .. Len + 1) := Buf (1 .. Len); - Ndigs := Len + 1; + if Ndigs = 0 then + Digs (1) := '-'; end if; - -- Or else pad the output with zeroes up to Maxdigs - - else - for K in 1 .. Maxdigs - Len loop - Digs (Ndigs + K) := '0'; - end loop; - - for K in 1 .. Len loop - Digs (Ndigs + Maxdigs - Len + K) := Buf (K); - end loop; - - Ndigs := Ndigs + Maxdigs; + Digs (2 .. Len + 1) := Buf (1 .. Len); + Ndigs := Len + 1; end if; - Scale := Scale + Maxdigs; + -- Or else pad the output with zeroes up to Maxdigs + + else + for K in 1 .. Maxdigs - Len loop + Digs (Ndigs + K) := '0'; + end loop; + + for K in 1 .. Len loop + Digs (Ndigs + Maxdigs - Len + K) := Buf (K); + end loop; + + Ndigs := Ndigs + Maxdigs; end if; + + Scale := Scale + Maxdigs; + J := J + 1; end loop; -- If no digit was output, this is zero