Irk. If libm is busted when changing rounding modes, that puts a nasty twist on things. <div><br></div><div>I do agree that even if that hurdle is jumped, setting the local rounding mode will have to be part of every green thread context switch. But if libm is hosed that kinda makes adding that machinery a smudge pointless until there's a good story for that. <span></span><br><br>On Tuesday, May 5, 2015, Edward Kmett <<a href="mailto:ekmett@gmail.com">ekmett@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, May 5, 2015 at 7:22 AM, Scott Turner <span dir="ltr"><<a href="javascript:_e(%7B%7D,'cvml','2haskell@pkturner.org');" target="_blank">2haskell@pkturner.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span>On 2015-05-05 00:54, Levent Erkok wrote:<br>
> I can see at least two designs:<br>
><br>
> * One where the rounding mode goes with the operation: `fpAdd<br>
> RoundNearestTiesToEven 2.5 6.4`. This is the "cleanest" and the<br>
> functional solution, but could get quite verbose; and might be costly<br>
> if the implementation changes the rounding-mode at every issue.<br>
><br>
> * The other is where the operations simply assume the<br>
> RoundNearestTiesToEven, but we have lifted IO versions that can be<br>
> modified with a "with" like construct: `withRoundingMode<br>
> RoundTowardsPositive $ fpAddRM 2.5 6.4`. Note that `fpAddRM` (*not*<br>
> `fpAdd` as before) will have to return some sort of a monadic value<br>
> (probably in the IO monad) since it'll need to access the rounding<br>
> mode currently active.<br>
><br>
> Neither choice jumps out at me as the best one; and a hybrid might<br>
> also be possible. I'd love to hear any insight you gain regarding<br>
> rounding-modes during your experiment.<br>
<br>
</span>The monadic alternative is more readily extensible to handle IEEE 754's<br>
sticky flags: inexact, overflow, underflow, divide-by-zero, and invalid.<br></blockquote><div><br></div><div>This gets messier than you'd think. Keep in mind we switch contexts within our own green threads constantly on shared system threads / capabilities so the current rounding mode, sticky flags, etc. would become something you'd have to hold per Thread, and then change proactively as threads migrate between CPUs / capabilities, which we're basically completely unaware of right now.</div><div><br></div><div>This was what I learned when I tried my own hand at it and failed:</div><div><br></div><div><a href="http://hackage.haskell.org/package/rounding" target="_blank">http://hackage.haskell.org/package/rounding</a><br></div><div><br></div><div>There found I gave up, and moved setting the rounding mode into custom primitives themselves. But even then you find other problems! The libm versions of almost every combinator doesn't just give slightly wrong answers when you switch rounding modes, it gives _completely_ wrong answers when you switch rounding modes. cos basically starts looking like a random number generator. This is rather amusing given that libm is the library that specified how to change the damn rounding mode and fixing this fact it was blocked by Ulrich Drepper when I last looked.</div><div><br></div><div>Workarounds such as using <a href="http://lipforge.ens-lyon.fr/www/crlibm/" target="_blank">crlibm</a> exist, but isn't installed on most platforms and it would rather dramatically complicate the installation of ghc to incur the dependency.</div><div><br></div><div>This is why I've switched to using MPFR for anything with known rounding modes and just paying a pretty big performance tax for correctness. (That and I'm working to release a library that does exact real arithmetic using trees of nested linear fractional transformations -- assuming I can figure out how to keep performance high enough.)</div><div><br></div><div>-Edward</div><div><br></div><div><br></div><div><br></div><div>-Edward</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div>_______________________________________________<br>
Libraries mailing list<br>
<a href="javascript:_e(%7B%7D,'cvml','Libraries@haskell.org');" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</div></div></blockquote></div><br></div></div>
</blockquote></div>