https://gcc.gnu.org/g:bd6079f1a465fad5c9916bcad71c583a69fbd281

commit r17-5538-gbd6079f1a465fad5c9916bcad71c583a69fbd281
Author: Thomas Koenig <[email protected]>
Date:   Sat Oct 24 17:24:42 2026 +0200

    LEADZ, POPCNT, POPPAR and TRAILZ for unsigned numbers.
    
    Playing around with UNSIGNED, I noted that LEADZ and TRAILZ were rejected
    for UNSIGNED, which makes no sense.  POPCNT and POPPAR worked, but this
    was not documented.  All that was needed for the LEADZ and TRAILZ to work
    was to adjust the check routine.
    
    gcc/fortran/ChangeLog:
    
            PR fortran/128124
            * gfortran.texi: Note that LEADZ, POPCNT, POPPAR and TRAILZ take
            unsigned arguments.
            * intrinsic.cc (add_functions): LEADZ and TRAILZ also take unsigned
            arguments.
            * intrinsic.texi: Document unsigned arguments for LEADZ, POPCNT, 
POPPAR
            and TRAILZ.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/128124
            * gfortran.dg/unsigned_45.f90: New test.

Diff:
---
 gcc/fortran/gfortran.texi                 |  4 ++
 gcc/fortran/intrinsic.cc                  |  4 +-
 gcc/fortran/intrinsic.texi                |  8 ++--
 gcc/testsuite/gfortran.dg/unsigned_45.f90 | 67 +++++++++++++++++++++++++++++++
 4 files changed, 77 insertions(+), 6 deletions(-)

diff --git a/gcc/fortran/gfortran.texi b/gcc/fortran/gfortran.texi
index c9f45df1882f..24ae50083ff2 100644
--- a/gcc/fortran/gfortran.texi
+++ b/gcc/fortran/gfortran.texi
@@ -2622,6 +2622,7 @@ The following intrinsics take unsigned arguments:
 @item @code{IPARITY}, @pxref{IPARITY}
 @item @code{ISHFT}, @pxref{ISHFT}
 @item @code{ISHFTC}, @pxref{ISHFTC}
+@item @code{LEADZ}, @pxref{LEADZ}
 @item @code{MATMUL}, @pxref{MATMUL}
 @item @code{MAX}, @pxref{MAX}
 @item @code{MAXLOC}, @pxref{MAXLOC}
@@ -2636,6 +2637,8 @@ The following intrinsics take unsigned arguments:
 @item @code{MVBITS}, @pxref{MVBITS}
 @item @code{NOT}, @pxref{NOT}
 @item @code{OUT_OF_RANGE}, @pxref{OUT_OF_RANGE}
+@item @code{POPCNT}, @pxref{POPCNT}
+@item @code{POPPAR}, @pxref{POPPAR}
 @item @code{PRODUCT}, @pxref{PRODUCT}
 @item @code{RANDOM_NUMBER}, @pxref{RANDOM_NUMBER}
 @item @code{RANGE}, @pxref{RANGE}
@@ -2644,6 +2647,7 @@ The following intrinsics take unsigned arguments:
 @item @code{SHIFTL}, @pxref{SHIFTL}
 @item @code{SHIFTR}, @pxref{SHIFTR}
 @item @code{SUM}, @pxref{SUM}
+@item @code{TRAILZ}, @pxref{TRAILZ}
 @item @code{TRANSPOSE}, @pxref{TRANSPOSE}
 @item @code{TRANSFER}, @pxref{TRANSFER}
 @end itemize
diff --git a/gcc/fortran/intrinsic.cc b/gcc/fortran/intrinsic.cc
index b0c246daeb4e..b9f0fd0f085f 100644
--- a/gcc/fortran/intrinsic.cc
+++ b/gcc/fortran/intrinsic.cc
@@ -2535,7 +2535,7 @@ add_functions (void)
 
   add_sym_1 ("leadz", GFC_ISYM_LEADZ, CLASS_ELEMENTAL, ACTUAL_NO,
             BT_INTEGER, di, GFC_STD_F2008,
-            gfc_check_i, gfc_simplify_leadz, NULL,
+            gfc_check_iu, gfc_simplify_leadz, NULL,
             i, BT_INTEGER, di, REQUIRED);
 
   make_generic ("leadz", GFC_ISYM_LEADZ, GFC_STD_F2008);
@@ -3427,7 +3427,7 @@ add_functions (void)
 
   add_sym_1 ("trailz", GFC_ISYM_TRAILZ, CLASS_ELEMENTAL, ACTUAL_NO,
             BT_INTEGER, di, GFC_STD_F2008,
-            gfc_check_i, gfc_simplify_trailz, NULL,
+            gfc_check_iu, gfc_simplify_trailz, NULL,
             i, BT_INTEGER, di, REQUIRED);
 
   make_generic ("trailz", GFC_ISYM_TRAILZ, GFC_STD_F2008);
diff --git a/gcc/fortran/intrinsic.texi b/gcc/fortran/intrinsic.texi
index 0d75b47c57be..bce59971e4bc 100644
--- a/gcc/fortran/intrinsic.texi
+++ b/gcc/fortran/intrinsic.texi
@@ -9773,7 +9773,7 @@ Elemental function
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
-@item @var{I} @tab Shall be of type @code{INTEGER}.
+@item @var{I} @tab Shall be of type @code{INTEGER} or @code{UNSIGNED}.
 @end multitable
 
 @item @emph{Return value}:
