[project @ 2001-03-01 13:06:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
index 037460b..9478bdc 100644 (file)
@@ -1,10 +1,12 @@
+% -----------------------------------------------------------------------------
+% $Id: CPUTime.lhs,v 1.29 2001/02/22 16:48:24 qrczak Exp $
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1995-1997
+% (c) The University of Glasgow, 1995-2000
 %
-\section[CPUTime]{Haskell 1.4 CPU Time Library}
+\section[CPUTime]{Haskell 98 CPU Time Library}
 
 \begin{code}
-{-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
+{-# OPTIONS -#include "cbits/stgio.h" #-}
 
 module CPUTime 
        (
@@ -15,16 +17,16 @@ module CPUTime
 
 
 #ifndef __HUGS__
+
 \begin{code}
-import PrelBase
-import PrelArr         ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
-import PrelMaybe
-import PrelNum
-import PrelNumExtra
-import PrelIOBase
-import PrelST
-import IO              ( ioError )
-import PrelNum ( Num(..), Integral(..) )       -- To get fromInt/toInt
+import Prelude         -- To generate the dependency
+import PrelGHC         ( indexIntArray# )
+import PrelBase                ( Int(..) )
+import PrelByteArr     ( ByteArray(..), newIntArray )
+import PrelArrExtra     ( unsafeFreezeByteArray )
+import PrelIOBase      ( IOException(..), 
+                         IOErrorType( UnsupportedOperation ), 
+                         unsafePerformIO, stToIO, ioException )
 import Ratio
 \end{code}
 
@@ -37,30 +39,6 @@ in CPU time that the implementation can record, and is given as an
 integral number of picoseconds.
 
 \begin{code}
-#ifdef __HUGS__
-
-sizeof_int :: Int
-sizeof_int = 4
-
-getCPUTime :: IO Integer
-getCPUTime = do
-    marr <- primNewByteArray (sizeof_int * 4)
-    rc   <- primGetCPUTime marr
-    if rc /= 0 then do
-        x0 <- primReadIntArray marr 0
-        x1 <- primReadIntArray marr 1
-        x2 <- primReadIntArray marr 2
-        x3 <- primReadIntArray marr 3
-        return ((fromIntegral x0 * 1000000000 + fromIntegral  x1 + 
-                fromIntegral x2 * 1000000000 + fromIntegral  x3)
-              * 1000)
-      else
-       ioError (IOError Nothing UnsupportedOperation 
-                        "getCPUTime"
-                        "can't get CPU time")
-
-#else
-
 getCPUTime :: IO Integer
 getCPUTime = do
     marr <- stToIO (newIntArray ((0::Int),3))
@@ -74,31 +52,38 @@ getCPUTime = do
                 fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
                  fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
      else
-       ioError (IOError Nothing UnsupportedOperation 
+       ioException (IOError Nothing UnsupportedOperation 
                         "getCPUTime"
-                        "can't get CPU time")
-#endif
+                        "can't get CPU time"
+                        Nothing)
 
 cpuTimePrecision :: Integer
 cpuTimePrecision = round ((1000000000000::Integer) % 
-                          fromInt (unsafePerformIO clockTicks))
-\end{code}
+                          fromIntegral (unsafePerformIO clockTicks))
 
-\begin{code}
 foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
-foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
+foreign import "libHS_cbits" "clockTicks" unsafe clockTicks :: IO Int
 
 \end{code}
 
 #else
 
 \begin{code}
--- TODO: Hugs/getCPUTime
+import PrelPrim ( nh_getCPUtime
+               , nh_getCPUprec
+               , unsafePerformIO
+               )
+
 getCPUTime :: IO Integer
-getCPUTime = return 0
+getCPUTime 
+   = do seconds <- nh_getCPUtime
+        return (round (seconds * 1.0e+12))
 
--- TODO: Hugs/cpuTimePrecision
 cpuTimePrecision :: Integer
-cpuTimePrecision = 1
+cpuTimePrecision
+   = unsafePerformIO (
+        do resolution <- nh_getCPUprec
+           return (round (resolution * 1.0e+12))
+     )
 \end{code} 
 #endif