News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

AVX for 32-bit Windows applications

Started by Gunther, May 27, 2014, 04:08:26 AM

Previous topic - Next topic

Gunther

The attached archive is ArrayCopy.zip. The C program is only the frame. The real work is done by the assembly language procedures.

The application checks if the AVX instruction set is available (since Sandy Bridge and Bulldozer) and if the Operating System supports it. If so, it copies an array of doubles with AVX instructions. If not, it uses SSE2 instructions.

The strange thing is: Under Windows 7, SP1 (32-bit), the program output is this:
Quote
AVX support not available:
==========================

X[0] = 1.00     Y[0] = 1.00
X[1] = 2.00     Y[1] = 2.00
X[2] = 3.00     Y[2] = 3.00
X[3] = 4.00     Y[3] = 4.00

Please, press enter to end the application ...


The same program brings under Windows 7, SP1 (64-bit) in the compatibility mode:
Quote
AVX support available:
======================

X[0] = 1.00     Y[0] = 1.00     Z[0] = 1.00
X[1] = 2.00     Y[1] = 2.00     Z[1] = 2.00
X[2] = 3.00     Y[2] = 3.00     Z[2] = 3.00
X[3] = 4.00     Y[3] = 4.00     Z[3] = 4.00

Please, press enter to end the application ...


The compatibility mode seems to be the only chance to use AVX, AVX2 etc. instruction sets. But I can test it only under a virtual machine. What is with a native Win7 (32-bit) with SP1? That must be tested. The C source should be compile after a few minor changes with VC, too. But that must also be tested. Your help and comments are very welcome.

Gunther
You have to know the facts before you can distort them.

jj2007

Hi Gunther,

One question I ask myself (and I can't test it) is what exactly does "supported by the OS" mean?

Can the OS tell the CPU "throw an exception if you hit an AVX instruction", even if the CPU supports it?

Or does it only mean that the AVX registers are not saved during a context switch?

dedndave

the OS can disable many of the newer features via the control registers (ring 0)
because AVX is only supported for 64-bit OS's, i suspect they have been disabled under win7-32

Gunther

Jochen,

Quote from: jj2007 on May 27, 2014, 04:18:41 AM
One question I ask myself (and I can't test it) is what exactly does "supported by the OS" mean?

Can the OS tell the CPU "throw an exception if you hit an AVX instruction", even if the CPU supports it?

Or does it only mean that the AVX registers are not saved during a context switch?

The Intel® Advanced Vector Extensions Programming Reference says that (p. 2-2):
Quote
Prior to using AVX, the application must identify that the operating system supports the XGETBV instruction, the YMM register state, in addition to processor's support for YMM state management using XSAVE/XRSTOR and AVX instructions. The following simplified sequence accomplishes both and is strongly recommended.
1) Detect CPUID.1:ECX.OSXSAVE[bit 27] = 1 (XGETBV enabled for application use1)
2) Issue XGETBV and verify that XFEATURE_ENABLED_MASK[2:1] = '11b' (XMM state and YMM state are enabled by OS).
3) detect CPUID.1:ECX.AVX[bit 28] = 1 (AVX instructions supported).
(Step 3 can be done in any order relative to 1 and 2)

and that (p. 2-3):
Quote
Note: It is unwise for an application to rely exclusively on CPUID.1:ECX.AVX[bit 28] or at all on CPUID.1:ECX.XSAVE[bit 26]: These indicate hardware support but not operating system support. If YMM state management is not enabled by an operating systems, AVX instructions will #UD regardless of CPUID.1:ECX.AVX[bit 28]. "CPUID.1:ECX.XSAVE[bit 26] = 1" does not guarantee the OS actually uses the XSAVE process for state management.
These steps above also apply to enhanced 128-bit SIMD floating-pointing instructions in AVX (using VEX prefix-encoding) that operate on the YMM states.

Dave,

Quote from: dedndave on May 27, 2014, 04:30:46 AM
the OS can disable many of the newer features via the control registers (ring 0)
because AVX is only supported for 64-bit OS's, i suspect they have been disabled under win7-32

but that's not very logical. A 32-bit client can use under a 64-bit OS by using the compatibility mode (no re-compiling necessary) the AVX instructions. The compatibility mode is nearly the same as under a native 32-bit OS. Why these restrictions?

Gunther
You have to know the facts before you can distort them.

Gunther

Jochen,

