How to find the absolute value of an integer in Two's complement in ARM
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 |
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 '18 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 '18 at 15:48
add a comment |
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 '18 at 15:52
Peter Cordes
128k18191328
128k18191328
asked Nov 15 '18 at 1:59
mdrjjnmdrjjn
487
487
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 15:48
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 at 13:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 at 13:44
add a comment |
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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 at 13:44
add a comment |
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 '18 at 14:09
answered Nov 15 '18 at 7:13
fuzfuz
58.5k14127257
58.5k14127257
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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 11:39
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
I did, thanks for the reminder. I should have some time to look at that today.
– Peter Cordes
Nov 15 '18 at 11:43
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 at 11:44
@PeterCordes I'm looking forwards to your comments!
– fuz
Nov 15 '18 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 '18 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 '18 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.
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 '18 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 '18 at 15:48