{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, MagicHash, UnboxedTuples, NoImplicitPrelude #-}
{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_HADDOCK not-home #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Float.ConversionUtils
-- Copyright   :  (c) Daniel Fischer 2010
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC Extensions)
--
-- Utilities for conversion between Double/Float and Rational
--
-----------------------------------------------------------------------------

#include "MachDeps.h"

module GHC.Float.ConversionUtils ( elimZerosInteger, elimZerosInt# ) where

import GHC.Base
import GHC.Num.Integer

default ()

#if WORD_SIZE_IN_BITS < 64

#define TO64    integerToInt64#

-- Double mantissae have 53 bits, too much for Int#
elim64# :: Int64# -> Int# -> (# Integer, Int# #)
elim64# n e =
    case zeroCount (int64ToInt# n) of
      t | isTrue# (e <=# t) -> (# integerFromInt64# (uncheckedIShiftRA64# n e), 0# #)
        | isTrue# (t <# 8#) -> (# integerFromInt64# (uncheckedIShiftRA64# n t), e -# t #)
        | otherwise         -> elim64# (uncheckedIShiftRA64# n 8#) (e -# 8#)

#else

#define TO64    integerToInt#

-- Double mantissae fit it Int#
elim64# :: Int# -> Int# -> (# Integer, Int# #)
elim64# :: Int# -> Int# -> (# Integer, Int# #)
elim64# = Int# -> Int# -> (# Integer, Int# #)
elimZerosInt#

#endif

{-# INLINE elimZerosInteger #-}
elimZerosInteger :: Integer -> Int# -> (# Integer, Int# #)
elimZerosInteger :: Integer -> Int# -> (# Integer, Int# #)
elimZerosInteger Integer
m Int#
e = Int# -> Int# -> (# Integer, Int# #)
elim64# (TO64 m) e

elimZerosInt# :: Int# -> Int# -> (# Integer, Int# #)
elimZerosInt# :: Int# -> Int# -> (# Integer, Int# #)
elimZerosInt# Int#
n Int#
e =
    case Int# -> Int#
zeroCount Int#
n of
      Int#
t | Int# -> Bool
isTrue# (Int#
e Int# -> Int# -> Int#
<=# Int#
t) -> (# Int# -> Integer
IS (Int# -> Int# -> Int#
uncheckedIShiftRA# Int#
n Int#
e), Int#
0# #)
        | Int# -> Bool
isTrue# (Int#
t Int# -> Int# -> Int#
<# Int#
8#) -> (# Int# -> Integer
IS (Int# -> Int# -> Int#
uncheckedIShiftRA# Int#
n Int#
t), Int#
e Int# -> Int# -> Int#
-# Int#
t #)
        | Bool
otherwise         -> Int# -> Int# -> (# Integer, Int# #)
elimZerosInt# (Int# -> Int# -> Int#
uncheckedIShiftRA# Int#
n Int#
8#) (Int#
e Int# -> Int# -> Int#
-# Int#
8#)

-- | Number of trailing zero bits in a byte
zeroCount :: Int# -> Int#
zeroCount :: Int# -> Int#
zeroCount Int#
i = Int8# -> Int#
int8ToInt# (Addr# -> Int# -> Int8#
indexInt8OffAddr# Addr#
arr (Word# -> Int#
word2Int# (Word# -> Word#
narrow8Word# (Int# -> Word#
int2Word# Int#
i)))) -- index must be in [0,255]
  where
    arr :: Addr#
arr = Addr#
"\8\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\5\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\6\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\5\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\7\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\5\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\6\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\5\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0\4\0\1\0\2\0\1\0\3\0\1\0\2\0\1\0"#