Is there a way to implement a portable way of detecting over and underflows in C?
Another thing, my asm code that handles this for now doesnt work either. I cant figure out why not.
asm ("overflow: "
"xorl %eax, %eax \n"
"jno O_Ret \n"
"incl %eax \n"
"O_Ret: ret\n");
nig_t fuck () {
nig_t ass = tits + dick;
if (overflow)
return piss;
if (underflow)
return shit;
return ass;
}
Now if i could figure out a way for this to work it would only work temporary, as i will need a portable way in the future anyway.
Any help would be appreciated.
Name:
VIPPER2012-05-01 13:24
Forgot to say underflow() is just the same as overflow.
>>14
Well he's using ASM so it isn't portable to begin with and he will have to rewrite the ASM for any architechture he wants to port it to anyway.
Name:
Anonymous2012-05-01 16:11
If it is integer math, then test before you do the operation.
So, for example c=a*b can be rewritten as a=c/b.
Say overflow occurs when c>65535, then your test would look like
if (a > 65535/b) then overflow()
There are a couple of extra minor issues, but this is generally the approach.
Another thought occurred to me. In case of say addition, assuming the first operator being positive and second being negative, the result would have to be smaller than the first operator or we know that an underflow happened.
Ofcourse one could apply the same principle to other operations.
Would that thing work too?
Its late and i didnt have such a good week for now. Night /prog/, see ya tomorrow.
Name:
Anonymous2012-05-01 18:57
Signed integer overflow is undefined, so unless every compiler on every architecture agrees on how to handle it you can't do it reliably or portably.
Name:
Anonymous2012-05-01 18:59
>>21
So if you had to make a list of flaws in C this would your number one?
Name:
Anonymous2012-05-01 19:16
You'll have to do the checks before you do the math.
int main(int argc, char **argv) {
unsigned int x = 0xffffffff;
printf("%d\n", x + 2);
return 0;
}
$ gcc test.c -o test
$ ./test
1
IHBT
Name:
Anonymous2012-05-02 15:32
>>35
From ISO/IEC 9899:2011,
``A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.''
It also states that overflow is undefined.
Name:
Anonymous2012-05-02 15:35
>>35,36
Also your example is really bad since you make unhealthy assumptions about the largest value that can be represented by an unsigned int, try using UINT_MAX instead.
IF U WERE KILLED TOMORROW, I WOULDNT GO 2 UR FUNERAL CUZ ID B N JAIL
4 KILLIN DA NIGGA THAT KILLED U!
..... , ,_____________________
..... / `---___________----_____|]
...../_==o;;;;;;;;_______|
.... / ______|
.....), _(__) /
....// (..) ), ----"
...//___//
..//___//
.//___//
WE TRUE HOMIES
WE RIDE TOGETHER
WE DIE TOGETHER
send this GUN to every thread you care about including this one if
you care. C how many times you get this, if you get 13 your A TRUE
HOMIE