In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the method of merging C++ constants". In daily operation, I believe many people have doubts about the method of merging C++ constants. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the method of merging C++ constants?" Next, please follow the editor to study!
Here is an example of the main function in the APP.cpp file:
Int main () {return 7 + 8;}
First of all, it is important to note:
We will build the program from the command line (not Visual Studio)
We will use Visual Studio 2012. In particular, this version of the compiler produces x64-bit code (rather than the outdated x86 architecture) to be compiled on 64-bit machines.
If you want to continue, please read the instructions. In fact, you just need to select the correct variant from the Visual Studio list.
(note: if you are using a free compiler on Visual Studio Express, it can only run on x86, but it will also generate x64 code smoothly. It is also useful for this experiment. )
We can build the sample program by using the command CL / FA App.cpp. Create an output file with the / FA switch to save the assembly code generated by the compiler, which can be displayed by typing type App.asm:
PUBLIC main _ TEXT SEGMENT main PROC mov eax, 15 ret 0 main ENDP _ TEXT ENDS END
Interestingly, this instruction move ax,15-- only assigns 15 to the register EAX (according to the definition of the x64 call standard, the x64 function will set an int value as the result of the function and return it to the caller). The compiler does not issue the instruction of 7 plus 8 while it is running. It looks like this:
PUBLIC main _ TEXT SEGMENT main PROC mov eax, 7 add eax, 8 ret 0 main ENDP _ TEXT ENDS END
Notice that the ret 0 instruction in these two pieces of code returns control to the caller and pops up 0 bytes from the stack. Don't be misled into returning a value of 0 to the caller! )
I guess you might be thinking: that's great, but what idiot would think of writing a 7-8 operation in the code? Yes, you are right, but the compiler will see such a structure as a macro with side effects. After looking at the following example, you will see that constant merging is a very useful optimization method:
# define SECS_PER_MINUTE 60 # define MINUTES_PER_HOUR 60 # define HOURS_PER_DAY 24 enum Event {Started, Stopped, LostData, ParityError}; struct {int clock_time; enum Event ev; char* reason;} Record; int main () {const int table_size = SECS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY * sizeof Record; / / rest of program}
We want to create a table large enough to hold records for each second, so table_size is the size of the table, expressed in bytes. It is easy to view the assembly instructions for the variable table_size:
Mov DWORD PTR table_size$ [rsp], 1382400; 00151800H
There are no multiplication instructions, 60'60'24'16 '1382400 is calculated at compile time.
In fact, if we look inside the compiler, we will find that the operation of this constant merge is very simple, and it is performed by the front end. It does not require the cumbersome enhancement capabilities of the back-end optimizer. So it always exists. It makes no difference whether you turn on optimization (use / O2) or turn off optimization (/ Od)-the optimization is always performed automatically.
Can we merge constants during compilation, no matter how complex the expression is? In fact, the front end can handle arbitrary constant arithmetic expressions (even the sizeof mentioned above, as long as they can be evaluated at compile time) and operators (+-* /% > + + and -). You can even use Boolean values, logical operators and conditional operators if AND? : .
Is there a time when constant merging requires a back-end optimizer? Of course, take a look at the following example:
Int bump (int n) {return n + 1;} int main () {return 3 + bump (6);}
Enter the command cl / FA / Od App.cpp and you will get the message: cannot be optimized, thank you! Enter App.asm and we will get:
Mov ecx, 6 call? bump@@YAHH@Z; bump add eax, 3
As we expected: ECX saves * parameters 6, according to the x64 calling convention, then calls the bump function, the result is returned to EAX, and then EAX adds 3.
Let's see what happens if we use cl / FA / O2 App.cpp for optimization.
Mov eax,10 at this point, on the "C++ constant merge method is what is" the end of the study, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.