Author Topic: AVX for 32-bit Windows applications  (Read 32584 times)

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
AVX for 32-bit Windows applications
« on: May 27, 2014, 04:08:26 AM »
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
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 9795
  • Assembler is fun ;-)
    • MasmBasic
Re: AVX for 32-bit Windows applications
« Reply #1 on: May 27, 2014, 04:18:41 AM »
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

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: AVX for 32-bit Windows applications
« Reply #2 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

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: AVX for 32-bit Windows applications
« Reply #3 on: May 27, 2014, 07:03:47 AM »
Jochen,

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,

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
Get your facts first, and then you can distort them.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: AVX for 32-bit Windows applications
« Reply #4 on: May 28, 2014, 04:01:19 AM »
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
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 9795
  • Assembler is fun ;-)
    • MasmBasic
Re: AVX for 32-bit Windows applications
« Reply #5 on: May 28, 2014, 04:13:03 AM »
Danke :t

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: AVX for 32-bit Windows applications
« Reply #6 on: May 28, 2014, 06:04:45 AM »
Jochen,

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
Get your facts first, and then you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: AVX for 32-bit Windows applications
« Reply #7 on: June 28, 2014, 05:49:41 AM »
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

Code: [Select]
.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

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: AVX for 32-bit Windows applications
« Reply #8 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

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

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: AVX for 32-bit Windows applications
« Reply #9 on: June 28, 2014, 06:47:33 PM »
Dave,

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
Get your facts first, and then you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: AVX for 32-bit Windows applications
« Reply #10 on: June 28, 2014, 10:04:22 PM »
it's not that i don't trust you, Gunther   :biggrin:

but, i keep seeing documentation that says otherwise

anta40

  • Member
  • ***
  • Posts: 309
Re: AVX for 32-bit Windows applications
« Reply #11 on: June 29, 2014, 02:15:02 AM »
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

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: AVX for 32-bit Windows applications
« Reply #12 on: June 29, 2014, 02:41:09 AM »
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

  • Member
  • ***
  • Posts: 309
Re: AVX for 32-bit Windows applications
« Reply #13 on: June 29, 2014, 02:53:03 AM »
Yes dave you are right.
32-bit Win 7 with SP 1.

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: AVX for 32-bit Windows applications
« Reply #14 on: June 29, 2014, 03:39:05 AM »
... 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!