Requested by Richard Townsend at http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/1e4130b9720e4f2a LEADZ and TRAILZ are rather common vendor extensions, e.g. supported by the Intel compiler (it also allows for logical arguments). LEADZ (I) Description. Number of leading zero bits. Argument. I shall be of type integer. Result Value. If all of the bits of I are zero, the result value is BIT SIZE (I). [...] TRAILZ (I) Description. Number of trailing zero bits. Argument. I shall be of type integer. The implementation should be relatively easy using the following built-ins, however, one presumably needs a special case for ZERO. BUILT_IN_CLZ* "These functions return the number of leading 0-bits in a, starting at the most significant bit position. If a is zero, the result is undefined." and BUILT_IN_CTZ* "These functions return the number of trailing 0-bits in a, starting at the least significant bit position. If a is zero, the result is undefined." (Description copied from http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html) For compile-time simplification, MPFR does not seem to provide a ready-to-use function; if one cooks up something oneself, one needs to check endian issues (though there might be none).
(In reply to comment #0) > For compile-time simplification, MPFR does not seem to provide a ready-to-use > function; if one cooks up something oneself, one needs to check endian issues > (though there might be none). I think you mean GMP because we're concerned with INTEGER types. See mpz_scan0 and mpz_scan1.
I'll see this weekend if I can take care of this.
Created attachment 16414 [details] draft patch
Posted patch: http://gcc.gnu.org/ml/fortran/2008-09/msg00443.html Approved, but check in will probably deferred until 4.5 opens.
.
Subject: Bug 37635 Author: steven Date: Thu Oct 2 18:51:12 2008 New Revision: 140837 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=140837 Log: testsuite * gfortran.fortran-torture/execute/intrinsic_leadz.f90: New test. * gfortran.fortran-torture/execute/intrinsic_trailz.f90: New test. fortran/ PR fortran/37635 * intrinsic.c (add_functions): Add LEADZ and TRAILZ as generics. * intrinsic.h (gfc_simplify_leadz, gfc_simplify_trailz): New protos. * gfortran.h <enum gfc_isym_id>: (GFC_ISYM_LEADZ, GFC_ISYM_TRAILZ): New. * f95-lang (gfc_init_builtin_functions): Add BUILT_IN_CLZ, BUILT_IN_CLZL, BUILT_IN_CLZLL, BUILT_IN_CTZ, BUILT_IN_CTZL, and BUILT_IN_CTZLL. * trans-intrinsic.c (gfc_conv_intrinsic_leadz, gfc_conv_intrinsic_trails): New code-generation functions for LEADZ and TRAILZ intrinsics. (gfc_conv_intrinsic_function): Use them * intrinsic.texi: Add documentation for LEADZ and TRAILZ. * simplify.c (gfc_simplify_leadz, gfc_simplify_trailz): New functions. Added: trunk/gcc/testsuite/gfortran.fortran-torture/execute/intrinsic_leadz.f90 trunk/gcc/testsuite/gfortran.fortran-torture/execute/intrinsic_trailz.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/f95-lang.c trunk/gcc/fortran/gfortran.h trunk/gcc/fortran/intrinsic.c trunk/gcc/fortran/intrinsic.h trunk/gcc/fortran/intrinsic.texi trunk/gcc/fortran/simplify.c trunk/gcc/fortran/trans-intrinsic.c trunk/gcc/testsuite/ChangeLog