Changes between Version 30 and Version 31 of PrimBool


Ignore:
Timestamp:
Aug 27, 2013 7:42:35 AM (8 months ago)
Author:
jstolarek
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PrimBool

    v30 v31  
    11= Implementing new primitive comparisons to allow branchless algorithms = 
    22 
    3 This page gathers the notes about implementing new primitive logical operations and thus resolving ticket #6135. 
     3This page gathers the notes about implementing new primitive logical operations and thus resolving ticket #6135. See [http://ghc.haskell.org/trac/ghc/wiki/NewPrimopsInGHC7.8 this page] for a instructions how to adjust your already existing code to work with new primops. 
    44 
    55== The problem == 
     
    8383== Solution == 
    8484 
    85 This problem was solved by modifying comparison primops to return unboxed unlifted `Int#` instead of `Bool`. Having `Int#` returned as a result of logical comparison will allow to use branchless bitwise logical operators instead of branching logical operators defined for `Bool` values. 
     85This problem was solved by modifying comparison primops to return unboxed unlifted `Int#` instead of `Bool` (which is lifted and thus is returned as a thunk that needs to be evaluated). Having `Int#` returned as a result of logical comparison will allow to use branchless bitwise logical operators instead of branching logical operators defined by Haskell. 
    8686 
    8787== Implementation details == 
     
    9292  * as a small side-task, four new logical bitwise primops have been implemented: `andI#`, `orI#`, `xorI#` and `negI#` (#7689). These operate on values of type `Int#`. Earlier we had only bitwise logical primops operating on values of type `Word#`. 
    9393  * names of the existing comparison primops were changed. Operators had `$` added before `#`, others had `I` added before the `#` (this is a mnemonic denoting that this primop returns and `Int#`). Examples: 
     94 
    9495{{{ 
    9596>=$#      :: Int#    -> Int#    -> Int# 
     
    99100ltFloatI# :: Float#  -> Float#  -> Int# 
    100101leAddrI#  :: Addr#   -> Addr#   -> Int# 
    101 sameMutableArrayI# :: MutableArray# s a -> MutableArray# s a -> Int# 
    102 }}} 
    103  
    104   * built in `GHC.Prim` modules was renamed to `GHC.Prim.BuiltIn`. In `ghc-prim` we added a module `GHC.Prim` which re-exports all definitions from `GHC.Prim.BuiltIn` but also adds wrappers for new comparison primops. These wrappers have names identical to removed primops and return a `Bool`. Examples: 
     102}}} 
     103 
     104  * a new module `GHC.PrimWrappers` was added to ghc-prim library. This module contains wrappers for comparison primops. These wrappers have names identical to removed primops and return a `Bool`. Examples: 
     105 
    105106{{{ 
    106107gtChar# :: Char# -> Char# -> Bool 
     
    121122leAddr# :: Addr# -> Addr# -> Bool 
    122123leAddr# a b = tagToEnum# (a `leAddrI#` b) 
    123  
    124 sameMutableArray# :: MutableArray# s a -> MutableArray# s a -> Int# 
    125 sameMutableArray# a b = tagToEnum# (a `sameMutableArrayI#` b) 
    126 }}}  
    127    Thanks to renaming of previously existing `GHC.Prim` module and adding wrappers in new `GHC.Prim` module '''the whole change of primops is backwards compatible'''. 
    128  
    129   * functions for comparing `Integer` type, implemented in `integer-gmp` and `integer-simple` libraries, received a similar treatment. Technically they are not primops, because they are implemented in Haskell (in case of `integer-gmp` also with FFI), but they pretend to be ones. There are six primops for comparing `Integer` values: 
     124}}} 
     125 
     126Thanks to these wrappers the change is almost backwards compatible. '''The only thing primop users need to change in their existing code to make it work again is adding import of GHC.!PrimWrappers module.''' 
     127 
     128  * functions for comparing `Integer` type, implemented in integer-gmp and integer-simple libraries, received a similar treatment. Technically they are not primops, because they are implemented in Haskell (in case of integer-gmp also with FFI), but they pretend to be ones. There are six primops for comparing `Integer` values: 
    130129{{{ 
    131130eqInteger#  :: Integer -> Integer -> Int# 
     
    136135geInteger#  :: Integer -> Integer -> Int# 
    137136}}} 
    138    Each of these functions has a wrapper that calls `tagToEnum#` and returns a `Bool`. These wrappers are: `eqInteger`, `neqInteger`, `leInteger`, `ltInteger`, `gtInteger` and `geInteger`. 
    139  
    140   * This change also required some small adjustments in `base` package. 
     137Each of these functions has a wrapper that calls `tagToEnum#` and returns a `Bool`. These wrappers are: `eqInteger`, `neqInteger`, `leInteger`, `ltInteger`, `gtInteger` and `geInteger`. 
     138 
     139  * Six primops are an exception to the rules above: `sameMutableArray#`, `sameMutableByteArray#`, `sameMutableArrayArray#`, `sameMutVar#`, `sameMVar#` and `sameTVar#`. Their names have remained the same as before and new wrappers created for them lack `#` at the end of their name. We made that decission because this naming feels more consistent and these primops are rarely used so we expect that they won't break a lot of existing code. 
     140 
     141  * Other libraries that were modified to work with the new primops are: base, ghc-prim and primitive. The only required modifications were imports of the GHC.!PrimWrappers module in modules that use the primops. 
    141142 
    142143== Eliminating branches using new primops ==