VirtualBox

Changeset 104105 in vbox for trunk/src


Ignore:
Timestamp:
Mar 28, 2024 1:33:58 PM (10 months ago)
Author:
vboxsync
Message:

Runtime/darwin/time-darwin.cpp: Avoid using double-precision floating point while computing nano timestamp.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/darwin/time-darwin.cpp

    r99127 r104105  
    4848#include <iprt/time.h>
    4949#include <iprt/assert.h>
     50#include <iprt/asm-math.h>
    5051#include "internal/time.h"
    5152
     
    5556*********************************************************************************************************************************/
    5657static struct mach_timebase_info    g_Info = { 0, 0 };
    57 static double                       g_rdFactor = 0.0;
    5858static bool                         g_fFailedToGetTimeBaseInfo = false;
    5959
     
    6666    struct mach_timebase_info Info;
    6767    if (mach_timebase_info(&Info) == KERN_SUCCESS)
    68     {
    69         g_rdFactor = (double)Info.numer / (double)Info.denom;
    7068        g_Info = Info;
    71     }
    7269    else
    7370    {
    7471        g_fFailedToGetTimeBaseInfo = true;
    75         Assert(g_Info.denom == 0 && g_Info.numer == 0 && g_rdFactor == 0.0);
     72        Assert(g_Info.denom == 0 && g_Info.numer == 0);
    7673    }
    7774}
     
    8885        rtTimeDarwinLazyInit();
    8986
    90     /* special case: absolute time is in nanoseconds */
     87    /* special case: absolute time is in nanoseconds. */
    9188    if (g_Info.denom == 1 && g_Info.numer == 1)
    9289        return mach_absolute_time();
    9390
    9491    /* general case: multiply by factor to get nanoseconds. */
    95     if (g_rdFactor != 0.0)
    96         return mach_absolute_time() * g_rdFactor;
     92    if (g_Info.denom > 0 && g_Info.numer > 0)
     93    {
     94        /*
     95         * We could use clock_gettime_nsec_np(CLOCK_UPTIME_RAW) but internally
     96         * it might be calling mach_timebase_info() for each call. This is most
     97         * likely faster since we avoid calling mach_timebase_info() here.
     98         */
     99        uint64_t const cTicks = mach_absolute_time();
     100        return ASMMultU64ByU32DivByU32(cTicks, g_Info.numer, g_Info.denom);
     101    }
    97102
    98103    /* worst case: fallback to gettimeofday(). */
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette