Optimization issues - minimize branches
My goal is to minimize the number of branches (in assembly), which are very costly in our architecture. As a consequence, if I minimize the branches, then I would say that I can minimize the number of cycles, which is the real goal.
An array of 100 elements, which are to be in range [0,4999]. I have to count how many numbers are in [0,999], [1000, 1999], [2000, 2999], [3000, 3999], [4000, 4999]. If the an element is out of bound, print error message.
My current idea is to loop over every element of the array and do this
- If A[i] < 1000 and A[i] >= 0, counter1++ and go to the next element
- If A[i] < 2000, counter2++ and go to the next
- If A[i] < 5000, counter5++ and go to the next
Then instead of checking if an element is >4999, which would be executed as many times as the size of the array, to add the counters and if the result is equal to the size of the array, then we are ok, if not print error message (at least one element was out of range).
What do you say? Any ideas?