How to find the absolute value of an integer in Two's complement in ARM










2















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










share|improve this question



















  • 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











  • 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
















2















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










share|improve this question



















  • 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











  • 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














2












2








2








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:52









Peter Cordes

129k18194329




129k18194329










asked Nov 15 '18 at 1:59









mdrjjnmdrjjn

487




487







  • 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











  • 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





    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








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













1 Answer
1






active

oldest

votes


















4














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





share|improve this answer

























  • 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











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











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



);













draft saved

draft discarded


















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









4














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





share|improve this answer

























  • 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











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
















4














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





share|improve this answer

























  • 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











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














4












4








4







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 14:09

























answered Nov 15 '18 at 7:13









fuzfuz

58.5k14127258




58.5k14127258












  • 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











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


















  • 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











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

















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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3