News for hackers. Stuff that matters.
Re:How to check if any word is zero by rep_lodsb on Thursday October 20, 2022 @03:57PM (#33279887)
For "reasonably small" number of variables to test, branches are likely to be faster. Especially if zero values are either rare or very common, so the branches end up predictable.

SSE4.1 has packed integer min/max instructions which could well be optimal for testing if a big array contains any zeroes.

also: How to check if any word is non-zero? Simply OR them together!

Re:How to check if any word is zero by tylerhou on Thursday October 20, 2022 @04:06PM (#33279997)
I would expect a small number of variables to fit in cache or registers, so the small extra OOO/data dependency penalty should be much smaller than the cost of a mispredict, no?
Re:How to check if any word is zero by rep_lodsb on Thursday October 20, 2022 @04:12PM (#33280064)
It still has to compare every pair of variables, then do the CMOV (which can't be predicted). Then do the same with the result of the first step, computing the minimum for each pair of pairs. Not sure how many of these the CPU can do in parallel!
Re:How to check if any word is zero by tylerhou on Thursday October 20, 2022 @04:46PM (#33280450)
I don't mean the cmov code, I mean the normal naive testl + sete code. The CPU can do all testl's in parallel and all sete's in parallel. I think this should be faster than branches if you ever have a 0.

I'm assuming your branch code looks something like:

  if (a == 0) {
    return true;
  }
  if (b == 0) {
    return true;
  }
  // ...
  return false;
Re:How to check if any word is zero by drekipus on Thursday October 20, 2022 @04:44PM (#33280412)
You mean AND them together.

OR will give you non-zero If one of the inputs is non-zero.

Re:How to check if any word is zero by pklausler on Thursday October 20, 2022 @08:09PM (#33282543)
Neither of these are correct.
Re:How to check if any word is zero by drekipus on Thursday October 20, 2022 @10:53PM (#33283894)
oh gosh you're right. Why am I getting confused?

I was thinking he wants to check if any of them ARE zero.

OR'ing them together:

    0 || 1 || 0 || 0 = 1
    0 || 0 || 0 || 0 = 0
AND'ing them together:

    1 || 0 || 1 || 1 = 0
    1 || 1 || 1 || 1 = 1