Arm Community
Site
Search
User
Site
Search
User
Groups
Arm Research
DesignStart
Education Hub
Graphics and Gaming
High Performance Computing
Innovation
Multimedia
Open Source Software and Platforms
Physical
Processors
Security
System
Software Tools
TrustZone for Armv8-M
中文社区
Blog
Announcements
Artificial Intelligence
Automotive
Healthcare
HPC
Infrastructure
Innovation
Internet of Things
Machine Learning
Mobile
Smart Homes
Wearables
Forums
All developer forums
IP Product forums
Tool & Software forums
Pelion IoT Platform
Support
Open a support case
Documentation
Downloads
Training
Arm Approved program
Arm Design Reviews
Community Help
More
Cancel
Developer Community
IP Products
Processors
Jump...
Cancel
Processors
Classic processors forum
ARM7TDMI: SUBS vs SUB + CMP
Blogs
Forums
Videos & Files
Help
Jump...
Cancel
New
State
Accepted Answer
+1
person also asked this
people also asked this
Replies
7 replies
Subscribers
2 subscribers
Views
7483 views
Users
0 members are here
Arm7
Related
ARM7TDMI: SUBS vs SUB + CMP
Offline
Greger Greger
over 7 years ago
Note: This was originally posted on 2nd April 2009 at
http://forums.arm.com
Hi,
I have a question regarding the SUBS instruction and how it compares to SUB and CMP (due to unexpected behavior in a C-program).
The original C-code goes like this (all variables are 32 bit signed integers):
t = a*b - c*c;
if (t > 0) d = t;
In a particular case (a = 0x80, b = 0x106DCD9, c = 0x4E352501) I get an overflow in a*b as well as in the subtraction. What is puzzling to me is that there's a difference between the the following two assembler versions in a certain simulated environment - and I'm trying to figure out if this is expected or a bug in the simulator:
MUL temp1, c, c
MUL temp2, a, b
SUBS t, temp2, temp1
MOVGT d, t
and
MUL temp1, c, c
MUL temp2, a, b
SUB t, temp2, temp1
CMP t, #0
MOVGT d, t
(These are genereated depending on different compiler settings.)
Any help is greatly appreciated!
Greger
Top replies
Offline
Peter Harris
over 7 years ago
+1
verified
Note: This was originally posted on 6th April 2009 at http://forums.arm.com I'd highly recommend the ARM System Developer's Guide ( http://www.arm.com/documentation/books/4975.html ) - it covers lots...
Parents
0
Offline
Greger Greger
over 7 years ago
Note: This was originally posted on 3rd April 2009 at
http://forums.arm.com
Thank you for your answers!
I realize I gave the wrong numbers. The calculation in question (indeed, the root cause is poor algorithm design) is
t = 0x80 * 0x106DCD9 - 0x8D7F * 0x8D7F
=> t = 0x836E6C80 - 0x4E352501 (=0x3539477F)
(a = 0x80, b = 0x106DCD9, c = 0x8DF, a*b = 0x836E6C80, c*c = 0x4E352501)
So, if I understand you right we should have
MUL temp1, c, c => no overflow flag, no negative flag (this is in bounds)
MUL temp2, a, b => negative flag, no overflow flag (but shouldn't overflow be set since it's a signed multiply?)
SUBS t, temp2, temp1 => no negative flag, overflow flag (underflow)
MOVGT d, t => MOV is not executed
and
MUL temp1, c, c => no overflow flag, no negative flag (this is in bounds)
MUL temp2, a, b => negative flag, no overflow flag (but shouldn't overflow be set since it's a signed multiply?)
SUB t, temp2, temp1
CMP t, #0 => 0x3539477F - 0 => no negative flag, no overflow overflow flag
MOVGT d, t => MOV is executed
OK. Then it is really not a problem with the simulator but that the optimizer takes a legal short cut due to a poorly designed algorithm (which overflows). "Good."
Just a follow-up question here, shouldn't the negative flag be set in the SUBS? I mean we do subtract a positive number from a negative number.
Thanks again!
Greger
Cancel
Up
0
Down
Reply
Accept answer
Cancel
Reply
0
Offline
Greger Greger
over 7 years ago
Note: This was originally posted on 3rd April 2009 at
http://forums.arm.com
Thank you for your answers!
I realize I gave the wrong numbers. The calculation in question (indeed, the root cause is poor algorithm design) is
t = 0x80 * 0x106DCD9 - 0x8D7F * 0x8D7F
=> t = 0x836E6C80 - 0x4E352501 (=0x3539477F)
(a = 0x80, b = 0x106DCD9, c = 0x8DF, a*b = 0x836E6C80, c*c = 0x4E352501)
So, if I understand you right we should have
MUL temp1, c, c => no overflow flag, no negative flag (this is in bounds)
MUL temp2, a, b => negative flag, no overflow flag (but shouldn't overflow be set since it's a signed multiply?)
SUBS t, temp2, temp1 => no negative flag, overflow flag (underflow)
MOVGT d, t => MOV is not executed
and
MUL temp1, c, c => no overflow flag, no negative flag (this is in bounds)
MUL temp2, a, b => negative flag, no overflow flag (but shouldn't overflow be set since it's a signed multiply?)
SUB t, temp2, temp1
CMP t, #0 => 0x3539477F - 0 => no negative flag, no overflow overflow flag
MOVGT d, t => MOV is executed
OK. Then it is really not a problem with the simulator but that the optimizer takes a legal short cut due to a poorly designed algorithm (which overflows). "Good."
Just a follow-up question here, shouldn't the negative flag be set in the SUBS? I mean we do subtract a positive number from a negative number.
Thanks again!
Greger
Cancel
Up
0
Down
Reply
Accept answer
Cancel
Children
No data
More questions in this forum
By title
By date
By reply count
By view count
By most asked
By votes
By quality
Descending
Ascending
All recent questions
Unread questions
Questions you've participated in
Questions you've asked
Unanswered questions
Answered questions
Questions with suggested answers
Questions with no replies
Not Answered
Platform Security Architecture (PSA) and Azure Sphere
0
Platform Security Architecture (PSA)
azure
Internet of Things (IoT)
745
views
0
replies
Started
8 months ago
by
techguyz
Answered
Provision to get out of BKPT instruction
+1
4629
views
5
replies
Latest
8 months ago
by
LULIA
Answered
How to specify virtual Address for pl011 uart in linux kernel
0
APB Peripherals
Arm11
PrimeCell UART (PL011)
Interrupt
12378
views
10
replies
Latest
10 months ago
by
Brayden
Not Answered
Needs explanation of an assembly snippet
0
1566
views
1
reply
Latest
over 1 year ago
by
42Bastian Schick
Not Answered
2515 CAN controller with rpi3
0
1653
views
0
replies
Started
over 1 year ago
by
Tejadeep
<
>
View all questions in Classic processors forum