https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120158
Bug ID: 120158 Summary: Incorrect UNSIGNED maxval/maxloc etc. Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libfortran Assignee: unassigned at gcc dot gnu.org Reporter: jakub at gcc dot gnu.org Target Milestone: --- maxval/maxloc use #if defined('atype_inf`) maxval = -atype_inf; #else maxval = atype_min; #endif', compared to minval/minloc #if defined('atype_inf`) minval = atype_inf; #else minval = atype_max; #endif', Both of that is reasonable, but I think the atype_min definition for UNSIGNED is incorrect. The ../../../libgfortran/generated/maxloc0_8_m1.c:274:14: warning: unsigned conversion from ‘int’ to ‘GFC_UINTEGER_1’ {aka ‘unsigned char’} changes value from ‘-255’ to ‘1’ [-Woverflow] ../../../libgfortran/generated/maxloc0_4_m2.c:99:14: warning: unsigned conversion from ‘int’ to ‘GFC_UINTEGER_2’ {aka ‘short unsigned int’} changes value from ‘-65535’ to ‘1’ [-Woverflow] etc. warnings are suspicious. At least in C, minimum for unsigned is 0 and maximum 0xffffffff while signed int have minimum of -0x7fffffff - 1 and maximum 0x7fffffff. In Fortran for INTEGER I believe maximum is 0x7fffffff and minimum -0x7fffffff, but it would really surprise me if UNSIGNED maximum wasn't 0xffffffff and minimum 0. So, I think we need to somehow arrange for atype_min to be ((GFC_UINTEGER_N) 0) or so (for N 1, 2, 4, 8 or 16), or predefine macros for it in libgfortran and just arrange for define(atype_min,ifelse(regexp(file, `_\(.\)[0-9]*\.c$',`\1'),`i',`(-'atype_max`-1)',`-'atype_max))dnl to be perhaps --- libgfortran/m4/iparm.m4.jj 2024-09-25 17:25:07.201360961 +0200 +++ libgfortran/m4/iparm.m4 2025-05-07 15:53:42.940954331 +0200 @@ -28,7 +28,7 @@ define_type(rtype, rtype_tmp)dnl define(rtype_qual,`_'rtype_kind)dnl ')dnl define(atype_max, atype_name`_HUGE')dnl -define(atype_min,ifelse(regexp(file, `_\(.\)[0-9]*\.c$', `\1'),`i',`(-'atype_max`-1)',`-'atype_max))dnl +define(atype_min,ifelse(index(atype_name,`UNSIGNED`),-1,ifelse(regexp(file, `_\(.\)[0-9]*\.c$', `\1'),`i',`(-'atype_max`-1)',`-'atype_max),`((`atype_name`) 0)`))dnl define(atype_inf, atype_name`_INFINITY')dnl define(atype_nan, atype_name`_QUIET_NAN')dnl define(name, regexp(regexp(file, `[^/]*$', `\&'), `^\([^_]*\)_', `\1'))dnl ? So far completely untested.