From cdc1ef83ac849c539e5ef25f1a99b0784d46335f Mon Sep 17 00:00:00 2001
From: tobi <tobi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Thu, 2 Dec 2004 19:39:15 +0000
Subject: [PATCH] libgfortran/ PR fortran/18710 * io/transfer.c
 (unformatted_read, unformatted_write): width of a COMPLEX is twice its kind.

gcc/testsuite/
PR fortran/18170
* gfortran.dg/direct_io_3.f90: New test.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@91656 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/testsuite/ChangeLog                   |  5 +++++
 gcc/testsuite/gfortran.dg/direct_io_3.f90 | 20 ++++++++++++++++++++
 libgfortran/ChangeLog                     |  6 ++++++
 libgfortran/io/transfer.c                 | 18 +++++++++++++++---
 4 files changed, 46 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/direct_io_3.f90

diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 42c4c5b2cad5..7cb1ad6d59ba 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2004-12-02  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
+
+	PR fortran/18170
+	* gfortran.dg/direct_io_3.f90: New test.
+
 2004-12-02  Nathan Sidwell  <nathan@codesourcery.com>
 
 	PR 18758
diff --git a/gcc/testsuite/gfortran.dg/direct_io_3.f90 b/gcc/testsuite/gfortran.dg/direct_io_3.f90
new file mode 100644
index 000000000000..8603a833e12e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/direct_io_3.f90
@@ -0,0 +1,20 @@
+! { dg-do run }
+! PR 18710 : We used to not read and write the imaginary part of 
+! complex numbers
+       COMPLEX C, D
+       DOUBLE COMPLEX E, F
+
+       OPEN(UNIT=9,FILE='PR18710',ACCESS='DIRECT',RECL=132)
+
+       C = (120.0,240.0)
+       WRITE(9,REC=1)C
+       READ(9,REC=1)D
+       if (c /= d) call abort()
+
+       E = (120.0,240.0)
+       WRITE(9,REC=1)E
+       READ(9,REC=1)F
+       if (E /= F) call abort()
+
+       CLOSE(UNIT=9,STATUS='DELETE')
+       END
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 2ee79b2b75dc..4dfb33553411 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,9 @@
+2004-12-02  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
+
+	PR fortran/18710
+	* io/transfer.c (unformatted_read, unformatted_write): width of
+	a COMPLEX is twice its kind.
+
 2004-12-02  Richard Sandiford  <rsandifo@redhat.com>
 
 	* configure.ac: Use TL_AC_GCC_VERSION to set gcc_version.
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index ceff76fc35c5..c27f0f7bb83d 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -271,6 +271,13 @@ unformatted_read (bt type, void *dest, int length)
 {
   void *source;
   int w;
+
+  /* Transfer functions get passed the kind of the entity, so we have
+     to fix this for COMPLEX data which are twice the size of their
+     kind.  */
+  if (type == BT_COMPLEX)
+    length *= 2;
+
   w = length;
   source = read_block (&w);
 
@@ -288,9 +295,14 @@ static void
 unformatted_write (bt type, void *source, int length)
 {
   void *dest;
-   dest = write_block (length);
-   if (dest != NULL)
-     memcpy (dest, source, length);
+
+  /* Correction for kind vs. length as in unformatted_read.  */
+  if (type == BT_COMPLEX)
+    length *= 2;
+
+  dest = write_block (length);
+  if (dest != NULL)
+    memcpy (dest, source, length);
 }
 
 
-- 
GitLab