Commit graph

4368 commits

Author SHA1 Message Date
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
Bill Currie
068f685fde [qfcc] Simplify offset_cast for correctness
I don't know why I didn't think to do it this way before, but simply
recursing into each operand for + or - expressions makes it much easier
to generate correct code. Fixes the motor-point test.
2023-09-18 19:36:15 +09:00
Bill Currie
7646f6b51e [qfcc] Add failing test case for motor-point sandwich
This tests a subtle bug where part of the product is dropped on the
floor due to an issue with aliasing through expand expressions.
2023-09-18 19:34:22 +09:00
Bill Currie
ea375a4e2f [qfcc] Fix @dot for vecN types
The change from @dot producing a vector to producing a scalar missed
basic vecN types.
2023-09-14 21:29:59 +09:00
Bill Currie
e535fd51b7 [qfcc] Make passing int constants through ... ok
That is, passing int constants through ... in Ruamoko progs is no longer
a warning (still is for v6p and v6 progs). I got tired of getting the
warning for sizeof expressions when int through ... hasn't been a
problem for even most v6p progs, and was intended to not be a problem
for Ruamoko progs.
2023-09-14 18:33:06 +09:00
Bill Currie
862b2669a5 [qfcc] Ensure progs defs are sorted by address
This ensures they can be found by the progs debug support (since they do
a simple bsearch).
2023-09-12 22:26:30 +09:00
Bill Currie
3f93f6b226 [qfprogs] Create a stack for disassembly of locals
This prevents local variables getting intertwined with globals when
disassembling.
2023-09-12 22:21:55 +09:00
Bill Currie
8ec66f118a [qfcc] Set a flowvar's minimum size based on usage
But really only for memset and memmove because they need to use an int
alias of the variable and it may be only that alias that sets a much
larger variable.
2023-09-11 22:59:40 +09:00
Bill Currie
73d1044bec [qfcc] Support offset aliases of values
But explicitly not for pointers (even an offset of 0 breaks the pointer
relocation).
2023-09-11 22:56:10 +09:00
Bill Currie
b456f936c6 [qfcc] Fix vector expressions for generic vectors
It seems the code was never really tested due to an incorrect progs
version test.
2023-09-11 12:31:08 +09:00
Bill Currie
91c3049348 [qfcc] Ensure scale expressions have the correct type
fold_constants was replacing the pseudoscalar type with a float. I
really need to rewrite that whole block sometime.
2023-09-11 12:30:38 +09:00
Bill Currie
eb9954700a [qfcc] Simplify algebra_field_expr
This removes all the special cases and thus it should be more robust. It
did show up some out-by-one (or a factor of two!) errors elsewhere in
the group mask calculations.
2023-09-11 10:25:35 +09:00
Bill Currie
c066e638e5 [qfcc] Take memset/move size into account for use/def
This fixed the bogus uninitialized variable working in b648c353f
2023-09-11 00:37:53 +09:00
Bill Currie
b648c353ff [qfcc] Add a failing test for bogus uninitialized
The variable is very definitely initialized... by a memset (which is the
problem, actually).
2023-09-11 00:34:15 +09:00
Bill Currie
4bcbfc3473 [qfcc] Fix an uninitialized test
When the function is empty, not even a stack adjust instruction is
emitted, so checking for one wound up accessing uninitialized memory.
2023-09-11 00:32:01 +09:00
Bill Currie
2d7b5c18af [qfcc] Add named types to algebra subtypes
I'm uncertain about the names, but this makes it much easier to get at
specific subtypes (eg, PGA.tvec as a type) rather than having to know
the group mask.
2023-09-11 00:31:27 +09:00
Bill Currie
d387b56f60 [qfcc] Support block initializers for multi-vectors
They're not supported (yet) for single-group multi-vectors, and
designators are required for all initializer elements.
2023-09-11 00:24:50 +09:00
Bill Currie
b35f4c82a4 [qfcc] Implement field access for multi-vector expressions
Of course as an r-value (I think), but it makes getting only the desired
part of a geometric algebra expression much nicer.
2023-09-10 13:13:49 +09:00
Bill Currie
97624b84aa [qfcc] Implement field access to sub-algebra variables
This makes working with the plethora of types a little easier still. The
check for an algebra expression in field_expr needed to be moved up
because the struct code thought the algebra type was a normal vector.
2023-09-10 10:53:59 +09:00
Bill Currie
55fc343383 [qfcc] Allow casting float algebras to double
And vis-versa.

