constant expressions & compilers 
Author Message
 constant expressions & compilers

I have a number of functions which are passed a real and return a real
in response.  These functions are called in turn by other functions
which do the same.. i.e (simplistically):

      real func1(constant)
      real constant

      func1=constant*2.0

      return
      end

      real func2(constant)
      real constant

      func2=func1(constant)*2.0

      return
      end

-----

if func2 is called with a constant, will most (any? many?) compilers
reduce the calls into constants and save the overhead of calling the
functions?  Specifically, the latest DEC fortran compilers for
DECstations, and the Sun compilers for sparcs.

Did that make sense?

- Andrew Houghton



Tue, 24 Dec 1996 02:12:37 GMT  
 constant expressions & compilers


|>
|>I have a number of functions which are passed a real and return a real
|>in response.  These functions are called in turn by other functions
|>which do the same.. i.e (simplistically):
[snip]
|>
|>if func2 is called with a constant, will most (any? many?) compilers
|>reduce the calls into constants and save the overhead of calling the
|>functions?  Specifically, the latest DEC fortran compilers for
|>DECstations, and the Sun compilers for sparcs.

DEC Fortran will not call the functions in this case.  Compilers which
don't do procedure inlining won't be able to do this analysis to determine
that the functions can be evaluated at compile-time.

You'd have a better chance of compile-time evaluation if you used
statement functions for func1 and func2; even compilers which don't do
procedure inlining could have a shot at that.


DEC Fortran Development           WWW:  http://www.digital.com/info/slionel.html
Digital Equipment Corporation    
110 Spit Brook Road, ZKO2-3/N30
Nashua, NH 03062-2698             "Free advice is worth every cent"




Tue, 24 Dec 1996 04:19:58 GMT  
 constant expressions & compilers


   I have a number of functions which are passed a real and return a real
   in response.  These functions are called in turn by other functions
   which do the same.. i.e (simplistically):

         real func1(constant)
         real constant

         func1=constant*2.0

         return
         end

         real func2(constant)
         real constant

         func2=func1(constant)*2.0

         return
         end

   if func2 is called with a constant, will most (any? many?) compilers
   reduce the calls into constants and save the overhead of calling the
   functions?  Specifically, the latest DEC fortran compilers for
   DECstations, and the Sun compilers for sparcs.

   Did that make sense?

If you co-locate (viz. they are in the same source file) the function
definitions and their callsites, and you compile with -O4 the code
will be totally inlined. With modest changes to make your example real

      program bork
      x = func1(2.0)
      print*,x
      end
      real function func1(constant)
      real constant
      func1=constant*2.0
      return
      end
      real function func2(constant)
      real constant
      func2=func1(constant)*2.0
      return
      end

and we compile with -O4 with the Sun f77 compiler (3.0) -O4 -S

...

! SUBROUTINE _MAIN_
!
! OFFSET    SOURCE LINE LABEL   INSTRUCTION

                        .global _MAIN_
                       _MAIN_:
/* 000000            */         save    %sp,-96,%sp
! FILE inline.f
!    1                !      program bork
!    2                !      
!    3                !      x = func1(2.0)
/* 0x000004        3 */         sethi   %hi(L_const_seg_900000101),%o0
/* 0x000008        0 */         add     %o0,%lo(L_const_seg_900000101),%o1
!    4                !      print*,x
/* 0x00000c        4 */         sethi   %hi(GPB.bork.x),%o0
/* 0x000010        0 */         add     %o0,%lo(GPB.bork.x),%l1
/* 0x000014        3 */         ld      [%o1],%f1
/* 0x000018          */         fmuls   %f1,%f1,%f0
/* 0x00001c        4 */         sethi   %hi(v.16),%l0
/* 0x000020          */         sethi   %hi(L_const_seg_900000101+8),%o1
/* 0x000024          */         add     %l0,%lo(v.16),%o0
/* 0x000028        0 */         add     %o1,%lo(L_const_seg_900000101+8),%l0
/* 0x00002c        4 */         call    ___s_wsle,1     ! Result = %g0
/* 0x000030        3 */         st      %f0,[%l1]
/* 0x000034        4 */         add     %l1,0,%o2
/* 0x000038          */         add     %l0,-4,%o0
/* 0x00003c          */         add     %l0,0,%o1
/* 0x000040          */         call    ___do_l_out,4   ! Result = %g0
/* 0x000044          */         or      %g0,4,%o3
/* 0x000048          */         call    ___e_wsle,0     ! Result = %g0  ! (tail call)
/* 0x00004c          */         restore %g0,%g0,%g0
/* 0x000050        0 */         unimp   65536

...junk followed by the functions (in case one wants to call them from
the de{*filter*} or etc.)

        .seg    "text"
/* 000000            */         save    %sp,-112,%sp
!    6                !      real function func1(constant)
!    7                !      real constant
!    8                !
!    9                !      func1=constant*2.0
/* 0x000004        9 */         sethi   %hi(L_const_seg_900000202),%o0
/* 0x000008          */         ld      [%i0],%f3
/* 0x00000c          */         ld      [%o0+%lo(L_const_seg_900000202)],%f2
/* 0x000010          */         fmuls   %f3,%f2,%f0
!   11                !      return
/* 0x000014       11 */         ret
/* 0x000018          */         restore %g0,%g0,%g0
/* 0x00001c        0 */         unimp   65536

        .seg    "text"
...and etc.
--
----------------------------------------------------------------

SunPro 2550 Garcia MTV 12-40             | (415 336 2648) fax 964 0946
Mountain View, CA 94043  <speaking for myself, not Sun*> Copyright 1994



Tue, 24 Dec 1996 06:38:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PGF90-S-0087-Non-constant expression where constant expression required

2. PGF90-S-0087-Non-constant expression where constant expression required

3. Non-constant expression where constant expression required.

4. Salford compiler: Assigning a value to a constant expression is invalid

5. optimizing evaluation of constant expressions

6. constant expressions, parameters, `define

7. constant expressions, parameters, `define

8. '87 LRM - constant expression on port

9. Lint on a non-constant expression - harmful?

10. constants (literal, parameter, expression) in F77

11. complex constants and expression syntax extension a la Mathematica

12. Constant expressions and mathematical functions

 

 
Powered by phpBB® Forum Software