4 ms·
James Hague has a lot more Forth experience than I do. But I would initially write this function something like this: variable v1 variable v2 variable v
by kragen 7y ago
James Hague has a lot more Forth experience than I do. But I would initially write this function something like this:
variable v1 variable v2 variable v3
: vadd v3 ! v2 ! v1 !
3 0 do v1 @ i cells + @ v2 @ i cells + @ + v3 @ i cells + ! loop ;
Or, alternatively:
0 value v1 0 value v2 0 value v3
: vvadd to v3 to v2 to v1
3 0 do v1 i cells + @ v2 i cells + @ + v3 i cells + ! loop ;
I mean, yes, you can make it a puzzle. But you don't have to. These versions will be slower than Hague's first working version and probably the second too, but all of Hague's versions are slower (with any Forth compiler I've used) than what GCC will give you for the C version. And what GCC will give you for the C version, even if it improbably manages to vectorize, is also a lot slower than what you'll get if you do your vec3 math on your GPU. So maybe when it comes time to rewrite the naïve Forth version above, because it is slow, you should just code an SSE assembly routine and skip the manual loop unrolling at the Forth level, or restructure the whole system to do the vector math on the GPU.
Or maybe not. James Hague is a smart guy and he's shipped a lot of code, in assembly, C, and Forth. But I'd like to understand where my reasoning is going wrong, because I can't see it.
My perspective on Forth is that it's the thinnest possible layer on top of raw machine code that gives you all the fundamental mechanisms for improving your leverage. You need structured control flow, so it has IF ELSE THEN, DO LOOP, BEGIN WHILE REPEAT, and a couple of others. You need expressions, so it has an operand stack. You need procedural abstraction, so it has a return stack, which also serves to store loop counters. You need labels both to make your code readable and so that you can patch your code without overwriting parts of it with JMP instructions to wherever you have free space to put the patch code, so it has words. You need metaprogramming, so it has IMMEDIATE. You need run-time polymorphic procedure dispatch, so it has ', EXECUTE, DEFER, IS, and CREATE DOES>. You need namespace management, so new definitions hide old definitions with the same name, and you also have wordlists.
The problem with all these wonderful mechanisms is that they can become an attractive nuisance, as you futz around trying to find the perfect way to express your intent in the smallest, clearest amount of code, navigating the tradeoffs imposed by the sparse toolset. But you don't have to do it that way. You can just write simple, straightforward code that gets the job done with no fuss, and move on to the next problem. Forth lets you do that, too.
That said, I find it easier to get my code working in assembly language than in Forth, and I don't know if that's because I don't know Forth well enough, because Forth is inherently less practical than assembly language, or just because Forth somehow fails to fit my brain. My guess is that I fall for the attractive nuisances too often and waste my time with unproductive futzing. And that's what this blog post looks like to me.
- dwheeler 7y agoThe problem with using global variables (like this) is that it's not thread-safe. Many Forth systems supported multi-threading (each thread gets its own pair of stacks), even on resource-constrained systems. If you're going to have names like this, named parameters AKA local variables are a better solution.
- kragen 7y agoThose aren't global variables; they're just variables. They are statically allocated, which you would think would lead to the problems you describe. But multithreaded Forth systems traditionally use cooperative rather than preemptive multithreading, and since VADD doesn't invoke any words that can yield to another thread, it can be assured that no other instances of VADD will mess with its variables while it's running. So that problem doesn't actually exist unless you're using something like GForth's experimental pthreads interface. In that case you could define the variables as "user variables" (TLS, in more widespread terminology) as well as the possibility of making them stack-allocated variables as you suggest. I like stack-allocated variables, and they'd be the obviously correct solution in a language like C; but ANS Forth doesn't have them (oh, I see they've been added in Forth-2012; thank you!), they make it harder to factor words, and some of the other problems they solve in Algol-family and Lisp-family languages have other solutions in Forth, so the case for them is not as strong. Also, traditionally, multithreaded Forth systems were multiuser; each task had its own dictionary. So a fourth way to avoid the problem (in addition to procedure-local variables, cooperative multitasking, and user variables) is to load the definitions above after spawning the potentially interfering task rather than before.