News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Detailed AVX detection, MASM64 and the future

Started by Gunther, May 12, 2014, 03:15:16 AM

Previous topic - Next topic

Gunther

You'll find the file avx.zip in the attachment of this post. It's a simple test program which tests if AVX is supported by CPU and Operating System. We had a few discussions about that topic for example here, here, or here.

The program makes 3 steps:

  • Checks the AVX support by the CPU.
  • Checks if the Extended State Management by the OS is enabled.
  • Checks if the YMM state is enabled and the OS supports AVX during task switches.
The main program is written in C; gcc will  compile it, but VC should do the job, too. The tests are written in assembly language and jWasm will assemble them, but ml should do the same job. For details, please check the readme.txt and the b_avx.bat.
That's the output under XP:
Quote
                AVX check
                ---------

The CPU supports AVX.
The Operating System hasn't enabled XSETBV/XGETBV instructions.
Operating System doesn't support YMM state.

Please, press enter to end the application ...

There's no support for AVX. That isn't a surprise. The same is true for Windows 7 (32-bit) with SP1:
Quote
                AVX check
                ---------

The CPU supports AVX.
The Operating System hasn't enabled XSETBV/XGETBV instructions.
Operating System doesn't support YMM state.

Please, press enter to end the application ...

The 64-bit version of Windows 7 with SP1 has the full AVX support:
Quote
                AVX check
                ---------

The CPU supports AVX.
The Operating System has enabled XSETBV/XGETBV instructions.
Operating System supports YMM state.

Please, press enter to end the application ...

The successor Windows 8 (64-bit) comes with full AVX support, too:
Quote
                AVX check
                ---------

The CPU supports AVX.
The Operating System has enabled XSETBV/XGETBV instructions.
Operating System supports YMM state.

Please, press enter to end the application ...

It seems that Sinsi is right and the 32-bit Windows doesn't allow AVX. I've observed the same behaviour for Linux. The 64-bit version supports AVX, the 32-bit version doesn't support that instruction set. But I can test all the 32-bit versions only in virtual machines, not in native environments. So, some help from other forum members is necessary.

But if so, that has some consequences. We have already AVX and AVX2. The new AVX512 instruction set with 32 registers, which are 512 bit wide, is announced.

This means that 32-bit software can't use this new instruction sets and will not benefit from them. To ensure the future of assembly language programming under Windows we should discuss a kind of a MASM64 package.

Anyway, some test results under native installations are very welcome. Thank you.

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

habran

Here is a simpleminded  AVX_FME detection from:
    IntelĀ® Architecture Instruction Set Extensions Programming Reference

supports_AVX_FMA PROC
; result in eax
   mov eax, 1
   cpuid
   and ecx, 018001000h
   .if (ecx == 018001000h)  ; check both OSXSAVE and AVX feature flags
     ; processor supports AVX,FMA instructions and XGETBV is enabled by OS
     xor ecx,ecx            ; specify 0 for XFEATURE_ENABLED_MASK register
     xgetbv                 ; result in EDX:EAX
     and eax,6
     .if (ecx == 6)         ; check OS has enabled both XMM and YMM state support
       mov eax,TRUE
     .endif
   .else
     xor eax, eax
   .endif
   ret
supports_AVX_FMA ENDP





Cod-Father

Gunther

Hi habran,

Quote from: habran on November 02, 2014, 03:49:18 PM
Here is a simpleminded  AVX_FME detection from:
    IntelĀ® Architecture Instruction Set Extensions Programming Reference

thank you for sharing the code.

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

habran

It's my pleasure :biggrin:
here is original code:
Quote
INT supports_fma()
{ ; result in eax
mov eax, 1
cpuid
and ecx, 018001000H
cmp ecx, 018001000H; check OSXSAVE, AVX, FMA feature flags
jne not_supported
; processor supports AVX,FMA instructions and XGETBV is enabled by OS
mov ecx, 0; specify 0 for XFEATURE_ENABLED_MASK register
XGETBV; result in EDX:EAX
and eax, 06H
cmp eax, 06H; check OS has enabled both XMM and YMM state support
jne not_supported
mov eax, 1
jmp done
NOT_SUPPORTED:
mov eax, 0
done:
}
Cod-Father

Gunther

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

habran

here is another one:

supports_AVX2 PROC
  ; result in eax
  mov eax, 1
  cpuid
  and ecx, 018000000H
  .if (ecx == 018000000h) ; check both OSXSAVE and AVX feature flags
    ; processor supports AVX instructions and XGETBV is enabled by OS
    mov eax,7
    xor ecx,ecx
    cpuid
    and ebx,20h
    .if (ebx == 20h)     ; check AVX2 feature flags
      xor ecx,ecx        ; specify 0 for XFEATURE_ENABLED_MASK register
      xgetbv               ; result in EDX:EAX
      and eax,6
      .if (eax == 6)       ; check OS has enabled both XMM and YMM state support
        mov eax, 1
      .endif
    .endif
  .endif
  xor eax,eax
  ret
supports_AVX2 ENDP
Cod-Father

Gunther

Hi habran,

thank you. It's for those coders which like that macro coding style.  :t

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

habran

Yes Gunther,
I am also one of them :biggrin:
I find it easy to read and it looks nice and neat like a C
that's why I don't like ml64
NASM,YASM and FASM use [var] for the vars and I find it annoying
I am very happy with JWasm :t
Especially  the version I have enhanced  :bgrin:
Cod-Father

Gunther

Hi habran,

Quote from: habran on November 04, 2014, 05:57:22 AM
I am very happy with JWasm :t
Especially  the version I have enhanced  :bgrin:

of course, jWasm is an excellent assembler. I hope that Andreas will be back soon for further development. But your contribution is always welcome, I think.

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

habran

Hi Gunther,
QuoteI hope that Andreas will be back soon for further development.
Same here
However, if he doesn't return soon I'll be forced to implement AVX2 myself
I am studying his code and the Intel Manual these days :dazzled: 
Cod-Father

Gunther

Hi habran,

Quote from: habran on November 04, 2014, 02:39:32 PM
Same here
However, if he doesn't return soon I'll be forced to implement AVX2 myself
I am studying his code and the Intel Manual these days :dazzled: 

that's good news. It could be a good idea that jWasm has 2 or 3 maintainer. Perhaps qWord can help. You should contact him, because he's an excellent programmer.

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

aw27

Quote from: habran on November 04, 2014, 12:00:34 AM
here is another one:

....
  xor eax,eax
  ret
supports_AVX2 ENDP


So, it will return always false. :icon_rolleyes:

habran

Thanks aw27 :biggrin:
Sorry for late reply, I was busy with HJWasm.
Here is a correct one:

supports_AVX2 PROC FRAME USES ebx
   ; result in eax
  mov eax, 1
  cpuid
  and ecx, 18000000h
  .if (ecx == 18000000h) ; check both OSXSAVE and AVX feature flags
     ; processor supports AVX instructions and XGETBV is enabled by OS
     mov eax, 7
     mov ecx, 0
     cpuid
     and ebx, 20h
     .if (ebx != 20h)    ; check AVX2 feature flags
       jmp not_supported
     .endif
     xor ecx, ecx        ; specify 0 for XFEATURE_ENABLED_MASK register
     xgetbv              ; result in EDX:EAX
     and eax, 6
     .if (eax != 6)      ; check OS has enabled both XMM and YMM state support
       jmp not_supported
     .endif
     mov eax, 1
     jmp done
  .endif
not_supported:
     xor eax,eax
done:  ret
supports_AVX2 ENDP
Cod-Father