- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
That is called "the hacker mentality". Few hackers are good pedagogues.
Admin
That was in reply to Manni's
Admin
this code won't compile...
Admin
It's the '<' symbols that are making the code not display correctly. "1<<x" and "index < NUMBITS".
Admin
Those "<" were meant to be '<'s. I don't think I've ever managed to post correctly on this forum.
Admin
OMG! It worked!
Admin
Now I see what you guys are starting to complain about this terrible forums software! I edited the post to change the submitters' real name to a psedeonym. And that threw off all the formatting !#$&*!!!
But hey, uhh ... its got a good architecture ....
Admin
Nonsense; haven't you ever heard of 64bit computers? It's not 32 bits here :)
Admin
In Assembly just put the int into a register (eax as an example):
xorl %eax, %eax
ret
Admin
How about flipping the on/off switch? That would be fast!
Admin
Good idea!
I don't know about PC's but atleast when you do that on a C64, only about half of the memory will be filled with 0x00.. the rest will be filled with 0xFF.
(so there'll be something like 0x40 bytes of 0x00's followed by 0x40 bytes of 0xFF's, then 0x40 bytes of 0x00's again... etc etc (if i remember correctly))
But if you can live with this (or if PC's are different) then you've found a constant-time way of doing it even for n bytes! Cool! :)
Hardware-acceleration at it's best!
<font style="color: rgb(128, 128, 128);" size="1">PS. i really do hate the C way of starting hex-numbers with "0x".. but i just did it so you'ld know what i meant..</font>
Admin
On late x86-platforms with paralell-execution, xor is not the recommended way to clear something - mov eax, 0 is, though more complex, quicker.
Pushing them through a nand-gate should do the trick. :D
Admin
As somebody pointed out, this will not compile.
Alex you re-typed it??Admin
aside from the various suggestions here, there is ALWAYS the delphi version, gets the all the bits regardless of the size of the mask :
FillChar(Flag,SizeOf(Flag),#0);
Destination, Size, byte pattern. Obviously, not very efficent for 32 bits or smaller, but since the dest is passed as typeless pointer, and the compiler worries about the data size, the code is safe, 100% efficent and totally safe, and since the COMPILER knows the size of the structure (delphi being a typed language and all), even though the function doesn't do range checking, it is still safe.
I still prefer the old XOR EAX,EAX -> Confusing and obscure, but damn efficent since Intel decided to optimize specifically that opcode for zeroing.
Of course, if you have a REALLY big data structure to move, you could always do something like :
Push ES
Push DS
Pop ES
LEA EAX,[flag]
Mov ESI,EAX
Inc EAX
Mov EDi,EAX
Mov ECX,[Count]
Mov [byte ptr EDI],0
REP MOVSB
Pop ES
true, not as sexy as xor ax,ax, but REP MOVSB can get hardware accerlation flowing. Forgive any rustyness in my x86, its almost been a LONG time (long enough that I can remember accelerating computers by reducing the ram refresh rate)
Admin
There is nothing funky going on here, its a simple unsigned int parameter, so setting it to zero will set all its bits to zero.
Admin
dear God. To program should require a license.
Admin
Hah! It blew up on Alex's face. Let's see if he changes it. [:p]
Admin
I presume this wont compile because INDEX is not declared. If it did compile because it was declared somewhere, it is going to loop forever 'cos index is not getting incremented.
Crazy coding or wot. Just delete the routine, unless you are having to clear a hardware register in which case just setting to zero may not work since when I used to do assembler, clr instructions would do a read-modify-write.
Admin
There are faster implementations using dynamic logic than using the boolean primitives. However, they're still slower than A & ~A. Especially if you're using dynamic logic, you typically already have the A and ~A signals available to you at no cost. Of course, you'd prolly want to use inverted logic, as well.
Admin
Even though I highly doubt that return 0 will compile to something other than returning all bits as 0, Couldn't you do something like this? return 0x0000000000000000;
Admin
The biggest WTF I see is he forgot to add the line:
assert(bitfield == 0);
:)
Admin
You've clearly never written a compiler before.
Admin
hehe.. yeah, coz clearly 0 != 0x0000000000000000 :)
Admin
I think these days the best way to move large amounts of data around is by using the SSE (Streaming SIMD* Extensions) instructions.
* SIMD (Single Instruction Multiple Data)
Admin
What is it with the anonymous cowards posting replies with no context. Who are you referring to? What is your point?
Admin
An anonymous ... oh, no, it was dubwai: "What is it with the anonymous cowards posting replies with no context"
Perhaps Edgar the Virus Hunter (not so anonymous) was expecting the 'Reply' button to add something more than it did. Like some threading to add context.
Admin
Yeah, just like 'Deep Throat' wasn't anonymous.
Admin
It looks a little less anonymous than 'dubwai', at least to me.
Admin
Well, you are entitled to your (wrong) opinion.
Admin
My given name is so common that 'dubwai' is a lot less anonymous, if you must know.
Admin
You see, anyone can post a message as Edgar The Virus Hunter. It's also quite simple to just create a new name when you want to post something and are too cowardly to associate it to your forum persona.
That's what I mean by 'anonymous coward'. There's nothing difficult to understand about it. I don't understand why you are having trouble.
Admin
"Well, you are entitled to your (wrong) opinion"
Looks like we'll continue to differ, since you're obviously so fat-headed that you can't see when I'm so obviously right.
(Note: I'm right. I'm always right. Even Ivanova is wrong when she disagrees with me.)
I was bemused by your getting upset over 'Edgar' in the first place, and somewhat surprised at your categorisation of him as anonymous, when (a) pseudonymous would better fit, and (b) neither you nor I are posting under our real names either.
(I've not checked to see whether I can register an ID as 'dubwai'.)
Admin
No, that's not possible becasue I am always right. Since you disagree with me you are wrong. QED
Perhaps the source of your bewilderment had it's roots in misinterpreting my criticizm as a sign of my being upset.
Well if pedantry is your thing, have at it. I find semantical arguments to be a waste of time.
dubwai is as real a name as any other that I have.
Admin
Truth is the guy gets paid for lines of code, or otherwise has some line count forecast to meet.
(Engineering gets the fun out of programming!)
Admin
You know, that piece of code might be for real...
We don't know the context of the source. Did it come from a custom piece of hardware, where the default state of some part of memory needs to have all bits set to 0, but declaring an integer with a value of 0 on the particular platform, actually had bits set to 1? It might seem weird but it does happen.
Maybe it was the only way of coming up with a guarenteed way of ensuring the particular devices memory was initialised/reset correctly.
BUt if not, a major wt. Probably the worst yet
Admin
Why the hell would you even need a function for this?
Admin
I agree that an integer in C never would (no one would be that daft), but technically it could. There is one odd power of 2, you know. On an architechture where sizeof(int) == sizeof(short) == 1 and CHAR_BIT == 33 ... but, of course, no one would be that daft.
Admin
That's technically true, but in practice there is so much code out there using "xor reg, reg" to zero registers that there is no way Intel would allow any change that caused a performance hit for this case. Just think -- AMD would just have to get the dependency analysis right for "xor eax, eax", and they would gain an advantage over Intel. Can you imagine either one being dumb enough to make such a mistake?
But the larger point is a good one. Whether operation X is a single instruction, whether it's in silicon or microcode, what is a good peephole optimisation -- these are all things that are totally dependent on the architecture, and no general statements can be made.
Admin
What the hell? Even for a WTF, I'm thinking 'WTF? Just XOR it?' Why is this so confusing?
Admin
Well this is just silly. The task can be done in a single line of C: