module Data.Repa.Array.Auto.Convert
(
readIntFromOffset, readIntFromOffset#
, readDouble, readDoubleFromBytes
, showDouble, showDoubleAsBytes
, showDoubleFixed, showDoubleFixedAsBytes)
where
import Data.Repa.Array.Auto.Base
import Data.Repa.Array.Generic.Convert
import System.IO.Unsafe
import Data.Word
import Data.Char
import GHC.Ptr
import GHC.Exts
import qualified Data.Repa.Array.Material.Auto as A
import qualified Data.Repa.Array.Material.Foreign as A
import qualified Data.Repa.Array.Meta as A
import qualified Data.Repa.Array.Generic as A
import qualified Data.Double.Conversion.ByteString as DC
import qualified Foreign.ForeignPtr as F
import qualified Foreign.Storable as F
import qualified Foreign.Marshal.Alloc as F
import qualified Foreign.Marshal.Utils as F
readIntFromOffset :: Array Char -> Int -> Maybe (Int, Int)
readIntFromOffset arr (I# ix0)
= case readIntFromOffset# arr ix0 of
(# 0#, _, _ #) -> Nothing
(# _ , n, ix #) -> Just (I# n, I# ix)
readIntFromOffset# :: Array Char -> Int# -> (# Int#, Int#, Int# #)
readIntFromOffset# !arr !ix0_
= start ix0
where
!ix0 = I# ix0_
!len = A.length arr
start !ix
| ix >= len = (# 0#, 0#, 0# #)
| otherwise = sign ix
sign !ix
| !s <- A.index arr 0
= case s of
'-' -> loop 1 (ix + 1) 0
'+' -> loop 2 (ix + 1) 0
_ -> loop 0 ix 0
loop !(neg :: Int) !ix !n
| ix >= len
= end neg ix n
| otherwise
= case ord $ A.index arr ix of
w | w >= 0x30 && w <= 0x039
-> loop neg (ix + 1) (n * 10 + (fromIntegral w 0x30))
| otherwise
-> end neg ix n
end !neg !ix !n
| ix == ix0
, neg == 0
= (# 0#, 0#, 0# #)
| ix == (ix0 + 1)
, neg /= 0
= (# 0#, 0#, 0# #)
| neg == 1
, I# n' <- negate n
, I# ix' <- ix
= (# 1#, n', ix' #)
| otherwise
, I# n' <- n
, I# ix' <- ix
= (# 1#, n', ix' #)
readDouble :: Array Char -> Double
readDouble vec
= readDoubleFromBytes
$ A.computeS A.A $ A.map (fromIntegral . ord) vec
readDoubleFromBytes :: Array Word8 -> Double
readDoubleFromBytes
(A.toForeignPtr . convert -> (start, len, fptr :: F.ForeignPtr Word8))
= unsafePerformIO
$ F.allocaBytes (len + 1) $ \pBuf ->
F.alloca $ \pRes ->
F.withForeignPtr fptr $ \pIn ->
do
F.copyBytes pBuf (pIn `plusPtr` start) (fromIntegral len)
F.pokeByteOff pBuf len (0 :: Word8)
let !d = strtod pBuf pRes
return d
foreign import ccall unsafe
strtod :: Ptr Word8 -> Ptr (Ptr Word8) -> Double
showDouble :: Double -> Array Char
showDouble !d
= A.computeS A.A $ A.map (chr . fromIntegral)
$ showDoubleAsBytes d
showDoubleAsBytes :: Double -> Array Word8
showDoubleAsBytes !d
= convert
$ A.fromByteString $ DC.toShortest d
showDoubleFixed :: Int -> Double -> Array Char
showDoubleFixed !prec !d
= A.computeS A.A $ A.map (chr . fromIntegral)
$ showDoubleFixedAsBytes prec d
showDoubleFixedAsBytes :: Int -> Double -> Array Word8
showDoubleFixedAsBytes !prec !d
= convert
$ A.fromByteString $ DC.toFixed prec d