Fixing the logic in SystemNative_GetTimestampResolution to return the correct "ticks...
authorTanner Gooding <tagoo@outlook.com>
Sat, 27 Apr 2019 01:05:18 +0000 (18:05 -0700)
committerGitHub <noreply@github.com>
Sat, 27 Apr 2019 01:05:18 +0000 (18:05 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/4ff66e551b07802a4021d538633d77d3ee66ec89

src/libraries/Native/Unix/System.Native/pal_time.c

index 11946d7fdf52c2a344a96a5d0471e902941eda80..90665e7c9671845cd1d96bd89ee0271c9be43ded 100644 (file)
@@ -62,8 +62,11 @@ uint64_t SystemNative_GetTimestampResolution()
         return 0;
     }
 
-    uint64_t nanosecondsPerTick = ((uint64_t)(mtid.denom) / (uint64_t)(mtid.numer));
-    return SecondsToNanoSeconds * nanosecondsPerTick;
+    // (numer / denom) gives you the nanoseconds per tick, so the below code
+    // computes the number of ticks per second. We explicitly do the multiplication
+    // first in order to help minimize the error that is produced by integer division.
+
+    return (SecondsToNanoSeconds * (uint64_t)(mtid.denom)) / (uint64_t)(mtid.numer);
 #else
     struct timespec ts;
 
@@ -73,7 +76,7 @@ uint64_t SystemNative_GetTimestampResolution()
     }
 
     uint64_t nanosecondsPerTick = ((uint64_t)(ts.tv_sec) * SecondsToNanoSeconds) + (uint64_t)(ts.tv_nsec);
-    return SecondsToNanoSeconds * nanosecondsPerTick;
+    return SecondsToNanoSeconds / nanosecondsPerTick;
 #endif
 }