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

Pages: 1-

Analyzing ReactOS's code

Name: Anonymous 2011-09-05 14:37

I will only cite the following piece of code for dessert:

    #define SWAP(a,b,c)  c = a;\
                         a = b;\
                         a = c

http://www.viva64.com/en/a/0076/

Name: Anonymous 2011-09-05 15:05

NO IKKUSEPUSHOUNS!

Name: Thank you! 2011-09-05 21:20

That was ReactOS Quality!

Name: n3n7i 2011-09-05 21:54

What was that code supposed to do?
...this?

c=a;
a=b;
b=c;?

Name: Anonymous 2011-09-05 23:29

viva64
Not this shit again...

And I bet he'd find just as many errors if he had access to Windows' alpha versions' source code.

Name: Anonymous 2011-09-05 23:29

>>4
Learn C, CRETIN!

Name: Anonymous 2011-09-06 3:25

>>1
What's wrong with:

inline void Swap(int *a, int *b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}

Name: Anonymous 2011-09-06 4:08

>>7
Nothing.

Name: Anonymous 2011-09-06 4:21

>>7
You might want to dereference those pointers

Name: Anonymous 2011-09-06 5:51

>>4
This video accurately reflects what I feel when I read your posts.

http://www.youtube.com/watch?v=EqV1MCDLcMM

Name: Anonymous 2011-09-06 6:07

>>10
Huh, my feelings are closer to this: http://www.youtube.com/watch?v=oHg5SJYRHA0

Name: Anonymous 2011-09-06 6:18

it should be

[code]
do{a^= b;b^=a;a^=b;}while(0)
[\code]

c is a gem, right?

Name: Anonymous 2011-09-06 8:27

>>7
If you have a CPU that was designed during the last twenty years or so, XOR will be slower than using a temporary variable.

Name: Anonymous 2011-09-06 8:30

>>7

xor swap fails if you happen to call it with two pointers to the same address, so it might be good to have


inline void swap(int *a, int *b) {
  if(a != b) {
    *a ^= *b;
    *b ^= *a;
    *a ^= *b;
  }
}


although the check will have a certain amount of cost, and a smart program shouldn't try to swap the contents of a single memory address.

Name: Anonymous 2011-09-06 9:02

>>7,14
won't work with types that can't be xored, fagstrum

Name: Anonymous 2011-09-06 9:22

>>14
>with two pointers to the same address
Swapping same object with itself..why would this be used anyway?

Name: Anonymous 2011-09-06 10:17

>>13
doesn't XOR use the same FU as other basic integer operations? why it would be slower?

Name: Anonymous 2011-09-06 11:21

#define swap(a, b) do { typeof(a) c = a; a = b; b = c; } while (0);

Name: Anonymous 2011-09-06 11:36

>>18
do {} while(0)

WHY?

Name: Anonymous 2011-09-06 11:47

>>18
Because he need to execute several statements at once. do{}while(0) is the most convenient way.

Name: Anonymous 2011-09-06 11:51

>>19
Consider the following:
1.

#define swap(a, b) typeof(a) c = a; a = b; b = c;

int a = 0, b = 4;
if (a > b)
    swap(a, b);


expands to


if (a > b)
    int c = a;
a = b;
b = c; // error: 'c' undefined


2.

#define swap(a, b) typeof(a) c = a; a = b; b = c;

int a = 0, b = 4, c = 25;
swap(a, b);


expands to


int a = 0, b = 4, c = 25;
int c = 0; // error: 'c' already defined
a = b;
b = c;



If you use do-while-0 then it groups all of the statements into one compound-statement which prevents the problems in the examples above from occurring:

#define swap(a, b) do { typeof(a) c = a; a = b; b = c; } while (0)

int a = 0, b = 4, c = 25;

if (a > b)
    swap(a, b);


expands to


int a = 0, b = 4, c = 25;

if (a > b)
    do { int c = a; a = b; b = c; } while (0);
    // It's ok that 'c' was defined twice. The compiler uses the version of 'c'
    // in the inner-most scope (it might give a warning though)


I shouldn't have put a semi-colon on the end in my last post though.

Name: Anonymous 2011-09-06 12:36

>>20
why not { } ?

Name: Anonymous 2011-09-06 12:43

Name: Anonymous 2011-09-06 13:43

CPP needs gensym.

Name: Anonymous 2011-09-06 13:43

Wow, so now /pork/ has degenerated to the point of discussing shit that was covered in the C++ FAQ twenty years ago.  I guess it really is over.

Name: Anonymous 2011-09-06 13:49

>>25
Yes, now go away, faggot lithpfag.

Name: Anonymous 2011-09-06 15:50

>>22
Why not Zoidberg????

Name: Anonymous 2011-09-06 16:37

So why not something like if (1) {}?

Name: Anonymous 2011-09-06 17:37

>>28
because you touch yourself at night

Name: Anonymous 2011-09-06 18:01

>>16

yeah, that is a good point. If a program tries to swap a variable with itself, that is a wasted operation and should be avoided. It's just a subtle difference in the behavior of XOR swap and the other, temporary values swap.

>>15

you could cast the T* to a char* to an array of sizeof(T) chars, and then XOR swap each character. To get better performance, you could use a larger data type, like int or long, but then you'll need to handle if sizeof(T) isn't divisible by sizeof(int) or sizeof(long).


Also, I've heard that using something that the compiler will recognize as a swapping operation is better, as there might be assembly instructions that are intended for swapping memory.

>>28
 
using


#define SWAP(a,b) if(1) {typeof(a) c = a; a = b; b = c;}

would get

[code]
if(condition)
  SWAP(a,b);


to work, but then


if(condition)
  SWAP(a,b);
else
  b = a;


would fail, as it would expand to:


if(condition) {
  if(1) {
    typeof(a) c = a;
    a = b;
    b = c;
  }
  else {
    b = a;
  }
}


and so now the code in the else statement will never be executed, as it is paired with if(1).

the do{}while(0) is one way to do it, and it's pretty popular. If makes the macro look externally like a void procedure.

Name: Anonymous 2011-09-06 18:04

>>30

or am I wrong about casting a pointer to a class T to an array of sizeof(T) chars? It always works with structs in C, but classes have some meta information that you aren't supposed to see as a programmer. Would stuff like the vtable pointer get copied over using that approach? I'm not sure.

Name: Anonymous 2011-09-06 18:38

>>31
meta information like what? I don't think it stores more variables used in class unless you used that 'virtual' shit which you should be staying away at the first place

Name: Anonymous 2011-09-06 19:34

>>17
Because you're introducing unnecessary pipeline hazards. Register renaming hardware allows the extra variable version to run at full speed.

Name: Anonymous 2011-09-06 22:17

>>32

yeah, I was just concerned about the virtual shit.

Name: Anonymous 2011-09-07 11:28

>>25
You're not the only one that feels this way.

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