r/ProgrammingLanguages • u/Luroqa • Jan 24 '26
Discussion Why don't any programming languages have vec3, mat4 or quaternions built in?
Shader languages always do, and they are just heaven to work with. And tasty tasty swizzles, vector.xz = color.rb it's just lovely. Not needing any libraries or operator overloading you know? Are there any big reasons?
110
Upvotes
View all comments
Show parent comment
2
u/WittyStick Jan 25 '26 edited Jan 25 '26
I'm not arguing for a "one size fits all". There's nothing preventing people from implementing their own libraries for specific use-cases.
Using the same arguments, we could say that
<complex.h>,<math.h>and<tgmath.h>should not be included in C - since there are clearly multiple ways we can implement the features they provide. In some cases they're not the right tools and a library may provide alternative implementations, but they work for the majority of simple use-cases.The standard doesn't need to specify how they are implemented, just as it does not for many other things, like the complex numbers. There are many types in the C standard for which does not specify how they are represented but clearly states they are implementation defined. It provides a standard API to access some implementation defined feature - though the standard may place certain constraints or restrictions on the behaviour of that API.
The same is true for shader languages which have these types. They're implementation defined because the different GPUs might have different internal representations.
I'm suggesting, using C as an example, that we should have something like a
<vector.h>, exposing a basicvecNf,vecNd,vecNlforN = {2,3,4}, with common operations on them, and a<tgvector.h>providing macros using_Genericoverfloat,doubleandlong double, mirroring how the math libraries in the current standard are implemented.Similarly, a
<quaternion.h>could provide the equivalent functionality of<complex.h>extended the quaternions, and a<matrix.h>/<tgmatrix.h>could extend the vector library to provide a basic matrix definition formatrixMxNforM, N = { 1, 2, 3, 4 }An implementation should leverage SIMD on targets that support it, but it could use plain old structs or arrays of scalars implementations on those that don't. The library could be entirely optional, included as an annex in the standard like some other standard libraries. If standardized into the language rather than just a library, they would be exposed as
_Vecf2, etc, following the regular C conventions, but they'd still be implementation defined.For languages other than C, the same kind of principle applies but the implementation may differ and would follow the language's own conventions. Eg, for C++ they might be implemented as classes with operator overloading, and the vector size may be a template parameter, but where it is specialized for
N = 2, 3, 4Some languages aren't standardized - they're defined by their one and only implementation. In these cases I'd argue that the author should just pick a suitable implementation that works for common use-cases. If the language is taken seriously it might eventually get a standard which can a provide more relaxed or concrete specification of the behavior.
As for what operations they should provide: Essentially all the scalar operators promoted to element-wise operations on the vectors, plus dot product, etc - in addition to shuffling operations and strided loads/stores. They don't need to expose the entirety of what the platform's SIMD provides, but they should make use of most of it to minimize the necessity of manually writing intrinsics for common use-cases.