Hex Flt Constants at CRI

Tim Peters uunet!ksr.com!tim
Mon Aug 30 23:04:55 PDT 1993


> Recently we started a project to implement hex floating-point constants
> in the Cray Research C compiler.  ...
> I would appreciate hearing any reactions to this deviation from the
> Floating-point C Extensions Document, and if anyone else has encountered
> similar problems.


Overall, it's less work and less disruptive to other tools if you don't
change the syntax of the language.  KSR stole Fortran 90's TRANSFER
intrinsic to get the same effect, much like Cray Pascal's VIEWING
statement.  E.g.,

/* return F, where x = F*2^n with 1 <= F < 2 */
double
sig( double x )
{
    unsigned long xbits = _transfer( x, 1UL );
    unsigned long mantissa = xbits & D_MANTISSA_MASK;
    return = _transfer( mantissa | 0x3ff0000000000000UL, 1.0 );
}

_transfer(x,y) is a KSR C builtin that returns the bits in x unaltered,
but as if they had the type of y (the _value_ of y is irrelevant); it's
obviously unportable, but as you note, so are hex constants for fp
numbers.

There's nothing here that can't be done (& equally unportably <wink>)
with union tricks; _transfer is easier to use and understand.  So far as
most C-based tools go (from cparen thru Emacs C mode to users' brains
...), _transfer looks like a vanilla function call, so they needn't be
altered.  From the compiler's viewpoint, it's an easy builtin to add
(even easier than hacking the grammar, eh?).  From a non-insane user's
viewpoint, unportable trickery is easy to find by searching for the
string "_transfer"; much easier than trying to find sick union tricks.

So, in all, I think _transfer solves the problem at hand (plus a host of
others) in a pleasant way.  My main gripe with inventing new syntax for
hex floats is that initialization is not the only time it can be helpful
(or essential!) to confuse C's type system -- it solves only part of the
problem.  C already knows how to spell hex and octal stuff; it just needs
a way to attach an unnatural type to the resulting bitstring.

testing-virtual-memory-by-viewing-doubles-as-pointers-ly y'rs  - tim

Tim Peters   timaksr.com
not speaking for Kendall Square Research Corp



More information about the Numeric-interest mailing list