Bug 37635 - Fortran 2008: Support LEADZ / TRAILZ
Summary: Fortran 2008: Support LEADZ / TRAILZ
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Steven Bosscher
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2008-09-24 08:15 UTC by Tobias Burnus
Modified: 2008-10-02 18:52 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-09-24 22:28:12


Attachments
draft patch (3.96 KB, patch)
2008-09-27 14:09 UTC, Steven Bosscher
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2008-09-24 08:15:25 UTC
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).
Comment 1 kargls 2008-09-24 16:19:49 UTC
(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.

Comment 2 Steven Bosscher 2008-09-24 22:28:12 UTC
I'll see this weekend if I can take care of this.
Comment 3 Steven Bosscher 2008-09-27 14:09:43 UTC
Created attachment 16414 [details]
draft patch
Comment 4 Tobias Burnus 2008-09-28 18:03:40 UTC
Posted patch:
http://gcc.gnu.org/ml/fortran/2008-09/msg00443.html

Approved, but check in will probably deferred until 4.5 opens.
Comment 5 Steven Bosscher 2008-10-02 18:52:28 UTC
.
Comment 6 Steven Bosscher 2008-10-02 18:52:36 UTC
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