Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

c++ - Long long int on 32 bit machines

very simple question, I read that GCC supports long long int type. But how can make math operations with it, when CPU is 32 bit wide only?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The compiler will synthesize math operations (or use function calls) that use more than one CPU instruction to perform the operation. For example, an add operation will add the low order components (the low words) of the long long values and will then take the carry out of that operation and feed it into an add operation on the high order words of the long long.

So the following C code:

long long a;
long long b;
long long c;

// ...
c = a + b;

might be represented by an instruction sequence that looks something like:

mov eax, [a.low]   ; add the low order words
add eax, [b.low]

mov edx, [a.high]  ; add the high order words, 
adc edx, [b.high]  ; including the carry 

mov [c.low], eax
mov [c.high], edx

And if you consider for a moment, compilers for 8 and 16 bits systems had to do this type of thing for 16 and/or 32-bit values long before long long came into being.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...