From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public From: pmontgom@cwi.nl (Peter L. Montgomery) Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/20 Message-ID: X-Deja-AN: 281931831 Sender: news@cwi.nl (The Daily Dross) References: <62eqtu$2ua$1@goanna.cs.rmit.edu.au> Organization: CWI, Amsterdam Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec In article <62eqtu$2ua$1@goanna.cs.rmit.edu.au> ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) writes: >pmontgom@cwi.nl (Peter L. Montgomery) writes: >>Craig Burley writes: > >>>I don't see any rebuttal there. Care to explain what *low-level* >>>constructs C is offering that Fortran doesn't offer, aside from >>>pointers? > >> Nobody has been mentioning unsigned arithmetic. > >The machine I used as an undergraduate didn't _have_ unsigned arithmetic >in any form, and nobody ever missed it. I mean this quite literally: >the machine had _no_ instructions that could be used to do any kind of >unsigned arithmetic directly. Bitwise operations, yes. > >I wonder how many people would "need" unsigned arithmetic if C >didn't have it? The machine _I_ used as an undergraduate was the CDC 6400. Later, I used the CDC 7600 for 10 years as a professional. The CDC was ones' complement with 60-bit registers in the CPU. It lacked two's complement 60-bit addition/subtraction, but one could compute A + B mod 2^59 (say) by doing a ones' complement add and masking off the upper bit. It also lacked the shifts with zero-fill with Fortran and C now require (instead having circular shifts to the left and arithmetic shifts to the right). Just because one architecture lacks right and left shifts with zero fill is no reason for Fortran and C to omit such operations from the language. Lack of unsigned arithmetic made it hard to distinguish +0 from -0, such as when distinguishing whether 60 successive items are all TRUE from when they are all FALSE, in a bit array (I would test whether XOR(n, 1) .eq. 1 if I wanted to test n for +0). Code had kludges like this where unsigned arithmetic would improve the design. The CDC architecture also lacked double precision floating point arithmetic (except for addition/subtraction/multiplication of two single-precision operands), but its Fortran compiler generated inline code for many double precision operations. Today the Alpha architecture lacks integer division, even though the Fortran and C languages specified that operation long before the Alpha was designed. > >> C has conditional compilation, which Fortran does not. > > >This claim is a little extreme. It's not as if C conditional compilation > >were in any way integrated with the language, like SAIL or PL/I or Burroughs > >Algol or Lisp. > >Remember: #if sizeof (int) == 4 >is not legal C. Yes, there are severe limitations in C's conditional compilation. In the old CDC days, I used conditional compilation directives within UPDATE (a card-oriented text editor), but that had limitations too, like not allowing nested *IF - *ENDIF pairs. > > >> Fortran leads C in its large intrinsic function library. > >>C9X will finally let one write a MAX function which evaluates its > >>arguments once only and use the INLINE keyword to get good code, > > > >My understanding is that C9X is likely to contain > > f{min,max}{f,,l} > >functions anyway. That will be a step forward. > >> Neither language has constructs for the following operations > >>(although some implementations make them available): > > > >> . Trailing zero count of nonzero binary number > >> (equal to highest power of 2 dividing the number) > > > >This may be a low-level construct, but a lot of machines have no > >direct support for it. It may be programmed as easily in Fortran > >as in C. > > > >> . Truncated base-2 logarithm of nonnegative integer, > >> with LOG2(0) = -1. [Or return the number of bits required > >> to hold the value, which is one more than its logarithm.] > > > >This may be a low-level construct, but a lot of machines have no > >direct support for it. It may be programmed as easily in Fortran > >as in C. > > > >> . Population count of nonnegative integer. > > > >This may be a low-level construct, but a lot of machines have no > >direct support for it. It may be programmed as easily in Fortran > >as in C. Many of my number-theoretic codes need these functions. And they are frequently discussed on comp.arch, so others need them too. For example, when using the left-to-right method of binary exponentiation, I need to locate the leftmost nonzero bit in the exponent. Or I may need to loop through all the one-bits in word, where I anticipate only a few will be set (say 3 bits out of 32). The inner loop of the binary GCD algorithm needs to repeatedly divide a positive integer by 2 until the quotient is odd; that is, it needs to shift right by the trailing zero count of the operand. The population count arises when, for example, I need to take an inner product of two vectors over the field GF(2): this means taking the bitwise AND of the two vectors, getting the population count of this AND, and testing whether this count is odd or even. Yes, they can be programmed in both languages, but the straightforward methods have many branches (or require you to hard-wire the number of bits in the datum, such as if you try to take a population count by splitting a 32-bit integer into four eight-bit bytes, and using table look-ups). Branches are slow on modern processors, and we want to avoid them. Going back to the CDC 6400, which had population count, the others might be implemented as: Trailing zero count(N), where N <> 0: return population_count((N-1) & ~N) Base 2 logarithm(N) temp = (N >> 12) & 0xFFFFFFFFFFFF; (arithmetic shift) (constant is ~MASK(12)) if (temp == 0) then (N was less than 2**12) return 47 - normalize_count(N); else return 59 - normalize_count(temp); end if (normalize_count is the B-register output of a normalize instruction, used primarily to align the mantissa of a floating point operand). Putting these into a compiler may add a few hundred bytes to the compiler's executable, but will not penalize users who do not need the functions, except possibly to add reserved names to the language. Users whose codes benefit from the functions can use the functions (and buy machines from the manufacturers who best support them). Five years after the functions are in the standard, they will start appearing in benchmarks, and other architectures may start providing better support if doing so would improve benchmark speed. > > > > > >> . Upper half of (signed or unsigned) integer product. > > > > > >This may be a low-level construct, but some machines (sigh) have > > >no direct support for it. Ultra-SPARC is one of them. Perhaps if > >C _had_ had this, Ultra-SPARC would have, but then, it hasn't got > >a mod instruction either. > > > >LIA-2 has MULLO and MULHI functions, and C9x is taking LIA seriously, >so something of the sort may find its way into C. I wonder what they >will do on an UltraSPARC? A Fortran binding for LIA-2 will also have >to address this. Yes, the Ultra-SPARC lacks these for 64-bit operands. If I want high performance there, I'll need to use radix 2^32 in my multiple-precision codes (the Ultra-SPARC can multiply two 32-bit operands and get a 64-bit product). Meanwhile (using conditional compilation) let me use 64-bit operands on the MIPS and Alpha architectures, so I can take advantage of the 128-bit products available on those machines. For (slow) compatibility, the Ultra-SPARC can have a library routine which splits 64-bit operands into 32-bit operands and computes the product the hard way. Meanwhile the Ultra-SPARC can out-perform the MIPS machines and the pre-21264 Alpha's on codes using population count heavily. > > >> . Greatest common divisor (GCD) of two integers. > > >This too is in LIA, so may be in C9x. A Fortran binding for LIA-2 > >will also have to address it. That's good to hear. Few architectures support GCD directly, so it will be a library routine almost everywhere. Problems like writing a vectorizable GCD on the Cray will be solved once (by the compiler writer, if they consider GCD important), rather than separately by every user needing the function. >> . Floating point operations with explicit rounding, >> such as ROUND_DOWN(a - ROUND_ZERO(b*c)) >> to round the product b*c towards zero and >> round the difference a - b*c towards -infinity. > >Very nice, but there are pre-IEC559 machines that are still of >practical importance that have good Fortran systems, even C systems, >and cannot easily support these operations. Yes, it presumes IEEE arithmetic. If the machine does not support that, the application probably needs another algorithm. > >>All of these, except the GCD, have simple translations on the >>latest DEC Alpha. > >A nice machine. > >>They have mathematical definitions >>which do not refer to the target architecture, >>except that we need the number of bits per datum to >>define the upper half of an integer product. > > >Lisp has your truncated base-2 logarithm, population count, and > >greatest common divisor. It doesn't need MULLO/MULHI. > Perhaps I should learn Lisp. We've been comparing Fortran and C (in spite of the Subject: line of this article). -- Peter L. Montgomery pmontgom@cwi.nl San Rafael, California A mathematician whose age has doubled since he last drove an automobile.