Hi all,

After performing some operations I need to read flags such as the carry,
the zero, the negative. For instance:

static bool carry;
int sub(int x, int y) {
int res = x + y;
carry = get_cpu_carry_flag();
return res;
}

Is there and way to implement this get_cpu_carry_flag() function for
vc2005? I certainly need a fast way to do it. The inline assembler would
do it, but it is not there anymore.

Artem

Re: how to read x86/x64 flags by Phil

Phil
Fri Jun 23 18:14:16 CDT 2006

Artem Alimarine wrote:

> Hi all,
>
> After performing some operations I need to read flags such as the carry,
> the zero, the negative. For instance:
>
> static bool carry;
> int sub(int x, int y) {
> int res = x + y;
> carry = get_cpu_carry_flag();
> return res;
> }
>
> Is there and way to implement this get_cpu_carry_flag() function for
> vc2005? I certainly need a fast way to do it. The inline assembler would
> do it, but it is not there anymore.

I am using the free VC 2005 Express Edition and my inlined assembler functions
still work!

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

Re: how to read x86/x64 flags by Artem

Artem
Sat Jun 24 01:20:14 CDT 2006

>
> I am using the free VC 2005 Express Edition and my inlined assembler
> functions still work!
>

This one is probably only for x86 (32 bits), I am talking about
Professional on x64 (64 bits)

Re: how to read x86/x64 flags by Alex

Alex
Sat Jun 24 06:20:47 CDT 2006

Artem Alimarine wrote:
> Hi all,
>
> After performing some operations I need to read flags
> such as the carry, the zero, the negative. For instance:
>
> static bool carry;
> int sub(int x, int y) {
> int res = x + y;
> carry = get_cpu_carry_flag();
> return res;
> }
>
> Is there and way to implement this get_cpu_carry_flag()
> function for vc2005? I certainly need a fast way to do
> it. The inline assembler would do it, but it is not there
> anymore.

You have compiler intrinsic functions for almost everything.

"__getcallerseflags"
http://msdn2.microsoft.com/en-us/library/bkx0x9kc.aspx

"Compiler Intrinsics"
http://msdn2.microsoft.com/en-us/library/26td21ds.aspx

HTH
Alex



Re: how to read x86/x64 flags by Artem

Artem
Sat Jun 24 16:38:56 CDT 2006

Alex Blekhman wrote:
> You have compiler intrinsic functions for almost everything.
>
> "__getcallerseflags"
> http://msdn2.microsoft.com/en-us/library/bkx0x9kc.aspx

Thank you for the advice. The code now is

static unsigned flags;

int sub(int x, int y) {
int res = x - y;
flags = __getcallerseflags();
return res;
}

However, it does not work because the compiler reorders the operations
in such a way that the flags are fetched before the operation. How do I
wrestle that?

Re: how to read x86/x64 flags by Alexander

Alexander
Sat Jun 24 19:35:25 CDT 2006

carry = (unsigned) x < (unsigned) y;

I suppose you want to subtract them, not add?

For adding:

carry = (unsigned) (x+y) < (unsigned) y;
or:
carry = (unsigned) (x+y) < (unsigned) x;

"Artem Alimarine" <alimarine@zonnet.nl> wrote in message
news:eb4be$449c6eaa$513b8192$26072@news.versatel.net...
> Hi all,
>
> After performing some operations I need to read flags such as the carry,
> the zero, the negative. For instance:
>
> static bool carry;
> int sub(int x, int y) {
> int res = x + y;
> carry = get_cpu_carry_flag();
> return res;
> }
>
> Is there and way to implement this get_cpu_carry_flag() function for
> vc2005? I certainly need a fast way to do it. The inline assembler would
> do it, but it is not there anymore.
>
> Artem



Re: how to read x86/x64 flags by Alex

Alex
Sun Jun 25 04:58:03 CDT 2006

Artem Alimarine wrote:
>> You have compiler intrinsic functions for almost
>> everything. "__getcallerseflags"
>> http://msdn2.microsoft.com/en-us/library/bkx0x9kc.aspx
>
> Thank you for the advice. The code now is
>
> static unsigned flags;
>
> int sub(int x, int y) {
> int res = x - y;
> flags = __getcallerseflags();
> return res;
> }
>
> However, it does not work because the compiler reorders
> the operations in such a way that the flags are fetched
> before the operation. How do I wrestle that?

You can try to disable optimizer for specific function with
#pragma optimize. However, fighting optimizer is difficult
and often futile. Usually, if you catch yourself in such
situation, you should rethink what you actually want to do.



