Skip to content

Conversation

@DarinM223
Copy link

@DarinM223 DarinM223 commented Feb 28, 2025

In the ARM documentation it says that given CSEL Rd, Rn, Rm, cond, Rd = if cond then Rn else Rm, where R is either W or X.

The comparison in FindOldestPerson is cmp w2, w5 which means that it seems to be comparing if w2 > w5 instead of if w5 > w2, and the operands to CSEL are flipped. That means that it sets the oldest person to the current person if the oldest person's age is less than or equal to the current person's age. This looks different from the C ternary operator version below that explains how the instruction works.

This PR modifies the order of the cmp and the operands in csel to more closely match the logic in the equivalent C code.

Here is a sample run of the current FindOldestPerson function. Notice that there are two people with age 4992, but the last one is returned as the oldest person.

4608
4992
8
4864
392
512
520
4872
0
4608
904
4992*
264
4224
4736
904
4488
904
4096
4480

Here is a sample run of the program after the changes in the PR. Now the first person with the oldest age will be returned as the oldest person.

512
4872
0
4616
0
4864
5000*
4864
0
512
5000
0
776
512
128
392
4992
256
4616
264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant