Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Created May 9, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerrorJack/291266721eac170752f175dae6b0e135 to your computer and use it in GitHub Desktop.
Save TerrorJack/291266721eac170752f175dae6b0e135 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC
-Wall -ddump-to-file -ddump-simpl -ddump-stg -ddump-cmm-raw -ddump-asm #-}
import Control.Monad
import Data.Foldable
import GHC.Exts
import GHC.Types
unI# :: Int -> Int#
unI# (I# x) = x
fill ::
MutableByteArray# RealWorld
-> (Int -> Int)
-> State# RealWorld
-> State# RealWorld
fill mba f s0 =
case getSizeofMutableByteArray# mba s0 of
(# s1, len #) -> w len 0# s1
where
w :: Int# -> Int# -> State# RealWorld -> State# RealWorld
w len i s =
case i ==# len of
0# -> w len (i +# 1#) (writeInt8Array# mba i (unI# (f (I# i))) s)
_ -> s
m :: Int -> IO (Int, Int)
m (I# len) =
IO
(\s0 ->
case newPinnedByteArray# len s0 of
(# s1, mba #) ->
case fill mba (+ 1) s1 of
s2 ->
case unsafeFreezeByteArray# mba s2 of
(# s3, ba #) ->
(# s3
, ( I# (indexInt8Array# ba 0#)
, I# (indexInt8Array# ba (len -# 1#)))#))
main :: IO ()
main = for_ [63, 511, 8191, 65535] $ m >=> print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment