Efficient C Programming: Boolean Expressions (Part 1)

Publisher:caoxians4589Latest update time:2021-06-28 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: This chapter will help readers write efficient C code on ARM processors. Some of the techniques involved in this chapter are not only applicable to ARM processors, but also to other RISC processors. (EEFOCUS)

14.4 Boolean Expressions 14.4.1 Range Checks

Boolean expressions are often used to check whether a value is within a specific range. For example, in a graphics window handler, Boolean expressions are often used to determine whether a point on the screen is within the range of the currently active window.


The following program uses a structure to define point coordinates and calculate the current position of the coordinates.


boolPointInRect1(Pointp,Rectangle*r)


{return(px>=r->xmin&&p.xxmax&&


py>=r->ymin&&p.yymax);


}




The above function is compiled into the following instruction sequence.




PointInRect1


LDRa4,[a3,#0]


CMPa1,a4


BLT|L000034.J5.PointInRect1|


LDRa4,[a3,#4]


CMPa4,a1


BLE|L000034.J5.PointInRect1|


LDRa1,[a3,#8]


CMPa2,a1


BLT|L000034.J5.PointInRect1|


LDRa1,[a3,#&c]!


CMPa2,a1


MOVLTa1,#1


MOVLTpc,lr


|L000034.J5.PointInRect1|


MOVa1,#0


MOVpc,lr




But the above code is not the most concise. The compiler's processing of Boolean expressions of the form (x>=min&&x



boolPointInRect2(Pointp,Rectangle*r)


{return((unsigned)(px-r->xmin)xmax&&


(unsigned)(py-r->ymin)ymax);


}




The compiled assembly instruction sequence is as follows.




PointInRect2


LDRa4,[a3,#0]


SUBa1,a1,a4


LDRa4,[a3,#4]


CMPa1,a4


LDRCCa1,[a3,#8]


SUBCCa1,a2,a1


LDRCCa2,[a3,#&c]!


CMPCCa1,a2


MOVCSa1,#0


MOVCCa1,#1


MOVpc,lr


Reference address:Efficient C Programming: Boolean Expressions (Part 1)

Previous article:Application of ARM9 hardware coprocessor on WinCE embedded motherboard
Next article:Arm assembly language calls C functions with parameters and related examples

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号