Re: how to read x86/x64 flags by Frederico

Frederico
Mon Jun 26 07:49:52 CDT 2006


"Artem Alimarine" <alimarine@zonnet.nl> escreveu na mensagem
news:48e9b$449cd9a1$513b8192$2889@news.versatel.net...
>>
>> I am using the free VC 2005 Express Edition and my inlined assembler
>> functions still work!
>>
>
> This one is probably only for x86 (32 bits), I am talking about
> Professional on x64 (64 bits)

So what?! The routine below still works in x64!

-----------%<------------%<----------
bool __fastcall GetCarryFlag(void)
{
__asm
{
mov eax,0
setc al
}
}
--------%<------------%<---------------



Re: how to read x86/x64 flags by Artem

Artem
Mon Jun 26 12:55:33 CDT 2006

For this program I get the error log below. My compiler version is also
printed in the log.

Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

x.cpp
x.cpp(4) : error C4235: nonstandard extension used : '__asm' keyword not
support ed on this architecture
x.cpp(6) : error C2065: 'mov' : undeclared identifier
x.cpp(6) : error C2146: syntax error : missing ';' before identifier 'eax'
x.cpp(6) : error C2065: 'eax' : undeclared identifier
x.cpp(7) : error C2146: syntax error : missing ';' before identifier 'setc'
x.cpp(8) : error C2065: 'setc' : undeclared identifier
x.cpp(8) : error C2146: syntax error : missing ';' before identifier 'al'
x.cpp(8) : error C2065: 'al' : undeclared identifier
x.cpp(8) : error C2143: syntax error : missing ';' before '}'


Frederico Pissarra wrote:
> So what?! The routine below still works in x64!
>

Re: how to read x86/x64 flags by Jochen

Jochen
Tue Jun 27 00:22:54 CDT 2006

Hi Frederico!

> So what?! The routine below still works in x64!
>
> -----------%<------------%<----------
> bool __fastcall GetCarryFlag(void)
> {
> __asm
> {
> mov eax,0
> setc al
> }
> }
> --------%<------------%<---------------

But only if compiled with x86!
x64 and IA64 are not supporting asm constructs.

Greetings
Jochen

Re: how to read x86/x64 flags by Artem

Artem
Tue Jun 27 12:34:17 CDT 2006

Alexander Grigoriev wrote:
> carry = (unsigned) x < (unsigned) y;
>

Actually, I need four flags: zero, negative, carry and overflow. I know
how to compute that in C++, and it is currently done that way. However,
the flag computation takes a significant time, minimal 10%. So, I just
wanted to know whether on x86 the native flags could be exploited.
However, the optmizer on both MSVC and GCC gets lost with these tricks.
In the end it seems better not to do it. Lazy computation seems to be
more promising and system independent.

Re: how to read x86/x64 flags by Tim

Tim
Tue Jun 27 23:47:20 CDT 2006

"Frederico Pissarra" <me@nowhere.net> wrote:
>
>So what?! The routine below still works in x64!
>
>-----------%<------------%<----------
>bool __fastcall GetCarryFlag(void)
>{
> __asm
> {
> mov eax,0
> setc al
> }
>}
>--------%<------------%<---------------

No, it most certainly does NOT. The code WOULD work, if you could get it
to assemble, but the __asm extension is simply NOT SUPPORTED in the 64-bit
compilers.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: how to read x86/x64 flags by Frederico

Frederico
Mon Jul 03 10:58:55 CDT 2006


"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> escreveu na
mensagem news:%232fq1mamGHA.4816@TK2MSFTNGP03.phx.gbl...
> Hi Frederico!
>
>> So what?! The routine below still works in x64!
>>
>> -----------%<------------%<----------
>> bool __fastcall GetCarryFlag(void)
>> {
>> __asm
>> {
>> mov eax,0
>> setc al
>> }
>> }
>> --------%<------------%<---------------
>
> But only if compiled with x86!
> x64 and IA64 are not supporting asm constructs.
>
> Greetings
> Jochen

Argh! Why not? The MSC++ for IA64 is still beta?
I didn't tried by, how about using MASM?

[]s
Fred



Re: how to read x86/x64 flags by Jochen

Jochen
Mon Jul 03 12:34:03 CDT 2006

Hi Frederico!

>>But only if compiled with x86!
>>x64 and IA64 are not supporting asm constructs.
>
> Argh! Why not? The MSC++ for IA64 is still beta?
> I didn't tried by, how about using MASM?

The C++ compiler only supports __asm in x86 mode.

Of course, you can use a "real" assembler... but not inlined asm...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/