From wdunlap at tibco.com  Thu Apr  8 18:01:18 2010
From: wdunlap at tibco.com (William Dunlap)
Date: Thu, 8 Apr 2010 09:01:18 -0700
Subject: [Rd] LOGICAL arguments in FORTRAN code
In-Reply-To: <alpine.LFD.2.00.1004081231530.21500@gannet.stats.ox.ac.uk>
References: <20100408183235.4185aa65@bossiaea>
	<alpine.LFD.2.00.1004081231530.21500@gannet.stats.ox.ac.uk>
Message-ID: <77EB52C6DD32BA4D87471DCD70C8D70002C3499C@NA-PA-VBE03.na.tibco.com>

> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Prof Brian Ripley
> Sent: Thursday, April 08, 2010 4:41 AM
> To: Berwin A Turlach
> Cc: r-devel
> Subject: Re: [Rd] LOGICAL arguments in FORTRAN code
> 
> On Thu, 8 Apr 2010, Berwin A Turlach wrote:
> 
> > G'day all,
> >
> > I just took over maintenance of the quadprog package from 
> Kurt Hornik
> > and noticed that one of the FORTRAN routines has an argument that is
> > declared to be a LOGICAL.  The R code that calls this routine (via
> > the .Fortran interface) passes the argument down wrapped in 
> a call to
> > as.logical().
> >
> > This was fine (and as documented) under S-Plus 3.4, for 
> which this code
> > was originally developed.  However, as far as I know, in R 
> objects of
> > storage mode logical were always supposed to be passed to FORTRAN
> > arguments of type INTEGER; and that is what the current "Writing R
> > extension manual states".
> >
> > Thus, given that the port of quadprog existed for quite some time, I
> > am wondering whether it is o.k. to pass R objects with storage mode
> > logical into FORTRAN code to arguments declared as LOGICAL? 
>  Or should
> > the FORTRAN code be corrected to declare the argument in question as
> > INTEGER?
> 
> The second to be safe.  This is not a question on the S-PLUS/R side 
> but on the Fortran side.  A Fortran compiler may or may not use the 
> same storage for integer and logical (and it may depend on compiler 
> flags, although not on the compilers I just checked, gfortran and 
> SunStudio f95).  S-PLUS ran on only a few platforms and with 
> specified 
> compilers.

I agree that avoiding LOGICAL's in arguments to Fortran
subroutines is a good idea.  Fortran compilers are prone
to do weird things when encoding LOGICAL values.  E.g.,
I vaguely recall that Apollo's compiler (c. 1990) encoded
.true. as 0 (all zeros) and .false. as -1 (all 1's) and
HP's compiler of the same era looked only at the 8th bit
from the left (no matter if you had a 1, 2, or 4 byte LOGICAL),
0 was .false. and 1 was .true.  S+'s .Fortran() took care of
these but you couldn't safely call Fortran code with such arguments
from C.   Avoid character arguments for similar reasons.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> 
> >
> > Cheers,
> >
> > 	Berwin
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] LOGICAL arguments in FORTRAN code
William Dunlap