Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing bit patterns

Status
Not open for further replies.

Bosse7

Programmer
Aug 3, 2015
19
FR
Hi

I have a simple question, is there a way to prevent the F90 compiler to check if the actual and formal arguments to a subroutine are different?

Before telling me that it is criminal to pass the wrong kind of variable please read my reason for the question.

The reason for this question is that I am trying to convert an old F77 package to F90 and one feature of this package was the use an integer workspace to store records with all kinds of variables, (integers, pointers to other records (which were integers), characters and reals). This F77 package was written long before typing become available with F90. The problem I have is that the package goes via subroutine calls to transfer such variables of different types into the integer wrkspace and in the old F77 compilers there were never any check that the actual and formal arguments were of the same type. Thus it was possible to write

integer iws(10000)
double precision xx,yy
...
call storr(iws(77),xx)
...

where the subroutine was

subroutine storr(x1,x2)
double precision x1,x2
x1=x2
return
end

which would transfer the bit pattern representing the real value of xx to (two) integer words, iws(77) and iws(78).
One could then get the value back by reversing the arguments

call storr(yy,iws(77))

and yy would have the value of xx

Apart from giving a very primitive way of having record structures a unique feature of this integer workspace was that one could easily write all that was stored in this integer workspce on an unformatted file and then read it back and continue working with the data stored.

It will be extremely difficult to do this with the datastructure I have in my current F90 code using typed records because the records can vary greatly in size and number.

So after this long introduction, is there a way to prevent the F90 compiler to generate an error if the actual and formal arguments to a subroutine are different?

Maybe I can ask a C programmer for help?

Bosse
 
If you use your F90 compiler to build F77 source, does it still check?
 
My new code is F90 and quite large, I wanted to add this F77 feature as I cannot figure out any other way to save/read results stored in the F90 typed records. I made some simple conversion of the F77 code (! instead of C etc) and compiled it with F90 and of course got error as it does not like that real and formal arguments are different.

I have no experience programming in C but I guess it would be possible to write STORR in C and compile and link it. I assume F77 has a separate runtime library so compiling it with F77 and try to link it with F90 would be more problematic.

 
Please read up on intrinsic function "transfer" and see if you can use it to achieve what you need.
 
I've just tried it on gfortran. Doesn't seem to be a problem
Code:
      subroutine converter(src, dst)
      double precision src, dst
      dst = src
      return
      end
Code:
program main
   implicit none
   external converter
   integer twowords(2)
   double precision dp
   ! Convert the number the F95 way
   dp = 20.5
   twowords = transfer(dp, twowords, 2)
   ! Set to some other value
   dp = 192.4
   ! Try the transfer routine
   call converter(twowords, dp)
   write(*,*) "Converted ", dp
   stop
end
To build

gfortran -Wall -g converter.for main.f95

Doesn't come up with any warnings but instead of using the converter, you could use the transfer function in F90/F95
 
Thanks a lot, I assumed there must be some simple way inside F08/F90 and I am grateful not to have to browse 300 pages of documentation to find it.

 
Unlike languages like C/C++, external functions do not need a prototype. They just need the word external, which makes it highly error prone but if you are doing bit twiddling, they're brilliant. It is like having a void* in C/C++.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top