I'm not sure what I was thinking, but I've decided that not being able
to cast the pseudo-scalar from float to double (for printf etc) was a
bug.
2023-09-10 10:49:50 +09:00
Bill Currie
ed824405fe [qfcc] Support struct access to full algebra vectors
This makes them actually useable. Multi-vector expressions and variables
will be done soon.
2023-09-09 23:08:38 +09:00
Bill Currie
7f8cc64720 [qfcc] Clean up algebra type creation
It turned out two structs were being created for the full multi-vector.
Redundant and a potential source of interesting issues in the future.
2023-09-09 22:38:09 +09:00
Bill Currie
2967b97c18 [qfcc] Flip the 2d PGA layout
The makes 2d PGA and 3d VGA layouts consistent.
2023-09-09 21:19:50 +09:00
Bill Currie
be4405485d [qfcc] Implement the rest of 3d VGA
With tests.
2023-09-08 11:15:36 +09:00
Bill Currie
a299cda463 [qfcc] Handle algebra types in block initializers
For now, it's an error (but better than a segfault). I'll sort it out
when I'm happier with the type system.
2023-09-08 11:14:04 +09:00
Bill Currie
51fbdfe87d [qfcc] Merge code blocks more aggressively
The merge_blocks function wasn't reporting whether it had done anything
so the thread/merge/dead blocks loop was bailing early. With this,
simple functions (ie, no control flow) are fully visible to the CSE
optimizer and it can get quite aggressive (removed 3 assignments and a
cross product from my barycenter test code).
2023-09-07 21:42:46 +09:00
Bill Currie
6096edb1ca [qfcc] Implement wedge products for 3d VGA
That was pretty easy, but necessary for trying out barycentric
coordinates in geometric algebra :)
2023-09-07 21:40:17 +09:00
Bill Currie
8dd0410968 [qfcc] Ensure multi-vector objects have the correct type
Failing to promote ints to the algebra type results in a segfault in
assignment of a multi-vector due to the symbol pointer walking off the
end of the list of symbols.
2023-09-04 11:06:28 +09:00
Bill Currie
25e27bba64 [qfcc] Catch simple summed extend assignments
Simple k-vector assignments weren't being checked for summed extends.
This removes a whole pile of instructions from the 2d PGA test.
2023-09-02 21:41:10 +09:00
Bill Currie
5af1bf54f0 [qfcc] Convert summed extends to simple assignments
This knocked off another 6 instructions total :) The remaining
optimizations require better detection of products that unconditionally
produce 0.
2023-09-02 20:35:10 +09:00
Bill Currie
b514bbdb44 [qfcc] Propagate double-negative through addition
And convert addition to subtraction when extend expressions are not
involved. This has taken my little test down to 56 instructions total
(21 for `l p ~l`), down from 74 (39).
2023-09-02 19:43:51 +09:00
Bill Currie
c2472e99fa [qfcc] Merge extend expressions through sums
This takes care of chained sums of extend expressions. Now `l p ~l` has
only four extend instructions which is expected for the code not
detecting the cross product that always produces 0.
2023-09-02 18:46:14 +09:00
Bill Currie
924c13f925 [qfcc] Extract sub-vectors through summed extends
This goes a ways towards minimizing extend expressions, even finding
zeros and thus eliminating whole branches of expressions, but shows the
need for better handling of chained sums of extends.
2023-09-02 17:41:32 +09:00
Bill Currie
f3a1239095 [qfcc] Propagate negations above aliases
No change to the generated code, but this does get negations up as close
to extends (and final sums) as possible.
2023-09-02 13:43:22 +09:00
Bill Currie
4135849903 [qfcc] Apply algebraic rules to products of negatives
Any geometric algebra product of two negatives cancels out the negative,
and if the result is negative (because only one operand was negative),
the negation is migrated to above the operation. This resulted in
removing 2 instructions from one if my mini-tests (went from 74 to 78
with the addition/subtraction change, but this takes it back to 76
instructions).
2023-09-02 13:18:27 +09:00
Bill Currie
74e9b6b7dd [qfcc] Be consistent with summed extends
Summed extend expressions are used for merging a sub-vector with a
scalar. Putting the vector first in the sum will simplify checks later
on (it really doesn't matter which is first so long as it's consistent).
2023-09-02 11:43:49 +09:00
Bill Currie
3e142534c9 [qfcc] Clean up multi-vector addition and subtraction
Subtraction is implemented as adding a negative (with the plan of
optimizing it later). The idea is to give tree inspection and
manipulation a more consistent view without having to worry about
addition vs subtraction.

