on master help sign in
Define some primitive SIMD operators
commit436bf95fd389d162cec0cdad78aac3aa0c58613f authorBenjamin Saunders <besaunde@sfu.ca> dateMon 17 Jun 2013, 18:59 (13 years ago) parenta83adf1
1 file changed+61−4
src/Idris/Primitives.hs+61−4
src/Idris/Primitives.hs+61−4
@@ -179,13 +179,20 @@ primitives =
179179 ++ concatMap vecOps [ITVec IT8 16, ITVec IT16 8, ITVec IT32 4, ITVec IT64 2]
180180
181181intOps :: IntTy -> [Prim]
182−intOps ity =
182+intOps ity = intCmps ity ++ intArith ity ++ intConv ity
183+
184+intCmps :: IntTy -> [Prim]
185+intCmps ity =
183186 [ iCmp ity "lt" (bCmp ity (<)) (LLt . ATInt) total
184187 , iCmp ity "lte" (bCmp ity (<=)) (LLe . ATInt) total
185188 , iCmp ity "eq" (bCmp ity (==)) (LEq . ATInt) total
186189 , iCmp ity "gte" (bCmp ity (>=)) (LGe . ATInt) total
187190 , iCmp ity "gt" (bCmp ity (>)) (LGt . ATInt) total
188− , iBinOp ity "add" (bitBin ity (+)) (LPlus . ATInt) total
191+ ]
192+
193+intArith :: IntTy -> [Prim]
194+intArith ity =
195+ [ iBinOp ity "add" (bitBin ity (+)) (LPlus . ATInt) total
189196 , iBinOp ity "sub" (bitBin ity (-)) (LMinus . ATInt) total
190197 , iBinOp ity "mul" (bitBin ity (*)) (LTimes . ATInt) total
191198 , iBinOp ity "udiv" (bitBin ity div) LUDiv partial
@@ -199,7 +206,11 @@ intOps ity =
199206 , iBinOp ity "or" (bitBin ity (.|.)) LOr total
200207 , iBinOp ity "xor" (bitBin ity (xor)) LXOr total
201208 , iUnOp ity "compl" (bUn ity complement) LCompl total
202− , Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [AType . ATInt $ ity] StrType) 1 intToStr
209+ ]
210+
211+intConv :: IntTy -> [Prim]
212+intConv ity =
213+ [ Prim (UN $ "prim__toStr" ++ intTyName ity) (ty [AType . ATInt $ ity] StrType) 1 intToStr
203214 (1, LIntStr ity) total
204215 , Prim (UN $ "prim__fromStr" ++ intTyName ity) (ty [StrType] (AType . ATInt $ ity)) 1 (strToInt ity)
205216 (1, LStrInt ity) total
@@ -209,12 +220,14 @@ intOps ity =
209220 (1, LFloatInt ity) total
210221 ]
211222
223+vecOps :: IntTy -> [Prim]
212224vecOps ity@(ITVec elem count) =
213225 [ Prim (UN $ "prim__mk" ++ intTyName ity)
214226 (ty (replicate count . AType . ATInt . ITFixed $ elem) (AType . ATInt $ ity))
215227 count (mkVecCon elem count) (count, LMkVec elem count) total
216− ]
228+ ] ++ intArith ity
217229
230+mkVecCon :: IntTy -> Int -> [Const] -> Const
218231mkVecCon ity count args = if length ints == count
219232 then Just (mkVec ity count ints)
220233 else error $ "Got: " ++ show args ++ " when expecting " ++ show count ++ " x " ++ show ity
@@ -285,6 +298,18 @@ bsrem (ITFixed IT32) [B32 x, B32 y]
285298bsrem (ITFixed IT64) [B64 x, B64 y]
286299 = Just $ B64 (fromIntegral (fromIntegral x `rem` fromIntegral y :: Int64))
287300bsrem ITNative [I x, I y] = Just $ I (x `rem` y)
301+bsrem (ITVec IT8 _) [B8V x, B8V y]
302+ = Just . B8V $
303+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int8))) x y
304+bsrem (ITVec IT16 _) [B16V x, B16V y]
305+ = Just . VConstant . B16V $
306+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int16))) x y
307+bsrem (ITVec IT32 _) [B32V x, B32V y]
308+ = Just . B32V $
309+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
310+bsrem (ITVec IT64 _) [B64V x, B64V y]
311+ = Just . B64V $
312+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `rem` fromIntegral d :: Int64))) x y
288313bsrem _ _ = Nothing
289314
290315bsdiv :: IntTy -> [Const] -> Maybe Const
@@ -297,6 +322,18 @@ bsdiv (ITFixed IT32) [B32 x, B32 y]
297322bsdiv (ITFixed IT64) [B64 x, B64 y]
298323 = Just $ B64 (fromIntegral (fromIntegral x `div` fromIntegral y :: Int64))
299324bsdiv ITNative [I x, I y] = Just $ I (x `div` y)
325+bsdiv (ITVec IT8 _) [B8V x, B8V y]
326+ = Just . B8V $
327+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int8))) x y
328+bsdiv (ITVec IT16 _) [B16V x, B16V y]
329+ = Just . B16V $
330+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int16))) x y
331+bsdiv (ITVec IT32 _) [B32V x, B32V y]
332+ = Just . B32V $
333+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
334+bsdiv (ITVec IT64 _) [B64V x, B64V y]
335+ = Just . B64V $
336+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `div` fromIntegral d :: Int64))) x y
300337bsdiv _ _ = Nothing
301338
302339bashr :: IntTy -> [Const] -> Maybe Const
@@ -309,6 +346,18 @@ bashr (ITFixed IT32) [B32 x, B32 y]
309346bashr (ITFixed IT64) [B64 x, B64 y]
310347 = Just $ B64 (fromIntegral (fromIntegral x `shiftR` fromIntegral y :: Int64))
311348bashr ITNative [I x, I y] = Just $ I (x `shiftR` y)
349+bashr (ITVec IT8 _) [B8V x, B8V y]
350+ = Just . B8V $
351+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int8))) x y
352+bashr (ITVec IT16 _) [B16V x, B16V y]
353+ = Just . B16V $
354+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int16))) x y
355+bashr (ITVec IT32 _) [B32V x, B32V y]
356+ = Just . B32V $
357+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
358+bashr (ITVec IT64 _) [B64V x, B64V y]
359+ = Just . B64V $
360+ V.zipWith (\n d -> (fromIntegral (fromIntegral n `shiftR` fromIntegral d :: Int64))) x y
312361bashr _ _ = Nothing
313362
314363bUn :: IntTy -> (forall a. Bits a => a -> a) -> [Const] -> Maybe Const
@@ -318,6 +367,10 @@ bUn (ITFixed IT32) op [B32 x] = Just $ B32 (op x)
318367bUn (ITFixed IT64) op [B64 x] = Just $ B64 (op x)
319368bUn ITBig op [BI x] = Just $ BI (op x)
320369bUn ITNative op [I x] = Just $ I (op x)
370+bUn (ITVec IT8 _) op [B8V x] = Just . B8V $ V.map op x
371+bUn (ITVec IT16 _) op [B16V x] = Just . B16V $ V.map op x
372+bUn (ITVec IT32 _) op [B32V x] = Just . B32V $ V.map op x
373+bUn (ITVec IT64 _) op [B64V x] = Just . B64V $ V.map op x
321374bUn _ _ _ = Nothing
322375
323376bitBin :: IntTy -> (forall a. (Bits a, Integral a) => a -> a -> a) -> [Const] -> Maybe Const
@@ -327,6 +380,10 @@ bitBin (ITFixed IT32) op [B32 x, B32 y] = Just $ B32 (op x y)
327380bitBin (ITFixed IT64) op [B64 x, B64 y] = Just $ B64 (op x y)
328381bitBin ITBig op [BI x, BI y] = Just $ BI (op x y)
329382bitBin ITNative op [I x, I y] = Just $ I (op x y)
383+bitBin (ITVec IT8 _) op [B8V x, B8V y] = Just . B8V $ V.zipWith op x y
384+bitBin (ITVec IT16 _) op [B16V x, B16V y] = Just . B16V $ V.zipWith op x y
385+bitBin (ITVec IT32 _) op [B32V x, B32V y] = Just . B32V $ V.zipWith op x y
386+bitBin (ITVec IT64 _) op [B64V x, B64V y] = Just . B64V $ V.zipWith op x y
330387bitBin _ _ _ = Nothing
331388
332389bCmp :: IntTy -> (forall a. Ord a => a -> a -> Bool) -> [Const] -> Maybe Const