Christopher Howard | 11 Apr 07:38 2013

exp implementation

Hi. For my own learning, I wanted to see how the exp function is
implemented in GHC. I have GHC 7.4.1 source code open, but I'm having
trouble figuring out which file the actual function definition is in. I see

 expFloat    (F# x) = F# (expFloat# x)

in libraries/base/GHC/Float.lhs.

--

-- 
frigidcode.com

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Brandon Allbery | 11 Apr 16:37 2013
Picon

Re: exp implementation

On Thu, Apr 11, 2013 at 1:38 AM, Christopher Howard <christopher.howard <at> frigidcode.com> wrote:
Hi. For my own learning, I wanted to see how the exp function is
implemented in GHC. I have GHC 7.4.1 source code open, but I'm having
trouble figuring out which file the actual function definition is in. I see

 expFloat    (F# x) = F# (expFloat# x)

expFloat# is likely a primop; good luck.... Primops aka primitive operations are generally implemented in the compiler backend as assembly language or Cmm code. Untangling that part of ghc makes my head swim. >.>

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b <at> gmail.com                                  ballbery <at> sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Christopher Howard | 11 Apr 17:12 2013

Re: exp implementation

On 04/11/2013 06:37 AM, Brandon Allbery wrote:
> On Thu, Apr 11, 2013 at 1:38 AM, Christopher Howard
> <christopher.howard <at> frigidcode.com
> <mailto:christopher.howard <at> frigidcode.com>> wrote:
> 
>     Hi. For my own learning, I wanted to see how the exp function is
>     implemented in GHC. I have GHC 7.4.1 source code open, but I'm having
>     trouble figuring out which file the actual function definition is
>     in. I see
> 
>      expFloat    (F# x) = F# (expFloat# x)
> 
> 
> expFloat# is likely a primop; good luck.... Primops aka primitive
> operations are generally implemented in the compiler backend as assembly
> language or Cmm code. Untangling that part of ghc makes my head swim. >.>
> 
> -- 
> brandon s allbery kf8nh                               sine nomine associates
> allbery.b <at> gmail.com <mailto:allbery.b <at> gmail.com>                        
>          ballbery <at> sinenomine.net <mailto:ballbery <at> sinenomine.net>
> unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

I traced it down some more: I think it eventually goes into the
compiler/nativeGen section where it is translated into the platform's
native version of the function. On my platform, I think this is the expf
function from math.h. (See EXP(3)).

I find that to be interesting, because it means you could change the
output of your programs by altering your standard library. But I guess
there are a lot of things you could change by altering your standard
library!

--

-- 
frigidcode.com

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Christopher Howard | 11 Apr 17:30 2013

Re: exp implementation

On 04/11/2013 07:12 AM, Christopher Howard wrote:
> On 04/11/2013 06:37 AM, Brandon Allbery wrote:
> 
> I traced it down some more: I think it eventually goes into the
> compiler/nativeGen section where it is translated into the platform's
> native version of the function. On my platform, I think this is the expf
> function from math.h. (See EXP(3)).
> 
> I find that to be interesting, because it means you could change the
> output of your programs by altering your standard library. But I guess
> there are a lot of things you could change by altering your standard
> library!
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

In glibc, it eventual comes down to this approximation, I think (from
./sysdeps/ieee754/flt-32/e_expf.c):

code:
--------
      /* Compute ex2 = 2^n e^(t/512+delta[t]).  */
      ex2_u.d = __exp_atable[tval+177];
      ex2_u.ieee.exponent += (int) n;

      /* Approximate e^(dx+delta) - 1, using a second-degree polynomial,
	 with maximum error in [-2^-10-2^-28,2^-10+2^-28]
	 less than 5e-11.  */
      x22 = (0.5000000496709180453 * dx + 1.0000001192102037084) * dx +
delta;

/* ... snip ... */

      result = x22 * ex2_u.d + ex2_u.d;
--------

--

-- 
frigidcode.com

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane