- 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
But you can borrow my belt-sander for your retinas, if you want.
Admin
Ever tried implementing cooperative multitasking using nothing but C?
Admin
Admin
This is possible even in presence of pipelines: the loop hardware feeds the "next" instruction pointer value to the first pipeline stage, so that fetches always come from the right location and "looping" is free, because the pipeline does not have to be cleared. This is in spite of there being (typically) no branch prediction on the same DSPs: a branch opcode is not predicted/anticipated and casuses a pipeline stall, but any hardware-executed looping is "known" in advance and addressed properly at the throat of the pipeline.
Cheers, Kuba
Admin
Of course, some newer architectures are more compiler-friendly, so perhaps any gains he'd get on most modern hardware would be small-ish, but with such long execution times, even a 20% improvement is worth fighting for.
One problem with "high quality" compiler vendors is that they typically cater to Fortran/C/C++ folk. There are no "decent" say Haskell or OCaml compilers out there, even if a typicall Haskell code is usually easier to optimize than C. Heck, if we're into less esoteric, let's take say Python: the only way to get it "optimized" is to use IronPython and have the CLR run its JIT on it. I knowingly exclude research projects like PyPy here.
So, if you're into somehting higher level than C/C++/Fortran, you're facing a shortage of decent compilers with exception of perhaps CLR or Java platforms. Heck, even those don't (AFAIK) really do the vectorization you'd expect for numerical code. As for their scheduling performance on "streaming" code that would nominally benefit from hand-optimization with platform (say Intel) documentation in hand, I'm not impressed with that either.
Cheers, Kuba
Admin
TRWTF is that while is not a function. In Haskell terms, it would be just that, e.g.
(Following Haskell convention, the underscore indicates that the result type is (), i.e. void even though the result type of "action" may not be.)
or, for advanced (ab)users,
Now we can define whileM_ with the slightly cumbersome type
Usage example (assuming stopVar :: Control.Concurrent.MVar.MVar a)
So we reimplemented two important C control flow statements in two lines of Haskell code (not counting the optional explicit type signatures)...
SCNR
Addendum (2009-01-13 03:03): The recursive call to doWhileM is missing an underscore.
Admin
Mcoder wrote: "No optimizer can work well with goto statements"
Are you sure? I seem to recall from that a standard compiler technique was to convert for/while-loops into gotos.
On the other hand, if the gotos are used in complex ways, optimization does become more difficult. But this is not an inherent property of the goto. In fact, I'm writing a compiler for a subset of C, for embedded systems, and it has no problems doing loop invariant hoisting, constant propagation, common subexpression elimination, and other stuff, even when gotos are used.
Admin
Ah yes, but I remember creating a multi-threading framework under DOS using Borland C++ and setjmp/longjmp; you can't do that with your wimpy loops :-).
Admin
6502? Try almost any 8-bits risc processor ever made. I have worked on lots of different micro-processors (basically cpu+memory+simple I/O on one chip), and they al had these short jumps. Usually called short (relative) jumps. rjmp, sjmp, srjmp or simply jmp in their flavor of assembly.
Admin
I think he may have confused goto's with conditional jumps. Optimizing over these is pretty annoying. But certainly not impossible or unheard of. Lint-like systems comes to mind.
Maybe I'm confusing semantic trees with execution-flows. It's been too long since my Compiler Construction classes. :)
Admin
Admin
Isn't it obvious? Management had heard that "goto's are evil", and therefore banned them. Dijkstra never wrote a paper named "longjmp considered harmful", and it wouldn't make as good a soundbyte anyway, so of course longjmp was not banned. It has nothing to do with common sense, it is just rote application of rules.
Admin
Admin
jawdrops.... try reading the error again. What part of "constant" don't you understand?
Admin
Admin
You know, given then number of programmers and managers who have an iron-clad mantra of 'goto is always evil under all circumstances, rewrite this even if it is 10 times more code', I could easliy see this being the case.
Admin
Dijkstra never wrote a paper named "goto considered harmful." What he wrote was "non-local goto considered harmful." He was specifically talking about longjmp-type control flow. For example, using a local goto to simulate a loop will most likely result in equivalent code because the intention is the SAME. These routines (as a old C programmer) do have their uses (error handling that allows you to save out before crashing with a catastrophic failure.) The essential thing is that it is not acceptable for control flow.
Admin
Admin
Admin
I cried because I had no Linux. Then I met a man who had Windows Vista.
Admin
Lots of things that seem obvious turn out to be false when studied scientifically. It's obvious to many people that heavy objects will fall faster than light objects, that mass is independent of speed, or even that the Earth is flat.
Captcha: "nulla". A female null.
Admin
That's what the women are there for.
Admin
20 years ago, longjmp() wasn't that bad, as it only had to save the registers. Of course, it was still horrible in an overall sense, because it just sort of forgot everything that had happened between setjmp() and it.
Now, longjmp() is very slow, as it has to save ALL of the registers, and all of the register-like things (they may be called registers, too; I don't know - I don't really work at this level myself. The point is, there's hundreds, or possibly even thousands, of these things now.) Also note that you've also blown your pipeline, and you may blow significant chunks of cache. (Admittedly, most of that cache contains stuff you're not going to be using anymore, since you're going to be returning across several levels of function calls in any event. But it's still going to be stressful to the system.)
Admin
While the "go to" statement may be considered harmful, the more popular "go from" statement should be considered even more harmful.
The "go from" statement ("gofrom" in short) is written at the destination of the jump rather than at the source, and its argument refers to the place in the code there should be jumped from.
The gofrom-statement is thus not written at the place in the code to be jumped from (as is the case of the goto-statement), but it is written at the code-line to be jumped to. An example of its use:
After the assignment "int a=7" has been executed, the program jumps to the exit(1) statement. The programmer probably intended for a usual infinite loop to occur, but instead he is left with a program returning to the operative system! This is a critical bug that would be hard to detect by just looking at the code.(in this case it is simple enough, but imagine a whole program tangled with gofroms all over the place)
Since gofroms makes it very confusing to analyse the control-flow of the code, i propose that this statement should be removed from all languages (except -perhaps- machine code).
Thank you
Admin
Also, 65xx has an unconditional branch.
Admin
Admin
Well, you can get the same effect by creating a switch statement with every single integer in. And yes, on architectures where assigning an integer needs a temporary register, this will in fact have to generate code for each possibility to avoid breaking the C standard.
I've used setjmp before, but only in programs which were deliberately hard to read; and I tend to restrict uses to returning only 0 or 1, even then.
Admin
So it has already been invented..? :( Damn, i thought i had invented something beautiful and that i was the first to do so :'(
You are right it seems... (Direct wiki link: http://en.wikipedia.org/wiki/COMEFROM)
I will certainly give INTERCAL a shot! I can't believe i have been using inferior languages without this amazingly |3e+ construct until now!
Hopefully this will find its way into the next C or C++ standard. Imagine the fun of maintaining a code-base with gofrom as the main method of transfer of control. Bliss! :))
Admin
Admin
http://www.fortran.com/fortran/come_from.html
Admin
Wouldn't this make more sense if it would say high level language instead of low level language (asm for example is low level)
Admin
No idea what all this "code" stuff is, but longjmp is always used when going to places like Altair IV and Aldebaran, while a simple goto is used in-system. If you're just nudging the ship around in dock, impulse power only - right Scotty?
Admin
I think that in this particular example the compiler can optimize the code A LOT. Has anyone tried to look at the EXE file to see if it really contains JNE/INC etc. loop instructions? Because if you do this in VC6:
int a=0; while(a<50000) a++;
the compiled and optimized output will be: MOV [something],49999
So only one istruction with the RESULT of the loop. I think that the loops in this example could be computed by the compiler itself.
Admin
Exactly, that is (i think) what longjmp is most useful for.
The source code for Lua, the small-and-fast scripting language that is written entirely in ANSI C, uses longjmp in exactly one place -- to jump to an error handler when an error is 'thrown' by calling 'lua_error'.
Admin
Damn. I forgot the quote. I was replying to a post about longjmp being used for doing things like exception handling in pure C.
Admin
Completely Agreed.
Premature optimisation is as embarrasing as premature .....
Admin
Actually a lot of C's function names hearken back to the early days of UNIX where (because of a linker limitation?) only the first six characters were significant. With this in mind, "setjmp" was likely preferable to "setjump" or "set_jump"... think of these being confused with something like "setjumble" or "set_justify", then what would happen if one of these was called instead before a longjmp(). As for names like "creat()" though, even the creators of UNIX admit that if they did it all over again they'd spell things better.
Admin
on my linux machine, the jumping code in fact runs faster.