This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug ada/41416] New: Conversion Float to fixed-point behaves differently for static expressions


Hi,

the following complete sample code shows different conversion behaviours
between fixed-point types and Float to fixed-point depending on whether the
expressions are static or not.  In the static case, GNAT uses a conversion
strategy comparable to truncation, otherwise rounding is used.

I assume that this difference in behaviour is unintended.

----------- start -----------
with Ada.Text_IO;
use Ada.Text_IO;

procedure Conversion is

   type FpA is delta 0.5 range -10.0 .. +10.0;
   for FpA'Small use 0.5;
   type FpB is delta 0.4 range -10.0 .. +10.0;
   for FpB'Small use 0.4;

   function MakeB(F: in Float) return FpB is
   begin
      return FpB(F);
   end MakeB;

   function MakeBFromA(F: in FpA) return FpB is
   begin
      return FpB(F);
   end MakeBFromA;

   function MakeBViaA(F: in Float) return FpB is
   begin
      return FpB(FpA(F));
   end MakeBViaA;

   function Get_Minus_1_5 return Float is
   begin
      return -1.5;
   end Get_Minus_1_5;

begin

   Put_Line(FpA'Image(FpA(-1.5)));
   Put_Line(
      FpB'Image(FpB(-1.5)) & " "                   --> -1.2  (truncation)
      & FpB'Image(FpB(Float(-1.5))) & " "          --> -1.2  (truncation)
      & FpB'Image(FpB(Get_Minus_1_5)) & " "        --> -1.6  (rounding)
      & FpB'Image(MakeB(-1.5)));                   --> -1.6  (rounding)
   Put_Line(
      FpB'Image(FpB(FpA(-1.5))) & " "              --> -1.2  (truncation)
      & FpB'Image(FpB(FpA(Float(-1.5)))) & " "     --> -1.2  (truncation)
      & FpB'Image(FpB(FpA'Succ(FpA(-2.0)))) & " "  --> -1.2  (truncation)
      & FpB'Image(FpB(FpA(Get_Minus_1_5))) & " "   --> -1.6  (rounding)
      & FpB'Image(MakeBFromA(FpA(-1.5))) & " "     --> -1.6  (rounding)
      & FpB'Image(MakeBViaA(-1.5)));               --> -1.6  (rounding)

end Conversion;
------------ end ------------

The command line I am using for building:
> gnatmake -f -gnatVa -gnata -gnatwadhl.o -O3 -save-temps conversion.adb

The output from GNAT:
----------- start -----------
conversion.adb:35:17: warning: static fixed-point value is not a multiple of
Small
conversion.adb:36:19: warning: static fixed-point value is not a multiple of
Small
conversion.adb:40:17: warning: static fixed-point value is not a multiple of
Small
conversion.adb:41:19: warning: static fixed-point value is not a multiple of
Small
conversion.adb:42:19: warning: static fixed-point value is not a multiple of
Small
gnatbind -x conversion.ali
gnatlink conversion.ali
------------ end ------------

The version of GNAT I am using:
> gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.4-2'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr
--enable-targets=all --with-tune=generic --enable-checking=release
--build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.4 (Debian 4.3.4-2)

Friendly regards,
Dirk Herrmann


-- 
           Summary: Conversion Float to fixed-point behaves differently for
                    static expressions
           Product: gcc
           Version: 4.3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dirk dot herrmann-privat at gmx dot de


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41416


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]