Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83 (MC840302); site ttds.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!teddy!panda!talcott!harvard!seismo!mcvax!enea!ttds!sundman From: sundman@ttds.UUCP (Bo Sundman) Newsgroups: net.lang.f77 Subject: Problem with EQUIVALENCE Message-ID: <803@ttds.UUCP> Date: Tue, 22-Jan-85 14:50:33 EST Article-I.D.: ttds.803 Posted: Tue Jan 22 14:50:33 1985 Date-Received: Fri, 25-Jan-85 08:03:49 EST Reply-To: sundman@ttds.UUCP (Bo Sundman) Organization: The Royal Inst. of Techn., Stockholm Lines: 71 C The following piece of code is extracted from a numerical routine for which C I am not guilty. However, I am expected to make it work on f77 under C UNIX and I do not understand why it does not work. C The code works perfectly on a number of machines, including Fortran C on VAX/VMS, but f77 does not give the correct value on the same VAX. C C The intention of the code is, I believe, to store integer indices for matrix C elements in the only available workspace, a real array. I guess the reason C for the equivalence is to avoid conversion from real to integer and back C either to increase speed or to avoid rounding off errors. C IMPLICIT DOUBLE PRECISION (A-H,O-Z) DIMENSION W(10),NW(2) EQUIVALENCE (WW,NW(1)),(NQ,NW(2)) C MEMORY MAP OF WW, NW AND NQ C WORD: 1 2 C WW (..............) C NW (...1..)(...2..) C NQ (......) C C Storing an integer value into NQ is thus equivalent to storing the same C bit pattern into the least significant word of WW. C WRITE(*,*)'Test 1' NQ=10 WRITE(*,*)'Values should be 0 10:',NW W(5)=WW NQ=100 WRITE(*,*)'Values should be 0 100:',NW WW=1. WRITE(*,*)'Values should be 16512 0:',NW WW=W(5) WRITE(*,*)'Values should be 0 10:',NW,' <<< wrong' WRITE(*,*)'Test 2, WW=1.' WW=1. NQ=10 WRITE(*,*)'Values should be 16512 10:',NW W(5)=WW NQ=100 WRITE(*,*)'Values should be 16512 100:',NW WW=1. WRITE(*,*)'Values should be 16512 0:',NW WW=W(5) WRITE(*,*)'Values should be 16512 10:',NW END C C Output: C Test 1 Values should be 0 10: 0 10 Values should be 0 100: 0 100 Values should be 16512 0: 16512 0 Values should be 0 10: 0 0 <<< THIS IS WRONG Test 2, WW=1. Values should be 16512 10: 16512 10 Values should be 16512 100: 16512 100 Values should be 16512 0: 16512 0 Values should be 16512 10: 16512 10 My conclusion is that the run time system is "smart" and when it shall make the assignment WW=W(5) it discovers that the exponent of W(5) is zero, which normally means that W(5) IS zero, and it assigns zero to WW rather than copying the value of W(5) to WW. It may save some nanoseconds of execution time but is nontrivial in a case like this. -- Bo Sundman (..mcvax!enea!ttds!sundman) Thermo-Calc group, Division of Physical Metallurgy The Royal Institute of Technology Stockholm, Sweden