How to find the absolute value of an integer in Two's complement in ARM
up vote
2
down vote
favorite
If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.
Thank you
assembly arm twos-complement absolute-value
add a comment |
up vote
2
down vote
favorite
If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.
Thank you
assembly arm twos-complement absolute-value
1
ARM's negate instruction isrsb dst, src, #0
(reverse-subtract). You cantst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizingstd::abs
, but you should try compiling anabs
function with optimization enabled.
– Peter Cordes
Nov 15 at 2:21
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.
Thank you
assembly arm twos-complement absolute-value
If I have -2 (11111111111111111111111111111110), is there a neat ARM instruction or a series of such that will make it (00000000000000000000000000000010). OR or XOR would not work from what I have tried since I loose the 30th bit.
Thank you
assembly arm twos-complement absolute-value
assembly arm twos-complement absolute-value
edited Nov 15 at 15:52
Peter Cordes
117k16178305
117k16178305
asked Nov 15 at 1:59
mdrjjn
437
437
1
ARM's negate instruction isrsb dst, src, #0
(reverse-subtract). You cantst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizingstd::abs
, but you should try compiling anabs
function with optimization enabled.
– Peter Cordes
Nov 15 at 2:21
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48
add a comment |
1
ARM's negate instruction isrsb dst, src, #0
(reverse-subtract). You cantst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizingstd::abs
, but you should try compiling anabs
function with optimization enabled.
– Peter Cordes
Nov 15 at 2:21
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48
1
1
ARM's negate instruction is
rsb dst, src, #0
(reverse-subtract). You can tst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizing std::abs
, but you should try compiling an abs
function with optimization enabled.– Peter Cordes
Nov 15 at 2:21
ARM's negate instruction is
rsb dst, src, #0
(reverse-subtract). You can tst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizing std::abs
, but you should try compiling an abs
function with optimization enabled.– Peter Cordes
Nov 15 at 2:21
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
For finding the absolute value of an integer, use a comparison and a subtraction.
@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can usersbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second thatmvn
stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need anit mi
before thersbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
For finding the absolute value of an integer, use a comparison and a subtraction.
@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can usersbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second thatmvn
stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need anit mi
before thersbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44
add a comment |
up vote
4
down vote
accepted
For finding the absolute value of an integer, use a comparison and a subtraction.
@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can usersbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second thatmvn
stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need anit mi
before thersbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
For finding the absolute value of an integer, use a comparison and a subtraction.
@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0
For finding the absolute value of an integer, use a comparison and a subtraction.
@ input in r0
cmp r0, #0 @ is r0 < 0?
rsbmi r0, r0 #0 @ if yes, r0 = 0 - r0
edited Nov 15 at 14:09
answered Nov 15 at 7:13
fuz
56.8k14124253
56.8k14124253
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can usersbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second thatmvn
stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need anit mi
before thersbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44
add a comment |
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can usersbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second thatmvn
stood for “move negated.” That said, did you get my email?
– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need anit mi
before thersbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…
– cooperised
Nov 15 at 13:44
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
mvn
a bitwise NOT, ones-complement inversion rather than two's complement. infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/…. Like I commented, you can use rsbmi r0, #0
– Peter Cordes
Nov 15 at 11:36
@PeterCordes Clearly you are right. I though for a second that
mvn
stood for “move negated.” That said, did you get my email?– fuz
Nov 15 at 11:39
@PeterCordes Clearly you are right. I though for a second that
mvn
stood for “move negated.” That said, did you get my email?– fuz
Nov 15 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 at 11:44
2
2
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an
it mi
before the rsbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…– cooperised
Nov 15 at 13:44
Worth noting that if you're using an ARMv7 or ARMv8 device, you will be using the Thumb-2 instruction set which does not include conditional execution of non-branch instructions. You will need an
it mi
before the rsbmi
. Many assemblers will insert this for you, though some will warn because you're getting an instruction you didn't explicitly request. See community.arm.com/processors/b/blog/posts/…– cooperised
Nov 15 at 13:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311360%2fhow-to-find-the-absolute-value-of-an-integer-in-twos-complement-in-arm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
ARM's negate instruction is
rsb dst, src, #0
(reverse-subtract). You cantst
/ predicated-rsb
. I think that's what C++ compilers will do when inlining / optimizingstd::abs
, but you should try compiling anabs
function with optimization enabled.– Peter Cordes
Nov 15 at 2:21
which arm instruction set? did you look at NEG? you do need the compare as shown in the checked answer...
– old_timer
Nov 15 at 15:48