@@ -12268,7 +12268,7 @@ Elemental function
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
-@item @var{I} @tab Shall be of type @code{INTEGER}.
+@item @var{I} @tab Shall be of type @code{INTEGER} or @code{UNSIGNED}.
 @end multitable
 
 @item @emph{Return value}:
@@ -12316,7 +12316,7 @@ Elemental function
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
-@item @var{I} @tab Shall be of type @code{INTEGER}.
+@item @var{I} @tab Shall be of type @code{INTEGER} or @code{UNSIGNED}.
 @end multitable
 
 @item @emph{Return value}:
@@ -15434,7 +15434,7 @@ Elemental function
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
-@item @var{I} @tab Shall be of type @code{INTEGER}.
+@item @var{I} @tab Shall be of type @code{INTEGER} or @code{UNSIGNED}.
 @end multitable
 
 @item @emph{Return value}:
diff --git a/gcc/testsuite/gfortran.dg/unsigned_45.f90 
b/gcc/testsuite/gfortran.dg/unsigned_45.f90
new file mode 100644
index 000000000000..076e0376b00e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/unsigned_45.f90
@@ -0,0 +1,67 @@
+! { dg-do run }
+! { dg-additional-options "-funsigned" }
+! Check for implementation of leadz and trailz for unsigned types.
+program memain
+  integer(kind=1) :: i1
+  integer(kind=2) :: i2
+  integer(kind=4) :: i4
+  integer(kind=8) :: i8
+  integer(kind=16) :: i16
+  unsigned(kind=1) :: u1
+  unsigned(kind=2) :: u2
+  unsigned(kind=4) :: u4
+  unsigned(kind=8) :: u8
+  unsigned(kind=16) :: u16
+
+  i1 = 12_1
+  u1 = 12u_1
+  if (leadz(i1) /= leadz(u1)) stop 1
+  if (leadz(u1) /= leadz(12u_1)) stop 1
+  if (trailz(i1) /= trailz(u1)) stop 3
+  if (trailz(u1) /= trailz(12u_1)) stop 3
+  if (popcnt(i1) /= popcnt(u1)) stop 5
+  if (popcnt(u1) /= popcnt(12u_1)) stop 5
+  if (poppar(i1) /= poppar(u1)) stop 7
+  if (poppar(u1) /= poppar(12u_1)) stop 7
+  i2 = 1348_2
+  u2 = 1348u_2
+  if (leadz(i2) /= leadz(u2)) stop 9
+  if (leadz(u2) /= leadz(1348u_2)) stop 9
+  if (trailz(i2) /= trailz(u2)) stop 11
+  if (trailz(u2) /= trailz(1348u_2)) stop 11
+  if (popcnt(i2) /= popcnt(u2)) stop 13
+  if (popcnt(u2) /= popcnt(1348u_2)) stop 13
+  if (poppar(i2) /= poppar(u2)) stop 15
+  if (poppar(u2) /= poppar(1348u_2)) stop 15
+  i4 = 172472_4
+  u4 = 172472u_4
+  if (leadz(i4) /= leadz(u4)) stop 17
+  if (leadz(u4) /= leadz(172472u_4)) stop 17
+  if (trailz(i4) /= trailz(u4)) stop 19
+  if (trailz(u4) /= trailz(172472u_4)) stop 19
+  if (popcnt(i4) /= popcnt(u4)) stop 21
+  if (popcnt(u4) /= popcnt(172472u_4)) stop 21
+  if (poppar(i4) /= poppar(u4)) stop 23
+  if (poppar(u4) /= poppar(172472u_4)) stop 23
+  i8 = 18744625769701376_8
+  u8 = 18744625769701376u_8
+  if (leadz(i8) /= leadz(u8)) stop 25
+  if (leadz(u8) /= leadz(18744625769701376u_8)) stop 25
+  if (trailz(i8) /= trailz(u8)) stop 27
+  if (trailz(u8) /= trailz(18744625769701376u_8)) stop 27
+  if (popcnt(i8) /= popcnt(u8)) stop 29
+  if (popcnt(u8) /= popcnt(18744625769701376u_8)) stop 29
+  if (poppar(i8) /= poppar(u8)) stop 31
+  if (poppar(u8) /= poppar(18744625769701376u_8)) stop 31
+  i16 = 1267650600228229401496703205392_16
+  u16 = 1267650600228229401496703205392u_16
+  if (leadz(i16) /= leadz(u16)) stop 33
+  if (leadz(u16) /= leadz(1267650600228229401496703205392u_16)) stop 33
+  if (trailz(i16) /= trailz(u16)) stop 35
+  if (trailz(u16) /= trailz(1267650600228229401496703205392u_16)) stop 35
+  if (popcnt(i16) /= popcnt(u16)) stop 37
+  if (popcnt(u16) /= popcnt(1267650600228229401496703205392u_16)) stop 37
+  if (poppar(i16) /= poppar(u16)) stop 39
+  if (poppar(u16) /= poppar(1267650600228229401496703205392u_16)) stop 39
+
+end program memain

Reply via email to