Bryan O'Sullivan avatar Bryan O'Sullivan committed 0b6b550

Initial commit of Haskell code

Comments (0)

Files changed (10)

+^(?:cabal-dev|dist)$
+^demo/demo$
+\.(?:aux|eventlog|h[ip]|log|[oa]|orig|prof|ps|rej|swp)$
+~$
+syntax: glob
+.\#*

Data/Double/Conversion.hs

+{-# LANGUAGE ForeignFunctionInterface, MagicHash, Rank2Types,
+    UnliftedFFITypes #-}
+
+module Data.Double.Conversion
+    (
+      toExponential
+    , toFixed
+    , toPrecision
+    , toShortest
+    ) where
+
+import Control.Monad (when)
+import Control.Monad.ST (unsafeIOToST, runST)
+import Data.Text.Internal (Text(Text))
+import Foreign.C.Types (CDouble, CInt)
+import GHC.Prim (MutableByteArray#)
+import qualified Data.Text.Array as A
+
+toExponential :: Int -> Double -> Text
+toExponential ndigits = convert len $ \val mba ->
+                        c_ToExponential val mba (fromIntegral ndigits)
+  where len = c_ToExponentialLength
+        {-# NOINLINE len #-}
+
+toFixed :: Int -> Double -> Text
+toFixed ndigits = convert len $ \val mba ->
+                  c_ToFixed val mba (fromIntegral ndigits)
+  where len = c_ToFixedLength
+        {-# NOINLINE len #-}
+
+toShortest :: Double -> Text
+toShortest = convert len c_ToShortest
+  where len = c_ToShortestLength
+        {-# NOINLINE len #-}
+
+toPrecision :: Int -> Double -> Text
+toPrecision ndigits = convert len $ \val mba ->
+                      c_ToPrecision val mba (fromIntegral ndigits)
+  where len = c_ToPrecisionLength
+        {-# NOINLINE len #-}
+
+convert :: CInt -> (forall s. CDouble -> MutableByteArray# s -> IO CInt)
+        -> Double -> Text
+convert len act val = runST go
+  where
+    go = do
+      buf <- A.new (fromIntegral len)
+      size <- unsafeIOToST $ act (realToFrac val) (A.maBA buf)
+      when (size == -1) $ fail "convert"
+      frozen <- A.unsafeFreeze buf
+      return $! Text frozen 0 (fromIntegral size)
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToShortestLength"
+    c_ToShortestLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToShortest"
+    c_ToShortest :: CDouble -> MutableByteArray# s -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToFixedLength"
+    c_ToFixedLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToFixed"
+    c_ToFixed :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToExponentialLength"
+    c_ToExponentialLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToExponential"
+    c_ToExponential :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToPrecisionLength"
+    c_ToPrecisionLength :: CInt
+
+foreign import ccall unsafe "hs-double-conversion.h _hs_ToPrecision"
+    c_ToPrecision :: CDouble -> MutableByteArray# s -> CInt -> IO CInt
+See double-conversion/LICENSE for the license that applies to the C++
+library.
+
+Copyright (c) 2011 MailRank, Inc.
+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.
+
+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.
+# Welcome to text-format
+
+text-format is a fast and easy-to-use Haskell library for formatting
+text strings.
+
+# Join in!
+
+We are happy to receive bug reports, fixes, documentation enhancements,
+and other improvements.
+
+Please report bugs via the
+[github issue tracker](https://github.com/mailrank/text-format/issues).
+
+Master [git repository](https://github.com/mailrank/text-format):
+
+* `git clone git://github.com/mailrank/text-format.git`
+
+There's also a [Mercurial mirror](https://bitbucket.org/bos/text-format):
+
+* `hg clone https://bitbucket.org/bos/text-format`
+
+(You can create and contribute changes using either git or Mercurial.)
+
+Authors
+-------
+
+This library is written and maintained by Bryan O'Sullivan,
+<bos@mailrank.com>.
+#!/usr/bin/env runhaskell
+> import Distribution.Simple
+> main = defaultMain

cbits/hs-double-conversion.cc

+#include "double-conversion.h"
+#include "hs-double-conversion.h"
+#include <stdio.h>
+
+using namespace double_conversion;
+
+static const int kToShortestLength = 24;
+
+extern "C"
+int _hs_ToShortestLength(void)
+{
+  return kToShortestLength;
+}
+
+static const int kToFixedLength =
+  1 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
+  1 + DoubleToStringConverter::kMaxFixedDigitsAfterPoint;
+
+extern "C"
+int _hs_ToFixedLength(void)
+{
+  return kToFixedLength;
+}
+
+static const int kToExponentialLength =
+  DoubleToStringConverter::kMaxExponentialDigits + 8;
+
+extern "C"
+int _hs_ToExponentialLength(void)
+{
+  return kToExponentialLength;
+}
+
+static const int kToPrecisionLength =
+  DoubleToStringConverter::kMaxPrecisionDigits + 7;
+
+extern "C"
+int _hs_ToPrecisionLength(void)
+{
+  return kToPrecisionLength;
+}
+
+static int copy(uint16_t *buf, const StringBuilder& builder, const char *cbuf)
+{
+  const int pos = builder.position();
+  for (int i = 0; i < pos; i++)
+    buf[i] = cbuf[i];
+  return pos;
+}
+
+extern "C"
+int _hs_ToShortest(double value, uint16_t *buf)
+{
+  const DoubleToStringConverter& converter =
+    DoubleToStringConverter::EcmaScriptConverter();
+  char cbuf[kToShortestLength];
+
+  StringBuilder builder(cbuf, kToShortestLength);
+  bool ok = converter.ToShortest(value, &builder);
+
+  if (!ok)
+    return -1;
+
+  return copy(buf, builder, cbuf);
+}
+
+extern "C"
+int _hs_ToFixed(double value, uint16_t *buf, const int ndigits)
+{
+  const DoubleToStringConverter& converter =
+    DoubleToStringConverter::EcmaScriptConverter();
+  char cbuf[kToFixedLength];
+
+  StringBuilder builder(cbuf, kToFixedLength);
+  bool ok = converter.ToFixed(value, ndigits, &builder);
+
+  if (!ok)
+    return -1;
+  
+  return copy(buf, builder, cbuf);
+}
+
+extern "C"
+int _hs_ToExponential(double value, uint16_t *buf, const int ndigits)
+{
+  const DoubleToStringConverter& converter =
+    DoubleToStringConverter::EcmaScriptConverter();
+  char cbuf[kToExponentialLength];
+
+  StringBuilder builder(cbuf, kToExponentialLength);
+  bool ok = converter.ToExponential(value, ndigits, &builder);
+
+  if (!ok)
+    return -1;
+  
+  return copy(buf, builder, cbuf);
+}
+
+extern "C"
+int _hs_ToPrecision(double value, uint16_t *buf, const int precision)
+{
+  const DoubleToStringConverter& converter =
+    DoubleToStringConverter::EcmaScriptConverter();
+  char cbuf[kToPrecisionLength];
+
+  StringBuilder builder(cbuf, kToPrecisionLength);
+  bool ok = converter.ToPrecision(value, precision, &builder);
+
+  if (!ok)
+    return -1;
+  
+  return copy(buf, builder, cbuf);
+}
+import Data.Text ()
+import Data.Double.Conversion
+import System.Environment
+import System.Exit
+import System.IO
+
+main = do
+  args <- getArgs
+  case args of
+    ("shortest":xs) -> print (map (toShortest . read) xs)
+    ("fixed":n:xs) -> print (map (toFixed (read n) . read) xs)
+    ("exponential":n:xs) -> print (map (toExponential (read n) . read) xs)
+    ("precision":n:xs) -> print (map (toPrecision (read n) . read) xs)
+    _ -> do
+         hPutStrLn stderr "oops! no mode specified!"
+         exitWith (ExitFailure 1)
+demo: Demo.hs
+	ghc -O --make -o $@ $<
+
+clean:
+	-rm -f demo *.hi *.o

double-conversion.cabal

+name:           double-conversion
+version:        0.1.0.0
+license:        BSD3
+license-file:   LICENSE
+homepage:       https://github.com/mailrank/double-conversion
+bug-reports:    https://github.com/mailrank/double-conversion/issues
+category:       Text
+author:         Bryan O'Sullivan <bos@mailrank.com>
+maintainer:     Bryan O'Sullivan <bos@mailrank.com>
+stability:      experimental
+tested-with:    GHC == 7.0.3
+synopsis:       Fast conversion between double precision floating point and text
+cabal-version:  >= 1.8
+build-type:     Simple
+description:
+    A library that performs fast conversion between double precision
+    floating point and text, implemented as bindings to the C++
+    double-conversion library written by Florian Loitsch at Google.
+
+extra-source-files:
+    README.markdown
+    double-conversion/COPYING
+    double-conversion/LICENSE
+    double-conversion/Makefile
+    double-conversion/README
+    double-conversion/SConstruct
+    double-conversion/src/*.cc
+    double-conversion/src/*.h
+    double-conversion/src/SConscript
+    double-conversion/test/cctest/*.cc
+    double-conversion/test/cctest/*.h
+    double-conversion/test/cctest/SConscript
+
+flag developer
+  description: operate in developer mode
+  default: False
+
+library
+  c-sources:
+    cbits/hs-double-conversion.cc
+    double-conversion/src/bignum.cc
+    double-conversion/src/bignum-dtoa.cc
+    double-conversion/src/cached-powers.cc
+    double-conversion/src/diy-fp.cc
+    double-conversion/src/double-conversion.cc
+    double-conversion/src/fast-dtoa.cc
+    double-conversion/src/fixed-dtoa.cc
+    double-conversion/src/strtod.cc
+
+  extra-libraries: stdc++
+
+  include-dirs:
+    double-conversion/src
+    include
+
+  exposed-modules:
+    Data.Double.Conversion
+
+  build-depends:
+    base == 4.*,
+    ghc-prim,
+    text >= 0.11.0.8
+
+  if flag(developer)
+    ghc-options: -Werror
+    ghc-prof-options: -auto-all
+  else
+    cc-options: -DNDEBUG
+
+  ghc-options: -Wall
+
+  cpp-options: -DINTEGER_GMP
+
+  if impl(ghc >= 6.11)
+    build-depends: integer-gmp >= 0.2 && < 0.3
+
+  if impl(ghc >= 6.9) && impl(ghc < 6.11)
+    build-depends: integer >= 0.1 && < 0.2
+
+source-repository head
+  type:     git
+  location: https://github.com/mailrank/double-conversion
+
+source-repository head
+  type:     mercurial
+  location: https://bitbucket.org/bos/double-conversion

include/hs-double-conversion.h

+#ifndef _hs_double_conversion_h
+#define _hs_double_conversion_h
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <stddef.h>
+
+int _hs_ToShortestLength(void);
+int _hs_ToShortest(double value, uint16_t *buf);
+int _hs_ToFixedLength(void);
+int _hs_ToFixed(double value, uint16_t *buf, int ndigits);
+int _hs_ToExponentialLength(void);
+int _hs_ToExponential(double value, uint16_t *buf, int ndigits);
+int _hs_ToPrecisionLength(void);
+int _hs_ToPrecision(double value, uint16_t *buf, int ndigits);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _hs_double_conversion_h */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.