Last active
December 27, 2015 19:48
-
-
Save robstewart57/7379247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
module TwoDArray where | |
import Data.Int | |
import Data.Word | |
import LLVM.Core | |
import LLVM.Util.Loop | |
import Data.TypeLevel (D2,D4) | |
f :: CodeGenModule (Function (IO ())) | |
f = createNamedFunction ExternalLinkage "test" $ do | |
let xss = [[1,2,3,4],[5,6,7,8]] | |
height = 2 | |
width = 4 | |
let arr = Array $ map Array xss :: Array D2 (Array D4 Word8) | |
(ptr :: Value (Ptr (Array D2 (Array D4 Word8)))) <- malloc | |
store (valueOf arr) ptr | |
forLoop (valueOf 0) (valueOf (fromIntegral height) :: Value Int32) () $ \ h () -> do | |
forLoop (valueOf 0) (valueOf (fromIntegral width) :: Value Int32) () $ \ w () -> do | |
{- 1. load element at <h/w> position in 2D array arr -} | |
{- 2. add 2 to the value as y -} | |
{- 3. put value of y in to position <h/w> in 2D array arr -} | |
ret () | |
ret () | |
return () -- better still, return the pointer `ptr` | |
{- input 2D array | |
[ [1 , 2 , 3 , 4] | |
, [5 , 6 , 7 , 8] ] | |
-} | |
{- desired output 2D array | |
[ [3 , 4 , 5 , 6 ] | |
, [7 , 8 , 9 , 10] ] | |
-} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment