Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Interger over/under flow in C?

Name: VIPPER 2012-05-01 13:18

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: VIPPER 2012-05-01 13:24

Forgot to say underflow() is just the same as overflow.

Name: Anonymous 2012-05-01 13:26

Overflow and underflow of what?

Name: VIPPER 2012-05-01 13:28

>>3
Integral types of arbitrary size.

Name: Anonymous 2012-05-01 13:31

if(eax==verybig) then goto as

as:
printf("Ocer flow detedket");
quit Main();

Name: Anonymous 2012-05-01 13:32

>>5
o and #define verybig 4294567138

Name: Anonymous 2012-05-01 13:40

if(rand()==34874)
theres a 51% probility its right
so fuck youd og
fuck you ats
i ma man

Name: Anonymous 2012-05-01 13:48

Why the niggertits don't you just look at the overflow flag that the 8086 processor has?

Name: VIPPER 2012-05-01 14:03

>>5-7
What the hell man!
>>8
Not sure if you are trolling me.

Name: Anonymous 2012-05-01 14:11

i post 10 so i can get dubs after

Name: 10 2012-05-01 14:12

check out my dubs

Name: Anonymous 2012-05-01 14:41

>>1,9
your a fucking retard.

Name: VIPPER 2012-05-01 14:51

>>12
Lol fuck you.

Name: Anonymous 2012-05-01 15:11

>>8
Is not portable.

Name: Anonymous 2012-05-01 15:45

>>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: Anonymous 2012-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.

Name: Anonymous 2012-05-01 16:12

portable assembly

what a fucking idiot

Name: Anonymous 2012-05-01 16:25

And for addition,
if (a > 32767-b) overflow()
a and b being signed. Underflow is different, but based on same principle.

Name: Anonymous 2012-05-01 17:24

>>17
llvm bytecode

not so loud now, huh faggot?

Name: Anonymous 2012-05-01 17:30

>>19
Actually LLVM-IR didn't take calling conventions into account (last time I checked)

Name: Anonymous 2012-05-01 18:13

This might be the biggest flaw in C-language. There's no efficient and portable way to find out if overflow had occured.

Name: VIPPER 2012-05-01 18:36

>>16,18
Thanks, ill try this.

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: Anonymous 2012-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: Anonymous 2012-05-01 18:59

>>21
So if you had to make a list of flaws in C this would your number one?

Name: Anonymous 2012-05-01 19:16

You'll have to do the checks before you do the math.

Name: Anonymous 2012-05-01 20:57

>>24
It would be my number Segmentation fault

Name: Anonymous 2012-05-01 21:09

>>26
Segfaults aren't a flaw, they're a manifestation of C's power.

Name: Anonymous 2012-05-01 23:38

int isAddOverflow(unsigned int a, unsigned int b) {
 return a+b < a || a+b < b;
}


Subtraction is left as an exercise for the reader.

Name: Anonymous 2012-05-02 0:30

>>28
Overflow is undefined, fag.

Name: Anonymous 2012-05-02 3:18

>>29

it's defined for unsigned types.

>>28

that could technically be optimized to a single comparison to either a or b.

let c = a + b.
if there is no overflow, then c >= a and c >= b.
if there is overflow, then c < a and c < b.

Name: Anonymous 2012-05-02 7:29

>>30
it's defined for unsigned types.
It really fucking isn't. Read the standard, dipshit.

Name: Anonymous 2012-05-02 14:23

the prog special olympics

Name: Anonymous 2012-05-02 14:23

also, czech um

Name: Anonymous 2012-05-02 15:14

>>29-31
Overflow can't happen for unsigned integer types.

Name: Anonymous 2012-05-02 15:24

>>34
$ cat > test.c
#include <stdio.h>

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: Anonymous 2012-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: Anonymous 2012-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.

Name: Anonymous 2012-05-02 15:45

overflow my anus

Name: Anonymous 2012-05-02 17:14

>>37
~(type)0 is The Right Way™

Name: Anonymous 2012-05-02 17:29

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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List