Commit graph

4350 commits

Author SHA1 Message Date
Bill Currie
4952d298ac [qfcc] Fix some memory errors
Found thanks to running valgrind over qfcc through the entire build
whith -DDEBUG_QF_MEMORY.
2023-10-02 21:14:14 +09:00
Bill Currie
471f5488ff [qfcc] Catch some error expressions
Not really enough in general as an internal error still occurs, but the
triggering error is caused by some broken changes.
2023-10-02 14:46:42 +09:00
Bill Currie
261ea0c4de [qfcc] Be more const-correct with expressions
Diagnostics that return an expression now return const, and fixes error
not returning an error expression.
2023-10-02 14:46:39 +09:00
Bill Currie
f3ca0c9222 [qfcc] Dump conflicting type expressions in debug 2023-10-02 12:06:14 +09:00
Bill Currie
b89e243c47 [qfcc] Improve handling of different types in ?: 2023-10-02 09:07:37 +09:00
Bill Currie
2134c85a47 [qfcc] Collect common multiplication terms
This gets my `m * p * ~m` code as optimal as possible if my counting is
correct (this does not include the extra extends and add needed to merge
the values). Also, there might be a possibility of recombining some ops
into a vector op, but I'm happy with this.
2023-10-01 23:17:51 +09:00
Bill Currie
f3edc06c45 [qfcc] Prefer constant scalar mults for collecting
This makes it more likely for a squared scale multiplier remaining
intact, resulting in slightly better code.
2023-10-01 22:31:35 +09:00
Bill Currie
04f49d1ca4 [qfcc] Commit common scale terms
While it works, and does improve the code slightly, it could do better
by favoring constants over variables for the common factor.
2023-10-01 21:53:50 +09:00
Bill Currie
e8521eb4cd [qfcc] Collect like terms into products
That is, `x+x -> 2*x` (and similar for higher counts). Doesn't make much
difference for just 2, but it will make collecting scales easier and I
remember some testing showing that `2*x` is faster than `x+x` for
floating point.