Negation is moved as high as possible in the expression, but is always
below an extend expression. The plane here is that the manipulation code
can bypass an alias-add-extend combo and see the negation.
2023-09-02 11:18:50 +09:00
Bill Currie
2bb1a0a76e [qfcc] Search for alias nodes
This doesn't affect the generated code (aliases are free), but does
simplify the dag significantly, thus optimizing the compiler somewhat,
but also makes reading dags much easier and therefore optimizing the
debugging process.
2023-09-02 10:43:32 +09:00
Bill Currie
0c6586c753 [qfcc] Always initialize Ruamoko opcodes
They're used for compile-time evaluations regardless of the target.
2023-09-01 14:40:05 +09:00
Bill Currie
cea24af5c4 [qfcc] Clean up default option handling
It turns out the switch to c23 (and using bool) broke the way default
options were handled: -1 just doesn't work.
2023-09-01 14:18:54 +09:00
Bill Currie
d8a78fc849 [qfcc] Handle aliased temps better
Because the aliases were treated as live, every alias of a temp resulted
in an assignment, which proved to be quite significant (4-5 assignments
in some simple GA expressions). By using an alias node in the dag, the
unaliased temp can be marked live while the alias is treated as an
operation rather than an operand. Now my GA expressions have no
superfluous assignments (generally no assignments at all).
2023-09-01 11:59:47 +09:00
Bill Currie
d11f7be6bf [qfcc] Use the new 2d wedge and 2-component swizzles
This cleans up the generated geometric algebra a little bit and, more
importantly, fixes #58.
2023-08-31 20:22:59 +09:00
Bill Currie
a66fb80517 [gamecode] Switch dot products to give a scalar
Making them give a vector was a mistake for Ruamoko. I've probably got a
mess to clean up in game-source, but oh well.
2023-08-31 15:32:52 +09:00
Bill Currie
c9734aa945 [qfcc] Add some basic 3d PGA tests
And fix a few issues that showed up: I'd forgotten to add @regressive
and objects wider than 4 elements need to use the move instructions.
2023-08-30 17:32:02 +09:00
Bill Currie
d6e30538a8 [qfcc] Fix some more multi-vectors type issues
The full multi-vector group type got the wrong type (struct instead of
the wrapping algebra type), and the wrong group was used for v * v.
2023-08-30 15:45:44 +09:00
Bill Currie
1cad355366 [qfcc] Write tests for 3d PGA geometric products
And fix the flood of errors.
2023-08-30 15:00:27 +09:00
Bill Currie
9bce0f8e4f [qfcc] Calculate multi-vector size correctly
The struct for multi-group multi-vectors cleans up that code and makes
the "calculation" much easier (since it's handled by the struct building
code).
2023-08-30 14:58:22 +09:00
Bill Currie
4c808fd254 [qfcc] Allow assignment to superset multi-vectors
If the destination multi-vector fully contains the source multi-vector,
then the assignment is ok (any missing values are zeroed).
2023-08-30 14:56:12 +09:00
Bill Currie
52eaa2fee7 [qfcc] Handle more vector type tests
That new_zero_expr is coming in handy. I should clean up expr_bool.c.
2023-08-30 14:52:38 +09:00
Bill Currie
719cf055a5 [qfcc] Check validity of field expressions
Even though I plan on making multi-vector types easier to use with field
accesses, getting segfaults doesn't help with debugging.
2023-08-29 21:37:41 +09:00
Bill Currie
a01cbd56fd [qfcc] Write tests for 3d PGA dot products
And fix the bugs that were uncovered.
2023-08-29 21:30:18 +09:00
Bill Currie
c42aaea060 [qfcc] Write tests for 3d PGA wedge products
And fix the discovered bugs.
2023-08-29 19:42:00 +09:00
Bill Currie
33295a8ad9 [qfcc] Use structs for multi-vector type layouts
Simple k-vectors don't use structs for their layout since they're just
an array of scalars, but having the structs for group sets or full
multi-vectors makes the system alignment agnostic.
2023-08-29 17:10:29 +09:00
Bill Currie
09cdf87f67 [qfcc] Used aligned type sizes for sizeof
And geometric algebra vectors. This does break things a little in GA,
but it does bring qfcc's C closer to standard C in that sizeof respects
the alignment of the type (very important for arrays).
2023-08-29 13:59:12 +09:00
Bill Currie
ad6b4057f9 [qfcc] Apply member alignment to union size
Seeing a union of size 7 when its members need an alignment of 4 was
rather a concern.
2023-08-29 08:25:19 +09:00
Bill Currie
e4391992cb [qfcc] Remove the internal error for null bool lists
It turns out that both lists can be null when applying short circuit
logic to constants.
2023-08-29 08:06:28 +09:00
Bill Currie
d6ec250897 [qfcc] Fix vector int tests
These too needed a horizontal or.
2023-08-29 08:04:32 +09:00
Bill Currie
327cb88af3 [qfcc] Demote implicit double vector literals correctly
The code for demoting the literals wasn't taking the width into account.
2023-08-29 08:02:11 +09:00
Bill Currie
8de722bfc7 [qfcc] Mark large products as unimplemented
Before I forget and wonder why they're not working.
2023-08-28 20:35:03 +09:00
Bill Currie
58a1758363 [qfcc] Implement the regressive product
That went surprisingly well (but it is built up from other ops, so
pretty easy, really).
2023-08-28 20:35:03 +09:00
Bill Currie
e2d812ab6a [qfcc] Implement multi-vector dual
It's implemented as the Hodge dual, which is probably reasonable until
people complain. Both ⋆ and ! are supported, though the former is a
little hard to see in Consola.
2023-08-28 20:35:03 +09:00
Bill Currie
1329a1c43a [qfcc] Implement multi-vector negation
That was surprisingly harder than expected due to recursion and a
not-so-good implementation in expr_negate (it went too high-level thus
resulting in multivec expressions getting to the code generator).
2023-08-28 17:00:14 +09:00
Bill Currie
6b1684b711 [qfcc] Implement mutli-vector division
But only for scalar divisors. The simple method of AB†/(BB†) works only
if B is a versor and there's also the problem of left and right
division. Thanks to sudgy for making me stop and think before I actually
implemented anything (though he mentioned only that it doesn't work for
general mutli-vector divisors).
2023-08-28 12:17:59 +09:00
Bill Currie
41e0483102 [qfcc] Read float/double vector literals correctly
Wow, I thought that had been tested. I guess that not vary many vector
literals used fractional values.
2023-08-28 12:06:25 +09:00
Bill Currie
5b1ce309ef [qfcc] Implement multi-vector reversion
For a change, something that's actually general rather than specific to
PGA.
2023-08-28 11:56:35 +09:00
Bill Currie
717b09f33e [qfcc] Fix vector long tests
And the discovered typos (yes, I double-checked the values) in the PGA
tests.
2023-08-27 18:40:40 +09:00
Bill Currie
55b7547451 [qfcc] Get the rest of the main 2d PGA products working
That was tedious. I can't say I'm looking forward to writing the tests
for 3d. And even though trivector . bivector and bivector . trivector
give the same answer, they're not really commutative when it comes to
the code.
2023-08-27 15:24:35 +09:00
Bill Currie
5aea74de7e [qfcc] Show algebra signature for bad basis blades
Clear error messages are always more helpful than opaque ones.
2023-08-27 15:17:22 +09:00
Bill Currie
c7d6e80d69 [qfcc] Use GPU-style alignment for algebra types
Meaning vec3 is aligned to 4 components instead of 1. 2-component ops
use vec2 in the VM thus requiring alignment to boundaries of 2, but 4
seems better as it conforms with OpenGL and Vulkan (and, I imagine,
DirectX, but I doubt QF will ever use DirectX).
2023-08-27 15:13:49 +09:00
Bill Currie
74b7e0a35b [qfcc] Get some of 2d PGA mostly working
Better yet, some tests :)
2023-08-27 00:24:23 +09:00
Bill Currie
0939979d84 [qfcc] Allow #pragma to control warning options
I probably need to make a decision about vararg integer constants, but
the ability to control warnings is never a bad thing.
2023-08-26 23:47:00 +09:00
Bill Currie
a5304461dd [qfcc] Fix adjusted swizzles for the same def
The singleton alias resulted in the adjusted swizzles being corrupted
when for the same def. Other than adding properly sized swizzles
(planned), the simplest solution is to (separately) allow alias that
stick out from from the def.
2023-08-26 23:08:55 +09:00
Bill Currie
29a57b7128 [qfcc] Correct handling of 64-bit comparisons
While the progs engine itself implements the instructions correctly, the
opcode specs (and thus qfcc) treated the results as 32-bit (which was,
really, a hidden fixme, it seems).
2023-08-26 23:04:34 +09:00
Bill Currie
91edc5d177 [qfcc] Unalias expressions when checking for math values
Fixes some ICEs when trying to fold constants in geometric algebra.
2023-08-26 11:48:23 +09:00
Bill Currie
77c1b584ea [qfcc] Clean up the blade scaling
I didn't particularly like that solution due to the implied extra
bandwidth (probably should profile such sometime), but I think the
extend operations could be merged into simple assignments by the
optimizer at some stage (or further cleaned up when this stuff gets
moved to actual code gen where it should be).
2023-08-25 22:34:41 +09:00
Bill Currie
60ce4ba8fb [qfcc] Give direct access to algebra subtypes
Currently via only the group mask (which is really horrible to work
with: requires too much knowledge of implementation details, but does
the job for testing), but it got some basics working.
2023-08-25 22:13:54 +09:00
Bill Currie
d2e134cc22 [qfcc] Support multiple parameters for attributes
I have no idea why I threw away all but the *last* parameter (especially
considering nothing used parameters until now).
2023-08-25 22:13:54 +09:00
Bill Currie
b2301c8bad [qfcc] Implement 3d PGA geometric algebra
No clue whether anything is actually correct. Time to start creating
real tests.
2023-08-25 22:13:54 +09:00
Bill Currie
e888f71f8a [qfcc] Fetch sum operand type early
Because component_sum works in-place, using a[i]'s type after setting
c[i] resulting in a null type and thus a segfault.
2023-08-25 22:13:54 +09:00
Bill Currie
a46f9a63d2 [qfcc] Clean up GA sum and scale expressions
Scale was inconsistent and sum was error prone due to using
new_binary_expression raw.
2023-08-25 17:24:47 +09:00
Bill Currie
53d9fee586 [qfcc] Support the extended extend instruction
That... feels odd :)
2023-08-25 17:20:28 +09:00
Bill Currie
55bdcbe4c5 [qfcc] Implement 2d PGA geometric product
This is definitely showing the need for better vector instructions
(mostly a 2d wedge and width dependent swizzles).
2023-08-24 22:07:04 +09:00
Bill Currie
e66ae0aad6 [qfcc] Correct swizzle expressions
It turned out they were always using floats for the source type (meaning
doubles were broken), and not shifting the component in the final sizzle
code meaning all swizzles were ?xxx (neglecting minus or 0). I'd make
tests, but I plan on modifying the instruction set a little bit.
2023-08-24 21:43:09 +09:00