MSDN > フォーラム ホーム > Visual C++ Language > Targeting 64 bit processor?
質問する質問する
 

回答済みTargeting 64 bit processor?

  • 2009年11月4日 14:51Nick Duane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    I wanted to ask this in a more general Visual Studio forum but didn't find one that seemed to fit.

    I'm interested in using the 64 bit interlocked statements.  Are these supported by the processor only, or the processor and OS.  Meaning do I need to be running Windows x64 in order to use the underlying 64 bit processor interlocked support or as long as the processor is 64 bit can I take advantage of this support?  I see that I'll at least need to be running Vista or Windows 2003 Server in order to gain access to the exported functions.

    In looking through winbase.h, where these functions are defined (or declared), I see that it's dependent on _M_AMD64 or _M_IA64.  How do these get defined?  Can I target a 64 bit processor when compiling on XP 32 bit?

    Thanks,
    Nick

回答

  • 2009年11月5日 12:55crescens2k ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    If you are using a 32 bit version of Windows but on a 64 bit processor then you are not able to access these specific instructions. InterlockedCompareExchange64 uses the 64 bit instruction but this means that the operating system has to be running in 64 bit mode too.
    32 bit windows will put the processor into 32 bit mode, therefore it means that it will run identically to a 32 bit processor. The result of this is that the processor can't use the Rxx registers, the higher numbered xmm registers or 64 bit instructions.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • 回答としてマークNick Duane 2009年11月11日 17:08
    •  

すべての返信

  • 2009年11月4日 17:18Brian MuthMVPユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Nick, the operating system plays a key role in the implementation, namely through functions such as InterlockedCompareExchange64(). Authors of this API obviously have to worry about multi-core processors, and memory caching and so forth. Generally speaking, you shouldn't have to worry about the implementation details; you need to trust that the API function works as advertised.

    If you use the x64 C++ compiler, all the definitions are set automatically. Just include "windows.h" and you are on your way.
  • 2009年11月4日 17:33Sheng Jiang 蒋晟MVPユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    You need to use 64bit Windows SDK headers and libs, search "64 bit" in the Windows SDK forum if you need to know more.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
  • 2009年11月5日 0:52Nick Duane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    namely through functions such as InterlockedCompareExchange64(). Generally speaking, you shouldn't have to worry about the implementation details; you need to trust that the API function works as advertised.
    Yeah, I saw the macros defined for the different interlocked functions based on InterlockedCompareExchange64 if _M_AMD64 or _M_IA64 is not defined.  I certainly don't want to use InterlockedCompareExchange64() to implement the other interlocked functions if the underlying processor has the capability to implement those functions directly.

    I guess I still don't know whether I can take advantage of the 64 bit interlocked statements (directly, not using InterlockedCompareExchange64) when running on a 64 bit processor but 32 bit Windows.  And if so can I do this on 32 bit processor running Windows XP but target that 64 bit processor?

    Thanks,
    Nick
  • 2009年11月5日 2:56Brian MuthMVPユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     

    Yeah, I saw the macros defined for the different interlocked functions based on InterlockedCompareExchange64 if _M_AMD64 or _M_IA64 is not defined.  I certainly don't want to use InterlockedCompareExchange64() to implement the other interlocked functions if the underlying processor has the capability to implement those functions directly.

    Why not? If there are low-level assembler instructions that handle this function atomically, then InterlockedCompareExchangexxx() is going to use them.

    I guess I still don't know whether I can take advantage of the 64 bit interlocked statements (directly, not using InterlockedCompareExchange64) when running on a 64 bit processor but 32 bit Windows.  And if so can I do this on 32 bit processor running Windows XP but target that 64 bit processor?

    InterlockedCompareExchange() will work in both environments. Your question is a bit unclear, actually, since if you "target a 64 bit processor" you need to use the x64 C++ compiler.

    Thanks,
    Nick

  • 2009年11月5日 10:14Rong-Chun ZhangMSFT, モデレータユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Hello Nick,

    Thanks for your post.

    > And if so can I do this on 32 bit processor running Windows XP but target that 64 bit processor?

    Did you mean compile the program targeting 64bit? If so, as Brain, we need to install the x64 bit compile. Although, we can compile an x64 application on a x86 windows, we cannot run it on x86 windows.

    Thanks,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • 2009年11月5日 12:55crescens2k ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    If you are using a 32 bit version of Windows but on a 64 bit processor then you are not able to access these specific instructions. InterlockedCompareExchange64 uses the 64 bit instruction but this means that the operating system has to be running in 64 bit mode too.
    32 bit windows will put the processor into 32 bit mode, therefore it means that it will run identically to a 32 bit processor. The result of this is that the processor can't use the Rxx registers, the higher numbered xmm registers or 64 bit instructions.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • 回答としてマークNick Duane 2009年11月11日 17:08
    •  
  • 2009年11月11日 17:09Nick Duane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Thanks crescens2k, that's the information I was looking for.
  • 2009年11月11日 17:28Nick Duane ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    Yeah, I saw the macros defined for the different interlocked functions based on InterlockedCompareExchange64 if _M_AMD64 or _M_IA64 is not defined.  I certainly don't want to use InterlockedCompareExchange64() to implement the other interlocked functions if the underlying processor has the capability to implement those functions directly.

    Why not? If there are low-level assembler instructions that handle this function atomically, then InterlockedCompareExchangexxx() is going to use them.


    When you use InterlockedExchangeCompare() you're sitting in a loop waiting for the signal that you're the one who made the last change.  What if for some reason your thread is never the one that makes that last change?  You'll be sitting in the loop forever.  Now I don't think there is a likelihood this will happen, but if there is an atomic instruction that does what I need I would rather use that than implement it using InterlockedExchangeCompare().  Here is one of the macros (inline function) I see in winbase.h:

    LONGLONG
    FORCEINLINE
    InterlockedIncrement64 (
        __inout LONGLONG volatile *Addend
        )
    {
        LONGLONG Old;

        do {
            Old = *Addend;
        } while (InterlockedCompareExchange64(Addend,
                                              Old + 1,
                                              Old) != Old);

        return Old + 1;
    }

    So if I could make use of an atomic processor instruction to increment a 64 bit value I would rather use that than use the above InterlockedIncrement64() based on InterlockedCompareExchange64().  Though based on another post someone made it looks as though I can't take advantage of the 64 bit instructions unless the OS, and I would assume the app, are also 64 bit.

    My guess is that these inline macros are just for the 32 bit apps which want to make use of the 64 bit interlocked statements.

    Thanks,
    Nick

  • 2009年11月11日 17:40ildjarn ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    What do you think those functions do internally? The purpose of the functions is to present a stable API regardless of the underlying architecture (e.g. an app compiled for Itanium could call the functions, even though it might not be atomic on that architecture), but realistically, on x86 and x64 they're going to use the exact same atomic instructions you want to use.