Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
binary
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Glasgow Haskell Compiler
Packages
binary
Commits
f3ec4e3e
Commit
f3ec4e3e
authored
9 years ago
by
DanielDiaz
Browse files
Options
Downloads
Patches
Plain Diff
Implementation of get/put functions for floats/doubles.
parent
55d5a94c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
binary.cabal
+5
-3
5 additions, 3 deletions
binary.cabal
src/Data/Binary/Get.hs
+44
-0
44 additions, 0 deletions
src/Data/Binary/Get.hs
src/Data/Binary/Put.hs
+42
-1
42 additions, 1 deletion
src/Data/Binary/Put.hs
tests/QC.hs
+27
-0
27 additions, 0 deletions
tests/QC.hs
with
118 additions
and
4 deletions
binary.cabal
+
5
−
3
View file @
f3ec4e3e
...
...
@@ -31,7 +31,7 @@ source-repository head
location: git://github.com/kolmodin/binary.git
library
build-depends: base >= 4.5.0.0 && < 5, bytestring >= 0.10.2, containers, array
build-depends: base >= 4.5.0.0 && < 5, bytestring >= 0.10.2, containers, array
, reinterpret-cast
hs-source-dirs: src
exposed-modules: Data.Binary,
Data.Binary.Put,
...
...
@@ -69,7 +69,8 @@ test-suite qc
random>=1.0.1.0,
test-framework,
test-framework-quickcheck2 >= 0.3,
QuickCheck>=2.8
QuickCheck>=2.8,
reinterpret-cast
-- build dependencies from using binary source rather than depending on the library
build-depends: array, containers
...
...
@@ -89,7 +90,8 @@ test-suite read-write-file
Cabal,
directory,
filepath,
HUnit
HUnit,
reinterpret-cast
-- build dependencies from using binary source rather than depending on the library
build-depends: array, containers
...
...
This diff is collapsed.
Click to expand it.
src/Data/Binary/Get.hs
+
44
−
0
View file @
f3ec4e3e
...
...
@@ -205,6 +205,14 @@ module Data.Binary.Get (
,
getInt32host
,
getInt64host
-- ** Decoding Floats/Doubles
,
getFloatbe
,
getFloatle
,
getFloathost
,
getDoublebe
,
getDoublele
,
getDoublehost
-- * Deprecated functions
,
runGetState
-- DEPRECATED
,
remaining
-- DEPRECATED
...
...
@@ -229,6 +237,9 @@ import GHC.Base
import
GHC.Word
#
endif
-- needed for casting words to float/double
import
Data.ReinterpretCast
(
wordToFloat
,
wordToDouble
)
-- $lazyinterface
-- The lazy interface consumes a single lazy 'L.ByteString'. It's the easiest
-- interface to get started with, but it doesn't support interleaving I\/O and
...
...
@@ -608,6 +619,39 @@ getInt64host = getPtr (sizeOf (undefined :: Int64))
{-# INLINE getInt64host #-}
------------------------------------------------------------------------
-- Double/Float reads
-- | Read a 'Float' in big endian format.
getFloatbe
::
Get
Float
getFloatbe
=
wordToFloat
<$>
getWord32be
{-# INLINE getFloatbe #-}
-- | Read a 'Float' in little endian format.
getFloatle
::
Get
Float
getFloatle
=
wordToFloat
<$>
getWord32le
{-# INLINE getFloatle #-}
-- | Read a 'Float' in native host order and host endianess.
getFloathost
::
Get
Float
getFloathost
=
wordToFloat
<$>
getWord32host
{-# INLINE getFloathost #-}
-- | Read a 'Double' in big endian format.
getDoublebe
::
Get
Double
getDoublebe
=
wordToDouble
<$>
getWord64be
{-# INLINE getDoublebe #-}
-- | Read a 'Double' in little endian format.
getDoublele
::
Get
Double
getDoublele
=
wordToDouble
<$>
getWord64le
{-# INLINE getDoublele #-}
-- | Read a 'Double' in native host order and host endianess.
getDoublehost
::
Get
Double
getDoublehost
=
wordToDouble
<$>
getWord64host
{-# INLINE getDoublehost #-}
------------------------------------------------------------------------
-- Unchecked shifts
...
...
This diff is collapsed.
Click to expand it.
src/Data/Binary/Put.hs
+
42
−
1
View file @
f3ec4e3e
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Safe #-}
-- {-# LANGUAGE Safe #-}
{-# LANGUAGE Trustworthy #-}
#
if
MIN_VERSION_base
(
4
,
9
,
0
)
#
define
HAS_SEMIGROUP
...
...
@@ -49,6 +50,8 @@ module Data.Binary.Put (
,
putInt16be
,
putInt32be
,
putInt64be
,
putFloatbe
,
putDoublebe
-- * Little-endian primitives
,
putWord16le
...
...
@@ -57,6 +60,8 @@ module Data.Binary.Put (
,
putInt16le
,
putInt32le
,
putInt64le
,
putFloatle
,
putDoublele
-- * Host-endian, unaligned writes
,
putWordhost
-- :: Word -> Put
...
...
@@ -67,6 +72,8 @@ module Data.Binary.Put (
,
putInt16host
-- :: Int16 -> Put
,
putInt32host
-- :: Int32 -> Put
,
putInt64host
-- :: Int64 -> Put
,
putFloathost
,
putDoublehost
-- * Unicode
,
putCharUtf8
...
...
@@ -93,6 +100,8 @@ import Data.Semigroup
import
Control.Applicative
import
Prelude
-- Silence AMP warning.
-- needed for casting Floats/Doubles to words.
import
Data.ReinterpretCast
(
floatToWord
,
doubleToWord
)
------------------------------------------------------------------------
...
...
@@ -346,6 +355,38 @@ putInt64host :: Int64 -> Put
putInt64host
=
tell
.
B
.
putInt64host
{-# INLINE putInt64host #-}
------------------------------------------------------------------------
-- Floats/Doubles
-- | Write a 'Float' in big endian format.
putFloatbe
::
Float
->
Put
putFloatbe
=
putWord32be
.
floatToWord
{-# INLINE putFloatbe #-}
-- | Write a 'Float' in little endian format.
putFloatle
::
Float
->
Put
putFloatle
=
putWord32le
.
floatToWord
{-# INLINE putFloatle #-}
-- | Write a 'Float' in native host order and host endianness.
putFloathost
::
Float
->
Put
putFloathost
=
putWord32host
.
floatToWord
{-# INLINE putFloathost #-}
-- | Write a 'Double' in big endian format.
putDoublebe
::
Double
->
Put
putDoublebe
=
putWord64be
.
doubleToWord
{-# INLINE putDoublebe #-}
-- | Write a 'Double' in little endian format.
putDoublele
::
Double
->
Put
putDoublele
=
putWord64le
.
doubleToWord
{-# INLINE putDoublele #-}
-- | Write a 'Double' in native host order and host endianness.
putDoublehost
::
Double
->
Put
putDoublehost
=
putWord64host
.
doubleToWord
{-# INLINE putDoublehost #-}
------------------------------------------------------------------------
-- Unicode
...
...
This diff is collapsed.
Click to expand it.
tests/QC.hs
+
27
−
0
View file @
f3ec4e3e
...
...
@@ -131,6 +131,26 @@ prop_Int64host = roundTripWith putInt64host getInt64host
prop_Inthost
::
Int
->
Property
prop_Inthost
=
roundTripWith
putInthost
getInthost
-- Floats and Doubles
prop_Floatbe
::
Float
->
Property
prop_Floatbe
=
roundTripWith
putFloatbe
getFloatbe
prop_Floatle
::
Float
->
Property
prop_Floatle
=
roundTripWith
putFloatle
getFloatle
prop_Floathost
::
Float
->
Property
prop_Floathost
=
roundTripWith
putFloathost
getFloathost
prop_Doublebe
::
Double
->
Property
prop_Doublebe
=
roundTripWith
putDoublebe
getDoublebe
prop_Doublele
::
Double
->
Property
prop_Doublele
=
roundTripWith
putDoublele
getDoublele
prop_Doublehost
::
Double
->
Property
prop_Doublehost
=
roundTripWith
putDoublehost
getDoublehost
-- done, partial and fail
...
...
@@ -552,6 +572,13 @@ tests =
,
testProperty
"Int64le"
(
p
prop_Int64le
)
,
testProperty
"Int64host"
(
p
prop_Int64host
)
,
testProperty
"Inthost"
(
p
prop_Inthost
)
-- Float/Double
,
testProperty
"Floatbe"
(
p
prop_Floatbe
)
,
testProperty
"Floatle"
(
p
prop_Floatle
)
,
testProperty
"Floathost"
(
p
prop_Floathost
)
,
testProperty
"Doublebe"
(
p
prop_Doublebe
)
,
testProperty
"Doublele"
(
p
prop_Doublele
)
,
testProperty
"Doublehost"
(
p
prop_Doublehost
)
]
,
testGroup
"String utils"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment