cereal-ieee754 (empty) → 0.1
raw patch · 4 files changed
+190/−0 lines, 4 filesdep +arraydep +basedep +cerealsetup-changed
Dependencies added: array, base, cereal
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- cereal-ieee754.cabal +32/−0
- src/Data/Serialize/IEEE754.hs +126/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2011, Jacob Stanley++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 Jacob Stanley nor the names of other+ 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+OWNER 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cereal-ieee754.cabal view
@@ -0,0 +1,32 @@+name: cereal-ieee754+version: 0.1+synopsis: Floating point support for the 'cereal' serialization library+description: Provides functions for reading and writing IEEE 754 floating+ point numbers using the 'cereal' binary serialization library.+homepage: http://github.com/jystic/cereal-ieee754+license: BSD3+license-file: LICENSE+author: Jacob Stanley+maintainer: Jacob Stanley <jacob@stanley.io>+category: Data, Parsing+build-type: Simple+cabal-version: >=1.6++source-repository head+ type: git+ location: http://github.com/jystic/cereal-ieee754.git++library+ hs-source-dirs: src+ exposed-modules: Data.Serialize.IEEE754++ build-depends:+ array == 0.3.*,+ base == 4.*,+ cereal == 0.3.*++ ghc-options:+ -Wall+ -funbox-strict-fields+ -fno-warn-unused-do-bind+ -fwarn-tabs
+ src/Data/Serialize/IEEE754.hs view
@@ -0,0 +1,126 @@+{-# LANGUAGE FlexibleContexts #-}++-- | Functions for reading, writing and converting IEEE 754 floating+-- point numbers.+--+-- Conversions use 'STUArray' and the 'ST' monad to reinterpret bytes+-- and get /what we assume to be/ the IEEE 754 binary representation+-- of single and double precision floating point numbers.++module Data.Serialize.IEEE754 (++ -- * Big-endian reads+ getFloat32be+ , getFloat64be++ -- * Little-endian reads+ , getFloat32le+ , getFloat64le++ -- * Host-endian, unaligned reads+ , getFloat32host+ , getFloat64host++ -- * Little-endian writes+ , putFloat32le+ , putFloat64le++ -- * Big-endian writes+ , putFloat32be+ , putFloat64be++ -- * Host-endian, unaligned writes+ , putFloat32host+ , putFloat64host++ -- * Conversions+ , floatToWord+ , wordToFloat+ , doubleToWord+ , wordToDouble++ ) where++import Control.Applicative ((<$>))+import Data.Array.ST (newArray, castSTUArray, readArray, MArray, STUArray)+import Data.Serialize+import Data.Word (Word32, Word64)+import GHC.ST (runST, ST)++------------------------------------------------------------------------+-- Get++-- | Read a 32-bit float in little endian format+getFloat32le :: Get Float+getFloat32le = wordToFloat <$> getWord32le++-- | Read a 64-bit float in little endian format+getFloat64le :: Get Double+getFloat64le = wordToDouble <$> getWord64le++-- | Read a 32-bit float in big endian format+getFloat32be :: Get Float+getFloat32be = wordToFloat <$> getWord32be++-- | Read a 64-bit float in big endian format+getFloat64be :: Get Double+getFloat64be = wordToDouble <$> getWord64be++-- | Read a 32-bit float in native host order and host endianness+getFloat32host :: Get Float+getFloat32host = wordToFloat <$> getWord32host++-- | Read a 64-bit float in native host order and host endianness+getFloat64host :: Get Double+getFloat64host = wordToDouble <$> getWord64host++------------------------------------------------------------------------+-- Put++-- | Write a 32-bit float in little endian format+putFloat32le :: Putter Float+putFloat32le = putWord32le . floatToWord++-- | Write a 64-bit float in little endian format+putFloat64le :: Putter Double+putFloat64le = putWord64le . doubleToWord++-- | Write a 32-bit float in big endian format+putFloat32be :: Putter Float+putFloat32be = putWord32be . floatToWord++-- | Write a 64-bit float in big endian format+putFloat64be :: Putter Double+putFloat64be = putWord64be . doubleToWord++-- | Write a 32-bit float in native host order and host endianness+putFloat32host :: Putter Float+putFloat32host = putWord32host . floatToWord++-- | Write a 64-bit float in native host order and host endianness+putFloat64host :: Putter Double+putFloat64host = putWord64host . doubleToWord++------------------------------------------------------------------------+-- Conversions++-- | Interpret a 32-bit word as a 32-bit float.+wordToFloat :: Word32 -> Float+wordToFloat x = runST (cast x)++-- | Interpret a 32-bit float as a 32-bit word.+floatToWord :: Float -> Word32+floatToWord x = runST (cast x)++-- | Interpret a 64-bit word as a 64-bit float.+wordToDouble :: Word64 -> Double+wordToDouble x = runST (cast x)++-- | Interpret a 64-bit float as a 64-bit word.+doubleToWord :: Double -> Word64+doubleToWord x = runST (cast x)++{-# INLINE cast #-}+cast :: (MArray (STUArray s) a (ST s),+ MArray (STUArray s) b (ST s)) => a -> ST s b+cast x = newArray (0 :: Int, 0) x >>= castSTUArray >>= flip readArray 0