You are not logged in.
Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.
Personally, I'd rather be back in Hobbiton.
Offline
Offline
ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.
Offline
The question now is how to handle it. Thanks for the info, we'll see how it goes.
Personally, I'd rather be back in Hobbiton.
Offline
ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.
Good thing it only applies to cats and not humans, otherwise bad programmers all over the world would have to have this conversation:
Girl: Are you sure it will be ok?
Guy: Don't worry, I know what I'm doing... my integers never overflow. Just relax and trust me.
*fast forward*
Guy: OH FSCK, OH FSCK, OH FSCK!
Girl: WHAT? What happened?
Guy: My program just crashed. I swear this has never happened to me before!
Girl: omg I can't believe I was so stupid. I knew I shouldn't have let you run that. My mother told me this would happen.
Guy: Hey, you wanted to run it just as much as I did. I don't remember you asking to check the source first. You were just as eager to "chmod +x" as I was.
Girl: Whatever. I'm never letting so much as compile code near me ever again.
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
kazuo wrote:ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.
Good thing it only applies to cats and not humans, otherwise bad programmers all over the world would have to have this conversation:
Girl: Are you sure it will be ok?
Guy: Don't worry, I know what I'm doing... my integers never overflow. Just relax and trust me.
Lol!! Hahaha
@Anikom15 You can get the needed info on google, this is a very common topic (its is a FAQ at comp.lang.c FAQ). For example look at http://www.fefe.de/intof.html
EDIT, now re-reading your last message looks like you question now is in a more general sense. You can look at something like http://en.wikipedia.org/wiki/Arbitrary- … arithmetic , but the final solution (bignum, or safe overflow, using bigger types) is per case basis. For example, if you know that you are limited to 64bit sizes you can use it (simple use a 64 bit type) if the needed type is not available you can create then (using for example two smaller types), as I said all depends of the user case.
Last edited by kazuo (2010-06-14 00:40:10)
Offline
Although it would be more portable to check for overflow, almost all CPUs handle overflow in the same way, which is by truncating the result to fit. So if you are looking to just emulate the behaviour of a CPU which does no overflow checking, just AND the result with, say, 0xff. However, some GPUs use saturation arithmetic instead, which is slightly harder to implement.
"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page
Offline
Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.
It actually depends on whether your integers are signed or not. Unsigned integers are supposed to be truncated, throwing away the extra MSBs. In other words, unsigned arithmetic is arithmetic mod 2^n, where n is the number of bits for that particular type. In theory, this is standard across all correct implementations of the C89 standard. In practice, even signed arithmetic behaves similarly, but it's technically undefined behavior.
Offline
Anikom15 wrote:Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.
It actually depends on whether your integers are signed or not. Unsigned integers are supposed to be truncated, throwing away the extra MSBs. In other words, unsigned arithmetic is arithmetic mod 2^n, where n is the number of bits for that particular type. In theory, this is standard across all correct implementations of the C89 standard. In practice, even signed arithmetic behaves similarly, but it's technically undefined behavior.
Can you point me to that paragraph? I believe you, but that would be a useful reference to have. C99 would work equally well.
Edit: never mind, found it myself. 6.2.5p9 in C99.
Last edited by Trent (2010-06-17 20:15:03)
Offline