Of course, motor-point keeps bouncing around numerically :/
2023-10-01 19:01:36 +09:00
Bill Currie
96215ed749 [qfcc] Clean up some struct forward declarations
Having to have `struct foo` everywhere gets a bit annoying after a
while.
2023-10-01 17:45:27 +09:00
Bill Currie
ca1b455aa0 [qfcc] Collect common cross product terms
This reduces the number of cross products in `m * p * ~m` from 4 or 5 (4
after the old CSE went through the code) to 2 even before CSE.
2023-10-01 17:32:10 +09:00
Bill Currie
afe6ea526b [qfcc] Give mono-group multivec types structs
This fixes the motor-point.r test (ie, the sub-type field selector works
on mono-group types now). Still need to sort out something for scalars
(but I imagine that can work only in an @algebra context).
2023-09-30 16:33:14 +09:00
Bill Currie
87cf48ffc4 [qfcc] Sort factors in summed terms
This allows them to be matched with cancelling factors. My fancy zero
test is now just that: a fancy zero:

    typedef @algebra(float(3,0,1)) PGA;
    typedef PGA.group_mask(0xa) bivector_t;
    typedef PGA.group_mask(0x1e) motor_t;
    typedef PGA.tvec point_t;
    typedef PGA.vec  plane_t;

    plane_t
    apply_motor (motor_t m, point_t p)
    {
        return (m * p * ~m).vec;
    }

    0000 nop there were plums...
    0001 adjstk 0, 0
    apply_motor:
    motor.r:32:{
    0002 with 2, 0, 1
    motor.r:33:     return (m * p * ~m).vec;
    0003 return (<void>)

The motor-point.r test fails because it uses (m * p * ~m).tvec to get
the value but the type system is slightly broken in that a mono-group
algebra type does not have a structure associated with it and thus the
"missing" field results in 0. Yes, I spent too long chasing that one,
too.
2023-09-30 15:13:14 +09:00
Bill Currie
546253cea7 [qfcc] Add support for associativity
With (not yet hooked up) options for floating point.
2023-09-30 11:06:06 +09:00
Bill Currie
3f40977ecb [qfcc] Handle cancellations in large chains
It turned out I wasn't shuffling canceled terms correctly, thus far too
many hours of bug hunting.
2023-09-29 21:53:56 +09:00
Bill Currie
35ec9062cf [qfcc] Find the dagged expression for scales
I'd missed this in the previous commit, which was a good thing, really,
as it turns out this was the trigger of the bug that causes my fancy
zero test to become non-zero. It seems the bug is in either
component_sum or in the extend merging.
2023-09-29 18:39:33 +09:00
Bill Currie
f1f87527aa [qfcc] Move nested scale handling to distribute_product
Or really, the implementers. This gets my fancy zero test down to just
unrecognized permutations of commutative multiplies and dot products
(with the multiplies above the dot products).
2023-09-29 15:06:07 +09:00
Bill Currie
55ee8562df [qfcc] Use an implementation function for products
Another step towards proper handling of nested scales.
2023-09-29 13:37:41 +09:00
Bill Currie
355d3d76b4 [qfcc] Clean up nested scale handling
This is incomplete in that the handling needs to be moved into
distribute_product, but the infrastructure is there without breaking
anything.
2023-09-29 13:37:41 +09:00
Bill Currie
a21c857579 [qfcc] Correct error messages in pga3d test 2023-09-29 12:50:36 +09:00
Bill Currie
f5ba761c75 [qfcc] Clean up a pile of redundant code
The code was made redundant by distribute_product doing the right thing
for products that can go to 0.
2023-09-29 11:13:49 +09:00
Bill Currie
26dec9ee21 [qfcc] Move product negation into distribute_product
Now all the products are handled consistently, and distribute_product
takes advantage of anti-commutativity.
2023-09-29 11:08:53 +09:00
Bill Currie
14d1148523 [qfcc] Distribute products over sums
While this does "explode" the instruction count (I'll have to collect
like terms later), it does allow for many more opportunities for things
to cancel out to 0 (once (pseudo)commutativity is taken care of).
2023-09-29 10:16:00 +09:00
Bill Currie
2bf855657c [qfcc] Convert dot of scales to scaled dot
That is, dot(scale(A,a),scale(B,b)) -> (a*b)*dot(A,B). Does the right
thing when only one side is a scale. No change to the instruction count
in my fancy zero, but it does open more opportunities when I distribute
products.
2023-09-29 10:16:00 +09:00
Bill Currie
4bf6ce45d4 [qfcc] Convert chained scales to scale by product
That is, scale(scale(A,b),c) becomes scale(A,b*c), thus giving the
expression dag more opportunities to find common sub-expressions. My
fancy zero test is down to 20 total instructions (including overhead, or
16 for the actual algebra).
2023-09-29 10:16:00 +09:00
Bill Currie
bc63f211bb [qfcc] Split up pga3 4-component scales
While splitting up the scaled vector into scaled xyz and scaled w does
cost an extra instruction, it allows for other optimizations to be
applied. For one, extends get all the way to the top now, and there are
at most two (in my test cases), thus either break-even or even a slight
reduction in instruction count. However, in the initial implementation,
I forgot to do the actual scaling, and 12 instructions were removed from
my fancy zero case, but real tests failed :P It looks like it's just
distributivity and commutativity holding things back (eg,
omega*gamma*sigma - gamma*omega*sigma: should be 0, but not recognized
as that).
2023-09-29 10:16:00 +09:00
Bill Currie
8bdeead37f [qfcc] Clean up some whitespace 2023-09-29 10:16:00 +09:00
Bill Currie
83e4b2b7f5 [qfcc] Merge extend expressions in sums
This removes another 3 instructions from the fancy zero test.
2023-09-29 10:16:00 +09:00
Bill Currie
026533d56b [qfcc] Use ex_list_t for multivec components
This fixes the motor test :) It turns out that every lead I had
previously was due to the disabling of that feature "breaking" dags
(such that expressions wouldn't be found) and it was the dagged
multi-vector components getting linked by expr->next that made a mess of
things.
2023-09-29 10:16:00 +09:00
Bill Currie
cf4916e4de [qfcc] Make expression lists more generally usable
That much conflation was a bit excessive.
2023-09-29 10:16:00 +09:00
Bill Currie
210a925be4 [qfcc] Make expressions const-correct
Or at least mostly so (there are a few casts). This doesn't fix the
motor bug, but I've wanted to do this for over twenty years and at least
I know what's not causing the bug. However, disabling fold_constants in
expr_algebra.c does "fix" things, so it's still a good place to look.
2023-09-29 10:15:59 +09:00
Bill Currie
153a19937f [qfcc] Simplify algebraic sums
This doesn't fix the motor bug, but it doesn't make it worse. However,
it does simplify the trees quite a bit, so it should be easier to debug.
It seems the problem has something to do with fold_constants messing up
dagged aliases: in particular, const-folding multiplication by e0123 in
3d PGA as fold_constants sees it as 1.
2023-09-29 10:15:04 +09:00
Bill Currie
198cec6df8 [qfcc] Add a failing test case for dags
I'm not yet sure what went wrong, but the introduction of dags broke
something in my set_transform function (perhaps the dual?), but it's
something to do with the symbol being dagged (I guess because it's
required for everything else to dag). However, the strangest thing is
the error shows up with 155a8cbcda which
is before dags had any direct effect on the geometric algebra code. I
have a sneaking suspicion it's yet another convert_name issue.
2023-09-29 10:15:04 +09:00
Bill Currie
9c258b02e8 [qfcc] Be more cautious with summed extents
They should be treated as such only when merging vector components. This
fixes a bug that doesn't actually exist (it's in experimental code),
where the sum of two 3-component vectors was getting lost.
2023-09-29 10:15:04 +09:00
Bill Currie
c01cbf4fc4 [qfcc] Use distributivity to cancel cross and dot products
For cross products: remove any a from a×(...+/-a...)
For dot products: remove any a×b from a•(...+/-a×b...) (or b×a)

