JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  2005

COMP-FORTRAN-90 2005

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

LOGICAL redux

From:

James Giles <[log in to unmask]>

Reply-To:

Fortran 90 List <[log in to unmask]>

Date:

Fri, 1 Apr 2005 16:20:12 -0700

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (77 lines)

Point one.

   Most uses of logical expressions do not produce any canonical
   form at all.  Depending on hardware, the condition may be a bit
   embedded in a flags register, a property of a value (zero, NaN,
   etc.), or many other things.  The production of any canonical form
   should be avoided unless it's really needed.  Unlike foolish
   advice from others, I do not recommend doing that work unless
   necessary.  The claim that 0 vs. 1 requires no more work at
   all is only true of one hardware platform (then new Crays) and
   only when the conditions are generated by the hardware instructions
   involved.  In all other cases, creating 0/1 is time consuming and
   to be avoided when unnecessary.

Point two.

   Logical values are unordered.  The use of MAXLOC or MINLOC
   to search a logical array for the first instance of .true. or .false.
   (respectively) will produce the *wrong* answer if the condition
   being sought is not present - that's even assuming the unnecessary
   choice that .true. is greater than .false.  Searching for the max value
   in array of all 0's will return the position of the first 0 if there are
   no non-0 entries in the array.   If an array is all .false. and you are
   looking for the first .true., the correct answer is to return a recognizably
   out of bounds index (like SCAN or VERIFY do, which return zero).

   The additional detail that MAXLOC and MINLOC actually search the
   entire array even if the first element happens to be the answer is also
   ignored by the people that recommend it.  (Most ordered types have
   more than two possible values and MAXLOC and MINLOC are
   defined to accomodate that.)

   Searching for a match to the specific value you're interested in is a
   better choice than searching for a maximum or minimum.

Point three.

   The rarely needed canonical form for internally storing .true. or
   .false. should be designed to be efficient to use (even at the expense
   of an extra step in producing it).  This is because of the likelyhood
   that there will be more than one reference to a given stored logical
   value in the rest of the program.  Choosing a canonical form that
   requires further processing in the most common uses of such a flag
   is not best design.

   For example, suppose the condition being stored in LOGVAR is the
   sign of a numeric value (ie. X<0 is .true.).  To process this into a
   canonical form requires that you pick up the sign bit (or the sign
   flag in a flags register, do some masking, shifting etc.  Suppose the
   next use of LOGVAR is in an expression: (LOGVAR .and. Y<0).
   Here, all the processing required to produce a canonical form
   in the first case must be repeated in the second on the result of Y<0,
   *unless* your stored canonical form of .true. is all-ones.  In that
   latter case, you can skip any shifts to line up the second conditional
   with the canonical form of the first.  All-ones is already lined-up with
   all the positions of the machine word.  Indeed, there exist machines
   where the implementation of the scalar form of the MERGE intrinsic
   consists of loading the three operands and issuing a single scalar
   merge instruction - but only if the canonical form of .true. is all-ones.

   This may save only a little bit on each use of a stored logical value,
   but it also costs, at most, only a little bit (comparable amount) to
   produce.  At worst, it merely breaks even with the less desirable
   canonical forms.  If there *are* multiple uses of a stored logical
   it almost always wins.

Any language design that's not based on these three points is a bad
language design is respect to it handling of LOGICALs.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

December 2023
February 2023
November 2022
September 2022
February 2022
January 2022
June 2021
November 2020
September 2020
June 2020
May 2020
April 2020
December 2019
October 2019
September 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
April 2015
March 2015
January 2015
December 2014
November 2014
October 2014
August 2014
July 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager