CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2017
    Posts
    12

    Implicit type conversion and time measurement.

    I'm implementing a game loop in `c++` using the `timeGetTime` function like below:

    Code:
     // ...
            DWORD oldtime = 0, newtime = 0, delta = 0;
            //...
    		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
    
    			if (msg.message == WM_QUIT) {				
    				exit(0);
    			}
    
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    
    		}
    
    		delta = newtime - oldtime;		
    
    		while (delta >= MS_PER_UPDATE) {
    
    			//update();
    			delta -= MS_PER_UPDATE;
    			g_loopCnt++;
    		}
            //render();
            // ...
    I know that the value returned by the `timeGetTime` function wraps around to 0 every 2^32 milliseconds, which is about 49.71 days as the official doc says. So is it possible (however, it is very unlikely) that the delta will be a negative number (when the `newtime` will pass the "magic" 2^32 milliseconds barrier). Lets take for an example VERY unlikely but theoretically possible case:

    > `newtime` == 304(dec)
    > `oldtime` == 0x7FFFF82F (2147481647 dec)

    when the game is running longer then these 49.71 days. We'll get:

    Code:
        delta = 304(dec) - 2147481647(dec) == -2147481343(dec signed!);		
        ...
            while (-2147481343 >= MS_PER_UPDATE) {...}
    So there is of course a resulting a negative numer (`-2147481343`) so on the fiest look it won't work here. BUT the compiler will make here an implicit conversion from signed to unsigned resulting in:

    while (2147485952 >= MS_PER_UPDATE) {...}
    and finally the code WILL work! (thanks to this implicit conversion)
    And here is my question. Is it ok/safe to let the code be simple like that and just rely on the implicit conversion made automatically OR maybe I have to take care of the "wrap around" problem myself doing some extra checking/coding?

    P.S

    If I cannot count on that automatically implicit conversion is it a good solution to just change the operand order when there is about to be an negative delta like below?:

    Code:
     if(newtime < oldtime)
            delta = oldtime - newtime;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,280

    Re: Implicit type conversion and time measurement.

    The Microsoft recommendation from https://docs.microsoft.com/en-us/win...pi-timegettime is:
    Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to measure short time intervals at a high resolution.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,665

    Re: Implicit type conversion and time measurement.

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.3.5)

  4. #4
    Join Date
    Oct 2017
    Posts
    12

    Re: Implicit type conversion and time measurement.

    Quote Originally Posted by VictorN View Post
    The Microsoft recommendation from https://docs.microsoft.com/en-us/win...pi-timegettime is:
    Ok, I already know about these functions BUT I'm intentionally using the timeGetTime so instead of proposing sth. else could You please try to answer my original question?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,280

    Re: Implicit type conversion and time measurement.

    Quote Originally Posted by Mulligan View Post
    ... could You please try to answer my original question?
    Sorry, I can't.
    You should use the debugger to test whether "it is a good solution to just change the operand order". First with some predefined (by you!) values, then with the real values that the timeGetTime returns.
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,665

    Re: Implicit type conversion and time measurement.

    The original question was

    Is it ok/safe to let the code be simple like that and just rely on the implicit conversion made automatically OR maybe I have to take care of the "wrap around" problem myself doing some extra checking/coding?
    When signed/unsigned operations are mixed, the signed will always be 'promoted' to unsigned. Then the operation is performed. This automatic promotion is often reported as a warning of mixed types by compilers. If you don't want the warnings then an explicit cast can be used.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.3.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured