Changes between Version 7 and Version 8 of ReplacingGMPNotes/TheCurrentGMPImplementation


Ignore:
Timestamp:
Jan 6, 2007 7:31:15 PM (7 years ago)
Author:
p_tanski
Comment:

Add note on encodeFloat in GHC.Num.lhs(RealFloat? Double)

Legend:

Unmodified
Added
Removed
Modified
  • ReplacingGMPNotes/TheCurrentGMPImplementation

    v7 v8  
    6565 * there is no specification of rounding mode--this operation relies on whatever hardware rounding may take effect; and, 
    6666 * certain floating point hardware exceptions (traps) for overflow or underflow may be triggered (though this is not generally the case; note: on x86 machines floating point exceptions may not be triggered but the resulting float may denormalized). 
    67 Nowhere in this function is there a check for whether the GMP number (the `const mp_limb_t *arr`) may be greater than the size of a double and either warn the user (possibly with an `ArithException` or round the resulting double toward zero.  Compare the problem section in `__encodeDouble` to the exponent check in the internal GMP function, `mpn_get_d`, that avoids overflow by comparing the relative magnitude of the GMP number with the maximum magnitude of the double: 
     67Nowhere in this function is there a check for whether the GMP number (the `const mp_limb_t *arr`) may be greater than the size of a double and either warn the user (possibly with an `ArithException` or round the resulting double toward zero.   
     68 
     69Compare the problem section in `__encodeDouble` to the exponent check in the internal GMP function, `mpn_get_d`, that avoids overflow by comparing the relative magnitude of the GMP number with the maximum magnitude of the double: 
    6870{{{ 
    6971   /* note: exp is a long int, given as an argument to min_get_d as 0L --PDT */ 
     
    8587    } 
    8688}}} 
    87 (If you want to see the full `mpn_get_d` function, it is in the file `[toplevel gmp source]/mpn/generic/get_d.c` .)  A replacement library for GMP might use the GMP strategies of including a special bitwise conversion (with appropriate masks) and a hardware-based version.  An unconventional solution might perform the rounding manually (but with relatively portable predictability) using interval arithmetic. 
     89(If you want to see the full `mpn_get_d` function, it is in the file `[toplevel gmp source]/mpn/generic/get_d.c` .)   
     90 
     91There is no check in the Haskell code ''using'' `__encodeFloat` or `__encodeDouble`, in [[GhcFile(libraries/base/GHC/Float.lhs)]].  For example, the `encodeFloat` Haskell function under class `RealFloat` uses `__encodeDouble` directly: 
     92{{{ 
     93#!html 
     94<pre><font color=Orange>encodeFloat</font> <font color=Blue>(</font><font color=Green>J</font><font color=Blue>#</font> <font color=Black>s</font><font color=Blue>#</font> <font color=Black>d</font><font color=Blue>#</font><font color=Blue>)</font> <font color=Black>e</font> <font color=Blue>=</font> <font color=Orange>encodeDouble</font><font color=Blue>#</font> <font color=Black>s</font><font color=Blue>#</font> <font color=Black>d</font><font color=Blue>#</font> <font color=Black>e</font></pre> 
     95}}} 
     96This is not the case with the safer `fromRat` function which ensures the Integer falls in the range of the mantissa (see the source code). 
     97 
     98A replacement library for GMP might use the GMP strategies of including a special bitwise conversion (with appropriate masks) and a hardware-based version.  An unconventional solution might perform the rounding manually (but with relatively portable predictability) using interval arithmetic.   
    8899 
    89100As for printing GMP numbers, [[GhcFile(libraries/base/GHC/Num.lhs)]] defines a special internal function `jtos` to handle `showsPrec` and `showList` for `Integer` as an instance of the class `Show`.  The `jtos` function uses the primitive GMP-based function `quotRemInteger` and performs many conversions from Integer to Int.  This is not as efficient as the internal GMP functions, especially for large numbers, because each conversion allocates extra storage for GMP as noted in [wiki:ReplacingGMPNotes#OptimisationOpportunities Replacing GMP -- Optimisation Opportunities].