fixed-0.2.1.1/0000755000000000000000000000000012476532003011140 5ustar0000000000000000fixed-0.2.1.1/.gitignore0000644000000000000000000000017512476532003013133 0ustar0000000000000000dist/ .hsenv/ docs wiki TAGS tags wip .DS_Store .*.swp .*.swo *.o *.hi *~ *# .cabal-sandbox/ cabal.sandbox.config codex.tags fixed-0.2.1.1/.travis.yml0000644000000000000000000000337412476532002013257 0ustar0000000000000000# NB: don't set `language: haskell` here # See also https://github.com/hvr/multi-ghc-travis for more information env: # we have to use CABALVER=1.16 for GHC<7.6 as well, as there's # no package for earlier cabal versions in the PPA - GHCVER=7.4.2 CABALVER=1.16 - GHCVER=7.6.3 CABALVER=1.16 - GHCVER=7.8.4 CABALVER=1.18 - GHCVER=7.10.1 CABALVER=1.22 - GHCVER=head CABALVER=1.22 matrix: allow_failures: - env: GHCVER=head CABALVER=1.22 # Note: the distinction between `before_install` and `install` is not # important. before_install: - travis_retry sudo add-apt-repository -y ppa:hvr/ghc - travis_retry sudo apt-get update - travis_retry sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH - cabal --version install: - travis_retry cabal update - cabal install --only-dependencies # Here starts the actual work to be performed for the package under # test; any command which exits with a non-zero exit code causes the # build to fail. script: # -v2 provides useful information for debugging - cabal configure -v2 # this builds all libraries and executables # (including tests/benchmarks) - cabal build # tests that a source-distribution can be generated - cabal sdist # check that the generated source-distribution can be built & installed - export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ; cd dist/; if [ -f "$SRC_TGZ" ]; then cabal install --force-reinstalls "$SRC_TGZ"; else echo "expected '$SRC_TGZ' not found"; exit 1; fi notifications: irc: channels: - "irc.freenode.org#haskell-lens" skip_join: true template: - "\x0313fixed\x03/\x0306%{branch}\x03 \x0314%{commit}\x03 %{build_url} %{message}" fixed-0.2.1.1/CHANGELOG.markdown0000644000000000000000000000076112476532003014177 0ustar00000000000000000.2.1.1 ------- * Minor documentation improvements. * Require `base >= 4.7` properly in the source repository as the implementation uses `coerce`, so it doesn't work on GHC < 7.8. This was fixed by maintenance releases to hackage previously. 0.2.1 ----- * Fixed bug in `signum` 0.2 --- * Convert to/from Double to avoid precision issues. 0.1.0.1 ------- * Change `tested-with` to admit the support window is only 7.8+ at this point due to the use of `Data.Coerce` 0.1 --- * Initial release fixed-0.2.1.1/fixed.cabal0000644000000000000000000000154312476532002013225 0ustar0000000000000000name: fixed category: Numeric version: 0.2.1.1 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE author: Edward A. Kmett maintainer: Edward A. Kmett stability: provisional homepage: http://github.com/ekmett/fixed bug-reports: http://github.com/ekmett/fixed/issues copyright: Copyright (C) 2014 Edward A. Kmett build-type: Simple tested-with: GHC == 7.8.3 synopsis: Signed 15.16 precision fixed point arithmetic description: Signed 15.16 precision fixed point arithmetic extra-source-files: .travis.yml .gitignore README.markdown CHANGELOG.markdown source-repository head type: git location: git://github.com/ekmett/fixed.git library hs-source-dirs: src build-depends: base >= 4.7 && < 5 ghc-options: -Wall -fwarn-tabs -O2 exposed-modules: Numeric.Fixed fixed-0.2.1.1/LICENSE0000644000000000000000000000236412476532002012151 0ustar0000000000000000Copyright 2014-2015 Edward Kmett All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. 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. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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. fixed-0.2.1.1/README.markdown0000644000000000000000000000100312476532003013633 0ustar0000000000000000fixed ==== [![Build Status](https://secure.travis-ci.org/ekmett/fixed.png?branch=master)](http://travis-ci.org/ekmett/fixed) This package supplies signed fixed-precision values 15 bits above the decimal, 16 bits below. These arise commonly in GPU applications and it is chosen to match the GLfixed specification. Contact Information ------------------- Contributions and bug reports are welcome! Please feel free to contact me through github or on the #haskell IRC channel on irc.freenode.net. -Edward Kmett fixed-0.2.1.1/Setup.lhs0000644000000000000000000000016512476532002012751 0ustar0000000000000000#!/usr/bin/runhaskell > module Main (main) where > import Distribution.Simple > main :: IO () > main = defaultMain fixed-0.2.1.1/src/0000755000000000000000000000000012476532002011726 5ustar0000000000000000fixed-0.2.1.1/src/Numeric/0000755000000000000000000000000012476532002013330 5ustar0000000000000000fixed-0.2.1.1/src/Numeric/Fixed.hs0000644000000000000000000001054012476532002014723 0ustar0000000000000000{-# LANGUAGE CApiFFI, DeriveDataTypeable, GeneralizedNewtypeDeriving #-} ----------------------------------------------------------------------------- -- | -- Copyright : (C) 2014-15 Edward Kmett -- License : BSD-style (see the file LICENSE) -- Maintainer : Edward Kmett -- Stability : experimental -- Portability : non-portable -- -- Fixed precision arithmetic. This format is the same format used by -- OpenGL ES 1's @GLfixed@ data type: -- -- One sign bit, 15 bits to the left of the decimal place and 16 bits -- to the right packed into a 32-bit integer. ----------------------------------------------------------------------------- module Numeric.Fixed ( Fixed(..) , fromFixed , toFixed ) where import Data.Bits import Data.Coerce import Data.Int import Data.Ratio import Data.Typeable import Foreign.Storable import Foreign.C.Types -- | A signed 2s complement 15.16 scale fixed precision number newtype {-# CTYPE "signed int" #-} Fixed = Fixed { getFixed :: CInt } deriving (Eq,Ord,Typeable,Storable) -- | Convert from a 'Fixed' precision value to a 'Double' fromFixed :: Fixed -> Double fromFixed (Fixed x) = fromIntegral x / 65536 -- | Convert from a 'Double' to a 'Fixed' precision value toFixed :: Double -> Fixed toFixed x = Fixed $ floor (x * 65536 + 0.5) instance Show Fixed where showsPrec d = showsPrec d . fromFixed instance Num Fixed where (+) = coerce ((+) :: CInt -> CInt -> CInt) (-) = coerce ((-) :: CInt -> CInt -> CInt) negate = coerce (negate :: CInt -> CInt) abs = coerce (abs :: CInt -> CInt) signum (Fixed a) = Fixed $ unsafeShiftL (signum a) 16 Fixed a * Fixed b = Fixed $ fromIntegral (unsafeShiftR (fromIntegral a * fromIntegral b) 16 :: Int64) fromInteger i = Fixed $ unsafeShiftL (fromInteger i) 16 instance Enum Fixed where succ (Fixed a) = Fixed (a + 0x10000) pred (Fixed a) = Fixed (a - 0x10000) fromEnum = truncate toEnum a = Fixed (unsafeShiftL (fromIntegral a) 16) enumFrom a = toFixed `map` enumFrom (fromFixed a) enumFromTo a b = toFixed `map` enumFromTo (fromFixed a) (fromFixed b) enumFromThen a b = toFixed `map` enumFromThen (fromFixed a) (fromFixed b) enumFromThenTo a b c = toFixed `map` enumFromThenTo (fromFixed a) (fromFixed b) (fromFixed c) instance Bounded Fixed where minBound = Fixed minBound maxBound = Fixed maxBound instance Fractional Fixed where Fixed a / Fixed b = Fixed $ fromIntegral (unsafeShiftL (fromIntegral a) 16 `div` fromIntegral b :: Int64) fromRational a = Fixed $ fromInteger (unsafeShiftL (numerator a) 16 `div` denominator a) instance Real Fixed where toRational (Fixed i) = toInteger i % 65536 instance RealFrac Fixed where properFraction (Fixed a) | a >= 0 = (fromIntegral (unsafeShiftR a 16), Fixed (a .&. 0xffff)) | otherwise = (negate $ fromIntegral $ unsafeShiftR (negate a) 16, Fixed $ (a .&. 0xffff) - 0x10000) truncate (Fixed a) | a >= 0 = fromIntegral (unsafeShiftR a 16) | otherwise = negate $ fromIntegral $ unsafeShiftR (negate a) 16 round (Fixed f) = fromIntegral $ unsafeShiftR (f + 0x800) 16 ceiling (Fixed f) = fromIntegral $ unsafeShiftR (f + 0xffff) 16 floor (Fixed f) = fromIntegral $ unsafeShiftR f 16 instance Floating Fixed where pi = toFixed pi exp = toFixed . exp . fromFixed sqrt = toFixed . sqrt . fromFixed log = toFixed . log . fromFixed a ** b = toFixed $ fromFixed a ** fromFixed b logBase a b = toFixed $ logBase (fromFixed a) (fromFixed b) sin = toFixed . sin . fromFixed tan = toFixed . tan . fromFixed cos = toFixed . cos . fromFixed asin = toFixed . asin . fromFixed atan = toFixed . atan . fromFixed acos = toFixed . acos . fromFixed sinh = toFixed . sinh . fromFixed tanh = toFixed . tanh . fromFixed cosh = toFixed . cosh . fromFixed asinh = toFixed . asinh . fromFixed atanh = toFixed . atanh . fromFixed acosh = toFixed . acosh . fromFixed instance RealFloat Fixed where floatRadix _ = 2 floatDigits _ = 16 decodeFloat = decodeFloat . fromFixed isInfinite _ = False isIEEE _ = False atan2 a b = toFixed $ atan2 (fromFixed a) (fromFixed b) isDenormalized (Fixed a) = a .&. 0x7fff0000 /= 0 isNaN _ = False isNegativeZero _ = False floatRange _ = (15,0) encodeFloat i j = toFixed $ encodeFloat i j exponent = exponent . fromFixed significand = toFixed . significand . fromFixed scaleFloat n (Fixed a) = Fixed (shift a n)