J3/04-220 Date: 2004-01-21 To: J3 From: fortran.com Subject: LOG2 intrinsic function Reference: Pub-112 This was submitted by Van Snyder van.snyder@jpl.nasa.gov =========================================================== Number: Title: LOG2 intrinsic function Submitted by: J3 Status: For Consideration References: Fortran 95, Section 13 Basic Functionality: Integer part of base-2 logarithm of (the absolute value of?) an integer. Rationale: One occasionally needs to know the integer part of the base-2 logarithm of an integer, that is, the largest exponent of a power of 2 that is less than or equal to that number. It's not difficult to do this with a loop that starts with 1 and doubles, but some processors have instructions that do it in a single cycle. Even without such an instruction, it's likely that an intrinsic function would be more efficient than even an optimized version of what one might write in a program. Estimated Impact: Trivial. Detailed Specification: Provide an intrinsic function to compute the integer part of the base-2 logarithm of an integer. Maybe specify that it computes the base-2 logarithm of the absolute value of its argument, since that's more likely to be useful than an error message. The usual desire for this function is for the integer part of the logarithm, not for a floating-point approximation to the logarithm. Providing the latter would just get in the way of what's really wanted, and would be less efficient. If one really wants the logarithm (not its integer part), one can get it about as efficiently as possible using log(real(I))/log(2.0) It ought to be generic for all kinds of integers, not just specific for default integers. It's possible that a processor could do a better job of the base-2 logarithm of a floating-point number in an intrinsic function than one can do with log(x)/log(2.0), but it's so rarely wanted that there's probably no point to providing it for floating point -- but that would be a trivial future extension if it's ever desired. History: Submitted as Pub-112