Software Archive
Read-only legacy content
17061 Discussions

How to test a mask register for any non-zeros?

Alastair_M_
New Contributor I
906 Views

Dear all,

I want to test a mask register for any non-zero values.  I am not able to test this on my own MIC for a few days so I decided to ask here if this is possible to do efficiently, i.e. not via memory.

Is _mm512_mask2int() able to load directly into a general purpose register?

If not, can I use _mm512_kortestz() and then check the ZF flag?

Best regards,

Alastair

 

0 Kudos
7 Replies
James_C_Intel2
Employee
906 Views

I think you're looking for the "jknzd" instruction

JKNZD - Jump near if mask is not zero

Description

Checks the value of source mask, and if not all mask bits are set to 0, performs a jump to the target instruction specified by the destination operand. If the condition is not satisfied, the jump is not performed and execution continues with the instruction following the instruction.

See p73 of the Intel® Xeon Phi™ Coprocessor Instruction Set Architecture Reference Manual https://software.intel.com/sites/default/files/forum/278102/327364001en.pdf ;

0 Kudos
Kevin_D_Intel
Employee
906 Views

I also asked Development about your question and they replied:

The user is thinking correctly, intrinsic
extern int       __ICL_INTRINCC _mm512_mask2int (__mmask16);
directly moves a value from mask register to general purpose register using instruction KMOV.

0 Kudos
Alastair_M_
New Contributor I
906 Views

James Cownie (Intel) wrote:

I think you're looking for the "jknzd" instruction

JKNZD - Jump near if mask is not zero

Description

Checks the value of source mask, and if not all mask bits are set to 0, performs a jump to the target instruction specified by the destination operand. If the condition is not satisfied, the jump is not performed and execution continues with the instruction following the instruction.

See p73 of the Intel® Xeon Phi™ Coprocessor Instruction Set Architecture Reference Manual https://software.intel.com/sites/default/files/forum/278102/327364001en.pdf 

Hi James,

Thanks for your response.  Do you know will the compiler generate this instruction if I write something like,

if (mask != 0) { }

Best regards and thanks,

Alastair

0 Kudos
James_C_Intel2
Employee
906 Views

Do you know will the compiler generate this instruction if I write something like, ...

I have no idea, but it's easy enough for you to find out.: "icc -mmic -S ... " and look at the generated code. That way you can also see what's generated for your actual code, rather than a trivial example.

0 Kudos
Alastair_M_
New Contributor I
906 Views

James Cownie (Intel) wrote:

Do you know will the compiler generate this instruction if I write something like, ...

I have no idea, but it's easy enough for you to find out.: "icc -mmic -S ... " and look at the generated code. That way you can also see what's generated for your actual code, rather than a trivial example.

Thanks James,

I haven't actually got access to my development system until next week.  It looks like I can also use the _mm512_mask2int() intrinsic efficiently if necessary.

I will test both approaches and report the results back here. Thanks a lot for your input.

Best regards,

Alastair

0 Kudos
Sylvain_C_
Beginner
906 Views

I had a similar issue. Although I could not get the compiler to generate JK(N)ZD instructions, the best alternative I found so far uses the kortestz intrinsics, just as you suggest in your original post:

if(!_mm512_kortestz(mask, mask)) // Returns true if at least one bit is 1
{}

At least, this avoids the conversion to an integer register.

Please let me know if you find a better answer, though.

0 Kudos
Alastair_M_
New Contributor I
906 Views

Sylvain C. wrote:

I had a similar issue. Although I could not get the compiler to generate JK(N)ZD instructions, the best alternative I found so far uses the kortestz intrinsics, just as you suggest in your original post:

if(!_mm512_kortestz(mask, mask)) // Returns true if at least one bit is 1
{}

At least, this avoids the conversion to an integer register.

Please let me know if you find a better answer, though.

Hi Sylvain,

Thank you very much for your response.  I will try this and come back and mark your answer as best reply if I can get it to work.

Best regards,

Alastair

0 Kudos
Reply