[project @ 1999-12-20 10:34:27 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
index 577da56..9d7e6a7 100644 (file)
@@ -4,54 +4,77 @@
 \section[CPUTime]{Haskell 1.4 CPU Time Library}
 
 \begin{code}
-{-# OPTIONS -fno-implicit-prelude -#include "cbits/stgio.h" #-}
+{-# OPTIONS -#include "cbits/stgio.h" #-}
 
 module CPUTime 
        (
          getCPUTime,       -- :: IO Integer
         cpuTimePrecision  -- :: Integer
         ) where
+\end{code}
 
-import PrelBase
-import PrelArr  ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
-import PrelMaybe
-import PrelNum
-import PrelAddr
-import PrelIOBase
-import IO
-import PrelST
-import Ratio
 
+#ifndef __HUGS__
+
+\begin{code}
+import Prelude         -- To generate the dependency
+import PrelGHC         ( indexIntArray# )
+import PrelBase                ( Int(..) )
+import PrelByteArr     ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
+import PrelNum         ( fromInt )
+import PrelIOBase      ( IOError(..), IOErrorType( UnsupportedOperation ), 
+                         unsafePerformIO, stToIO )
+import Ratio
 \end{code}
 
 Computation @getCPUTime@ returns the number of picoseconds CPU time
 used by the current program.  The precision of this result is
 implementation-dependent.
 
-The @cpuTimePrecision@ constant is the resolution (in picoseconds!) of
-the number of 
+The @cpuTimePrecision@ constant is the smallest measurable difference
+in CPU time that the implementation can record, and is given as an
+integral number of picoseconds.
 
 \begin{code}
 getCPUTime :: IO Integer
-getCPUTime = 
-    stToIO (newIntArray (0,3))         >>= \ marr ->
-    stToIO (unsafeFreezeByteArray marr)        >>= \ barr@(ByteArray _ frozen#) ->
-    _ccall_ getCPUTime barr            >>= \ ptr ->
-    if (ptr::Addr) /= ``NULL'' then
+getCPUTime = do
+    marr <- stToIO (newIntArray ((0::Int),3))
+    barr <- stToIO (unsafeFreezeByteArray marr)
+    rc   <- primGetCPUTime barr
+    if rc /= 0 then
+      case barr of
+       ByteArray _ _ frozen# -> -- avoid bounds checking
         return ((fromIntegral (I# (indexIntArray# frozen# 0#)) * 1000000000 + 
-                fromIntegral (I# (indexIntArray# frozen# 1#)) + 
-               fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
-                fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
-    else
-       fail (IOError Nothing UnsupportedOperation "getCPUTime"
-               "can't get CPU time")
+                 fromIntegral (I# (indexIntArray# frozen# 1#)) + 
+                fromIntegral (I# (indexIntArray# frozen# 2#)) * 1000000000 + 
+                 fromIntegral (I# (indexIntArray# frozen# 3#))) * 1000)
+     else
+       ioError (IOError Nothing UnsupportedOperation 
+                        "getCPUTime"
+                        "can't get CPU time")
 
 cpuTimePrecision :: Integer
 cpuTimePrecision = round ((1000000000000::Integer) % 
-                          fromInt (unsafePerformIO (_ccall_ clockTicks )))
-\end{code}
+                          fromInt (unsafePerformIO clockTicks))
 
+foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
+foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
 
+\end{code}
 
+#else
 
+\begin{code}
+getCPUTime :: IO Integer
+getCPUTime 
+   = do seconds <- nh_getCPUtime
+        return (round (seconds * 1.0e+12))
+
+cpuTimePrecision :: Integer
+cpuTimePrecision
+   = primRunST (
+        do resolution <- nh_getCPUprec
+           return (round (resolution * 1.0e+12))
+     )
+\end{code} 
+#endif