Question

I have an array of integers in the [0,5000) range and I want to split them in 5 intervals ([0,1000), [1000,2000), ..., [4000,5000)) in assembly. I used an approach which although correct has room for improvement :

sltiu $t2, $t0, 1000 ; array element in $t0, compare to 1000
;...
bne $t2, $zero, condition ; if $t0 was in the interval done

and going thusly for the rest. This results in, at worst, 5 branches with all kinds of unavoidable stalls. I would like to go for an approach where I would apply a mask to the array element which would result in a different bit pattern depending on if the value belongs to the intervals of interest. Then I would use the value of the bit pattern to jump into an array position (reserved with .space) where I'd put a +1 for the relevant interval (pseudocode):

daddi $v0, $zero, array_of_interval_hits
; loop
xor $t3, $t0, mask   ; $t0 contains the array element
lwu $t4,  array_of_interval_hits($t3)
daddi $t4, $t4, 1
sw $t4, array_of_interval_hits($t3)

Is there such a mask ?

PS : if number >= 5000 or number < 0 I have to (print a message and) exit - using unsigned arithmetics I only care for the case number >= 5000 (negative numbers are big positives in 2's complement)

Was it helpful?

Solution 2

Well I do not think that such a mask exists - if anyone comes up with a solution please post and will accept but closing this for now (for my solution to my programming problem see my comment : Assembly - bit mask for non power of two intervals)

OTHER TIPS

I'm not familiar with mips assembly, so I won't get into specific machine instructions. My thought was to not use a mask, but to just divide (integer divide not floating point divide) your number by 1000. You could use the result 0, 1, 2, ... to determine where to jump.

So for example numbers in the array between 0 - 999 would give a result of 0; array numbers 1000 - 1999 would give a result of 1; etc.

To deal with your PS - Any negative result (assuming signed divide operation) and you could jump to the proper handler. Same with any result of 5 or more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top