Author Topic: using the CPU macro  (Read 7027 times)

Offline fullofbugs

  • Jr. Member
  • *
  • Posts: 9
using the CPU macro
« on: May 11, 2018, 06:15:42 PM »
I am writing SIMD functions and want to use the highest SIMD instruction set on the host CPU. So I make the functions jump at their very beginning to different blocks according to the CPU (.SSE, .SSE2, .AVX, etc). The problem is that it is hard for me to remember if an SIMD instruction is supported in an SSE version, so I frequently use unsupported instructions. For example, I may write
Code: [Select]
.SSE2:
        ADDSUBPD XMM0, XMM1
        …
.SSE3
        …
There will not be any problem compiling and linking, but the program will crash on computers that don’t have SSE3 because ADDSUBPD is not available.
To prevent this I use the CPU macro to specify the instruction set.
Code: [Select]
.SSE2:
CPU Willamette
        ADDSUBPD XMM0, XMM1
        …
.SSE3
CPU Prescott
        …
This causes NASM to issue errors on ADDSUBPD. Great! But then I have two problems: 1) CPU accepts CPU id up to Prescott, when I try to use Penryn (SSE41), Nehalem (SSE42) or SANDYBRIDGE (AVX) they are not accepted (Unknown CPU type). In addition, I noticed that all the AVX2 instructions are tagged with AVX2 but no CPU ID.  2) How can I make it back to support all instructions (CPU and CPU all don't work)?

As a suggestion, wouldn't it be nice to allow us to set the highest SIMD instruction set, or allow use to turn on/off a particular set of of instructions? Like
Code: [Select]
InstructionSet AVX2 off
InstructionSet AES off

Thank you for any suggestion and help!

Offline fullofbugs

  • Jr. Member
  • *
  • Posts: 9
Re: using the CPU macro
« Reply #1 on: May 12, 2018, 03:15:46 PM »
I just figured out that any of the following should reset the instruction set settings to default
Code: [Select]
CPU all
CPU any
CPU default
[cpu all]
[cpu any]
[cpu default]
These work on version 2.13.03 but are broken in version 2.14rc0.
However, according to NASM documentation, "All options are case insensitive". This is true for version 2.14rc0 but not true for version 2.13.03. The CPU id must be all lower case. Any of the following cause "unknown cpu type" error.
Code: [Select]
CPU All
CPU Prescott
[cpu Any]
I filed a bug report.

Offline ig

  • Jr. Member
  • *
  • Posts: 12
Re: using the CPU macro
« Reply #2 on: May 14, 2018, 10:16:59 AM »
As a suggestion, wouldn't it be nice to allow us to set the highest SIMD instruction set, or allow use to turn on/off a particular set of of instructions? Like
Code: [Select]
InstructionSet AVX2 off
InstructionSet AES off

That would be very nice, IMHO - especially for the AVX-512 subsets (AVX-512BW, AVX-512DQ, AVX-512VNMI etc.) it can be very confusing to get it right.
However, I don't see the specific information in NASM sources (had just a brief look, so I may have overlooked it for sure) - so it may not be a simple thing to add.

As a suboptimal solution, you could try to run the compiled code under Intel SDE; you can set the required CPU on command-line, run it - and if an instruction unsupported on that platform is executed, you get an error.

Offline fullofbugs

  • Jr. Member
  • *
  • Posts: 9
Re: using the CPU macro
« Reply #3 on: May 16, 2018, 08:38:47 PM »
Quote
As a suboptimal solution, you could try to run the compiled code under Intel SDE; you can set the required CPU on command-line, run it - and if an instruction unsupported on that platform is executed, you get an error.
Thank you for your suggestions! Will give it a try.

Quote
However, I don't see the specific information in NASM sources (had just a brief look, so I may have overlooked it for sure) - so it may not be a simple thing to add.
I believe it can be easily implemented. iflag_set exists in iflags.h and can do it; and the instruction table already has all the information.
I ready hope this can happen!