https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116527

            Bug ID: 116527
           Summary: conversion from float to char incorrect
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xiaoyi_wu at yahoo dot com
  Target Milestone: ---

Created attachment 59024
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59024&action=edit
source code to indicate the problem

I have a fedora 40 virtual machine running on M3 MacBook Pro.  Since the M3 is
arm CPU instead of Intel, the default char in C++ in fedora is unsigned (unlike
on Intel, which is signed).  In the attached source code, function foo returns
float, function bar returns double, both are assigned to char variable a and b.
 But the conversion is incorrect for float, and also for double when compiled
without -O.  Here is the system and gcc version:

limo:work$ uname -a
Linux limo 6.9.6-200.fc40.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Jul 21 16:23:18
UTC 2024 aarch64 GNU/Linux
limo:work$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/14/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-14.1.1-20240620/obj-aarch64-redhat-linux/isl-install
--enable-gnu-indirect-function --build=aarch64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.1.1 20240620 (Red Hat 14.1.1-6) (GCC)

If I compile the source code with -O3:

$ g++ -Wall -O3 main.cc

the output is this:

-5 0 251
-4 0 252
-3 0 253
-2 0 254
-1 0 255
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4

This means double -1 convert to char will be 255, but float -1 convert to char
will be 0, which is wrong.  If I compile without optimization:

$ gcc -Wall main.cc

then the output is this:

-5 0 0
-4 0 0
-3 0 0
-2 0 0
-1 0 0
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4

which means both negative float and negative double convert to char becomes 0.

Basically, negative float convert to char will result in 0, while negative
double convert to char will result in 0 when compiled without optimization.

Reply via email to