here's another excerpt from the Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 1: Basic Architecture, p. 13-10.
Quote
Intel AVX instructions comprises of 256-bit and 128-bit instructions that operates on 256-bit YMM registers. The XSAVE feature set allows software to save and restore the state of these registers. See Chapter 13 of the Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 1. System software support requirements for 256-bit YMM states are described next:
For processors that support YMM states, the YMM state exists in all operating modes. However, the available instruction interfaces to access YMM states may vary in different modes. Operating systems must use the XSAVE feature set for YMM state management. The XSAVE feature set also provides flexible and efficient interface to manage XMM/MXCSR states and x87 FPU states in conjunction with newer processor extended states like YMM states. Operating systems may need to be aware of the following when supporting AVX.
• Saving/Restoring AVX state in non-compacted format without SSE state will also save/restore MXCSR even though MXCSR is not part of AVX state. This does not happen when compacted format is used.
• Few AVX instructions such as VZEROUPPER/VZEROALL may operate on future expansion of YMM registers.
An operating system must enable its YMM state management to support AVX and any 256-bit extensions that operate on YMM registers. Otherwise, an attempt to execute an instruction in AVX extensions (including an enhanced 128-bit SIMD instructions using VEX encoding) will cause a #UD exception.
AVX instructions may generate SIMD floating-point exceptions. An OS must enable SIMD floating-point exception support by setting CR4.OSXMMEXCPT[bit 10]=1.

Gunther
You have to know the facts before you can distort them.

jj2007


Gunther

Jochen,

Quote from: jj2007 on May 28, 2014, 04:13:03 AM
Danke :t

you're welcome.

On the other hand, it could be that there is a chance to use AVX and later instruction sets under plain DOS. That would be a joke.

Gunther
You have to know the facts before you can distort them.

dedndave

this code was written by someone who doesn't write ASM very often
but, we should be able to make it work

https://software.intel.com/en-us/blogs/2011/04/14/is-avx-enabled

.686p
.xmm
.model FLAT

; CPUID Win32
.code

; int isAvxSupported();
_isAvxSupported proc
xor eax, eax
cpuid
cmp eax, 1 ; does CPUID support eax = 1?
jb not_supported
mov eax, 1
cpuid
and ecx, 018000000h ;check 27 bit (OS uses XSAVE/XRSTOR)
cmp ecx, 018000000h ; and 28 (AVX supported by CPU)
jne not_supported
xor ecx, ecx ; XFEATURE_ENABLED_MASK/XCR0 register number = 0
xgetbv ; XFEATURE_ENABLED_MASK register is in edx:eax
and eax, 110b
cmp eax, 110b ; check the AVX registers restore at context switch
jne not_supported
mov eax, 1
ret
not_supported:
xor eax, eax
ret
_isAvxSupported endp
END

dedndave

ok - that looks similar to Gunther's check

this page is really confusing me, though

http://msdn.microsoft.com/en-us/library/windows/desktop/ff919571%28v=vs.85%29.aspx

they start out, talking about enabling the features under win7-32 SP1
and, the farther i read, the more confused i am - lol
by the end of it, i go away with the impression that AVX is for debuggers    :dazzled:

Gunther

Dave,

Quote from: dedndave on June 28, 2014, 06:00:35 AM
ok - that looks similar to Gunther's check

this page is really confusing me, though

http://msdn.microsoft.com/en-us/library/windows/desktop/ff919571%28v=vs.85%29.aspx

you can trust me. There's no chance for AVX and better under a 32-bit Operating System.

Gunther
You have to know the facts before you can distort them.

dedndave

it's not that i don't trust you, Gunther   :biggrin:

but, i keep seeing documentation that says otherwise

anta40

Hi Gunther,

I'm running 32-bit Win 7. CPU-Z says that my CPU supports AVX.
Your program seems to run correctly as expected.


dedndave

anta40 - have you installed service pack 1 ?
i am noticing that it is the Ultimate edition

perhaps the edition and SP level have something to do with it

anta40

Yes dave you are right.
32-bit Win 7 with SP 1.

qWord

... so it was the mysterious VM that doe not support AVX ::)


BTW: OllyDbg 2.01 does decode AVX instructions.
BTW2: For the 32- and 16 bit modes the VEX prefix is encoded as an invalid form of the LDS/ES instruction (LDS/ES reg,reg). However, only ymm0-7 can be used in these modes.
MREAL macros - when you need floating point arithmetic while assembling!