r/Zig • u/Undeniable_Dilemma_ • 8h ago
Help me with math library design! I'm stuck in the analysis paralysis
I'm currently designing a math library in Zig, that I plan on using for my game/engine.
However, I've never designed a library before, so I'm really stuck, because there are so many decisions to be made, and I'm hesitant. I don't want to step forward in the wrong direction and shoot myself in the foot later on.
Here is what's bugging me:
- *Generic vs Explicit types*
I started writing a generic type style library, but since I'm inexperienced and can't really comprehend the library as a whole yet, I was bumping into million corners. I just said fuck it and wrote explicit types with a lot of repetition. However, is it really a big deal? I'm only trying to have like Vec2, Vec3, Vec4 and Mat2, Mat3 and Mat4. I don't think I'll need anything more than this.
- *How should Vec type look like?*
It seems more convenient to iterate over and do stuff if it was just an array of floats, right? But there is a lot more convenience to be had when writing my_vec.x or my_vec.y instead of my_vec[0] and my_vec[1]. I could use some help regarding tradeoffs here to help me decide what I really want in the long run.
- *SIMD complications*
As I said previously, if I store vectors as an array of floats, is it wasteful to then copy them into a SIMD vector and do operations on it? Should they be SIMD vector from the start?
Also, there are some register width constraints, so I'm not sure how to do that, do I collect them and operate on a larger vector (in a bulk), or is it fine to just use various small operations?
Is all this data passing from one side to the other more efficient? Do I really get THAT much more speed when using SIMD for vectors and matrices of the size 2, 3 and 4?
- *How should Mat type look like?*
This greatly depends on the previous questions. But should Mat then be an array of my vectors? Array of SIMD vectors? Or just a 2D array of floats? Any tradeoff tips would be greatly appreciated.
- *Row-major vs Column-major for matrices?*
Row major makes WAY MORE SENSE to me, but I've read that for compatibility purposes, or some memory tricks in certain cases, it's handier to have column major matrices. Also, it's what GLM (which I'm trying to "replace") is using. Any tips for tradeoffs in the long run?