packages feed

feldspar-language-0.5.0.1: Feldspar/Core/Frontend/Bits.hs

--
-- Copyright (c) 2009-2011, ERICSSON AB
-- All rights reserved.
-- 
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
-- 
--     * Redistributions of source code must retain the above copyright notice, 
--       this list of conditions and the following disclaimer.
--     * Redistributions in binary form must reproduce the above copyright
--       notice, this list of conditions and the following disclaimer in the
--       documentation and/or other materials provided with the distribution.
--     * Neither the name of the ERICSSON AB nor the names of its contributors
--       may be used to endorse or promote products derived from this software
--       without specific prior written permission.
-- 
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
-- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

module Feldspar.Core.Frontend.Bits
where

import Prelude hiding (Integral(..))

import Data.Int
import Data.Word

import Language.Syntactic

import Feldspar.Range
import Feldspar.Core.Types
import Feldspar.Core.Constructs
import Feldspar.Core.Constructs.Bits
import Feldspar.Core.Frontend.Integral

import qualified Data.Bits as B

infixl 5 .<<.,.>>.
infixl 4 ⊕

-- TODO Make (Size a ~ Range a) a super-class constraint when going to newer GHC
class (Type a, B.Bits a, Integral a, Bounded a) => Bits a
  where
    -- Logical operations
    (.&.)         :: (Size a ~ Range a) => Data a -> Data a -> Data a
    (.&.)         = sugarSym BAnd
    (.|.)         :: (Size a ~ Range a) => Data a -> Data a -> Data a
    (.|.)         = sugarSym BOr
    xor           :: (Size a ~ Range a) => Data a -> Data a -> Data a
    xor           = sugarSym BXor
    complement    :: (Size a ~ Range a) => Data a -> Data a
    complement    = sugarSym Complement

    -- Bitwise operations
    bit           :: (Size a ~ Range a) => Data Index -> Data a
    bit           = sugarSym Bit
    setBit        :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    setBit        = sugarSym SetBit
    clearBit      :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    clearBit      = sugarSym ClearBit
    complementBit :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    complementBit = sugarSym ComplementBit
    testBit       :: (Size a ~ Range a) => Data a -> Data Index -> Data Bool
    testBit       = sugarSym TestBit

    -- Movement operations
    shiftLU       :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    shiftLU       = sugarSym ShiftLU
    shiftRU       :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    shiftRU       = sugarSym ShiftRU
    shiftL        :: (Size a ~ Range a) => Data a -> Data IntN -> Data a
    shiftL        = sugarSym ShiftL
    shiftR        :: (Size a ~ Range a) => Data a -> Data IntN -> Data a
    shiftR        = sugarSym ShiftR
    rotateLU      :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    rotateLU      = sugarSym RotateLU
    rotateRU      :: (Size a ~ Range a) => Data a -> Data Index -> Data a
    rotateRU      = sugarSym RotateRU
    rotateL       :: (Size a ~ Range a) => Data a -> Data IntN -> Data a
    rotateL       = sugarSym RotateL
    rotateR       :: (Size a ~ Range a) => Data a -> Data IntN -> Data a
    rotateR       = sugarSym RotateR
    reverseBits   :: (Size a ~ Range a) => Data a -> Data a
    reverseBits   = sugarSym ReverseBits

    bitScan       :: (Size a ~ Range a) => Data a -> Data Index
    bitScan       = sugarSym BitScan
    bitCount      :: (Size a ~ Range a) => Data a -> Data Index
    bitCount      = sugarSym BitCount

    bitSize       :: (Size a ~ Range a) => Data a -> Data Index
    bitSize       = sugarSym BitSize
    isSigned      :: (Size a ~ Range a) => Data a -> Data Bool
    isSigned      = sugarSym IsSigned

(⊕)    :: (Bits a, Size a ~ Range a) => Data a -> Data a -> Data a
(⊕)    =  xor
(.<<.) :: (Bits a, Size a ~ Range a) => Data a -> Data Index -> Data a
(.<<.) =  shiftLU
(.>>.) :: (Bits a, Size a ~ Range a) => Data a -> Data Index -> Data a
(.>>.) =  shiftRU

instance Bits Word8
instance Bits Word16
instance Bits Word32
instance Bits Word64
instance Bits WordN
instance Bits Int8
instance Bits Int16
instance Bits Int32
instance Bits Int64
instance Bits IntN