[project @ 1999-10-29 01:16:48 by andy]
[ghc-hetmet.git] / ghc / lib / std / CPUTime.lhs
index 12d1e87..037460b 100644 (file)
@@ -11,47 +11,94 @@ module CPUTime
          getCPUTime,       -- :: IO Integer
         cpuTimePrecision  -- :: Integer
         ) where
+\end{code}
+
 
+#ifndef __HUGS__
+\begin{code}
 import PrelBase
-import PrelArr  ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
+import PrelArr         ( ByteArray(..), newIntArray, unsafeFreezeByteArray )
 import PrelMaybe
 import PrelNum
-import PrelAddr
+import PrelNumExtra
 import PrelIOBase
-import IO
 import PrelST
+import IO              ( ioError )
+import PrelNum ( Num(..), Integral(..) )       -- To get fromInt/toInt
 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}
+#ifdef __HUGS__
+
+sizeof_int :: Int
+sizeof_int = 4
+
 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 <- 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))
+    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")
+#endif
 
 cpuTimePrecision :: Integer
 cpuTimePrecision = round ((1000000000000::Integer) % 
-                          fromInt (unsafePerformIO (_ccall_ clockTicks )))
+                          fromInt (unsafePerformIO clockTicks))
 \end{code}
 
+\begin{code}
+foreign import "libHS_cbits" "getCPUTime" unsafe primGetCPUTime :: ByteArray Int -> IO Int
+foreign import "libHS_cbits" "clockTicks" clockTicks :: IO Int
 
+\end{code}
 
+#else
 
+\begin{code}
+-- TODO: Hugs/getCPUTime
+getCPUTime :: IO Integer
+getCPUTime = return 0
+
+-- TODO: Hugs/cpuTimePrecision
+cpuTimePrecision :: Integer
+cpuTimePrecision = 1
+\end{code} 
+#endif