assemblybinaryboolean-logicmips64

Assembly - bit mask for non power of two intervals


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)


Solution

  • 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)