This removed another 2 instructions :)
2023-09-29 10:14:56 +09:00
Bill Currie
7271d2d570 [qfcc] Add flags for commutative and anticommutative
They don't have much effect that I've noticed, but the expression dags
code does check for commutative expressions. The algebra code uses the
anticommutative flag for cross, wedge and subtract (unconditional at
this stage). Integer ops that are commutative are always commutative (or
anticommutative). Floating point ops can be controlled (default to non),
but no way to set the options currently.
2023-09-25 17:26:37 +09:00
Bill Currie
5119237c4c [qfcc] Apply a×a=0
This takes advantage of the expression dag to detect when an expression
is on both sides of a cross product (which always results in 0). This
removes 3 instructions from my motor test (28 to go).
2023-09-25 16:57:15 +09:00
Bill Currie
6d2e3c166d [qfcc] Add GA algebraic expressions to the dags
Again, doesn't affect generated code yet, but it does make it easier to
work with the resulting graphs looking for cancellations.
2023-09-25 16:57:15 +09:00
Bill Currie
155a8cbcda [qfcc] Use dags for many expressions
Especially binary expressions. That expressions can now be reused is
what caused the need to make expression lists non-invasive: the reuse
resulted in loops in the lists. This doesn't directly affect code
generation at this stage but it will help with optimizing algebraic
expressions.

The dags are per sequence point (as per my reading of the C spec).
2023-09-25 16:57:15 +09:00
Bill Currie
d7714eeca7 [qfcc] Disable VM evaluation for non-ruamoko targets
This is really a bandaid for the problem of ruamoko not being an actual
superset of v6 progs (eg, no float | operation). With this, even ctf
builds.
2023-09-25 16:57:15 +09:00
Bill Currie
1e7b1ec86f [qfcc] Fix vector component symbols
The removal of the e tag from expr_t necessitated making convert_name
return a new expressions which resulted in get_type no longer being
enough to both convert a name expression and get the type. This was just
another missed spot. With this, all of game-source except ctf builds.
2023-09-25 16:57:15 +09:00
Bill Currie
79dbc9b866 [qfcc] Add pragma control of code options 2023-09-25 16:57:15 +09:00
Bill Currie
f974192177 [qfcc] Use non-invasive lists for function arguments
This allows expressions to be repeated (by reference) in function
argument lists, which will allow for expression dags.
2023-09-25 16:57:15 +09:00
Bill Currie
81b544c362 [qfcc] Use non-invasive lists for most expressions
This covers attribute params, vector, state, and comma expressions. Just
function args to go, I think.
2023-09-25 16:57:15 +09:00
Bill Currie
cc67e69923 [qfcc] Use non-invasive lists for block expressions
They will be used for other expression types too. Invasive lists make it
difficult to do expression dags.
2023-09-25 16:57:15 +09:00
Bill Currie
345eba45d5 [qfcc] Make the expression union anonymous
Finally, that little e. is cleaned up. convert_name was a bit of a pain
(in that it relied on modifying the expression rather than returning a
new one, or more that such behavior was relied on).
2023-09-23 18:01:49 +09:00
Bill Currie
9af94da151 [qfcc] Rename ex_list_t to ex_boollist_t
I need to create an actual expression list type and want the name.
2023-09-22 20:26:41 +09:00
Bill Currie
1183d44361 [qfcc] Skip dag dependency lines for leaf nodes
Leaf nodes never generate code so showing lines to them only cluttered
the displayed dag (they're still there internally, though).
2023-09-19 11:47:28 +09:00
Bill Currie
1e274b385d [qfcc] Improve handling of negative GA expressions
Sum expressions pull the negation through extend expressions allowing
them to switch to subtraction when appropriate, and offset_cast reaches
past negation to check for extend expressions. This has eliminated all
negation-only instructions from the motor-point, shaving off more
instructions (now 27 not including the return).
2023-09-19 00:47:29 +09:00
Bill Currie
ca01ab2a27 [qfcc] Improve offset_cast's zero detection
I'd caught 0 single components, but I'd missed 0 vector components. This
shaves 4 instructions off the motor-point test.
2023-09-18 21:43:23 +09:00