r/ProgrammingLanguages • • 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

132 comments sorted by

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 basic vecNf, vecNd, vecNl for N = {2,3,4}, with common operations on them, and a <tgvector.h> providing macros using _Generic over float, double and long double, mirroring how the math libraries in the current standard are implemented.

// <vector.h>

typedef /* unspecified */ vec2f_t;
typedef /* unspecified */ vec2d_t;
typedef /* unspecified */ vec2l_t;
...

vec2f_t vec2f_add(vec2f_t lhs, vec2f_t rhs);
vec2d_t vec2d_add(vec2d_t lhs, vec2d_t rhs);
vec2l_t vec2l_add(vec2l_t lhs, vec2l_t rhs);
...

// <tgvector.h>
#include <vector.h>
#define vec2_add(lhs, rhs) \
    _Generic \
        ( (lhs) \
        , float : vec2f_add \
        , double : vec2d_add \
        , long double : vec2l_add \
        ) (lhs, rhs)
...

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 for matrixMxN for M, 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, 4

namespace std 
{
    template <typename T, int N>
    class vec { ... };

    template <>
    class vec<float, 2> { ... }      //specialization
    ...
}

Some 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.

1

u/TTRoadHog Jan 25 '26

In your example, why not go all the way and define the addition operator, for notational convenience rather than the clunky looking add() function?

1

u/WittyStick Jan 25 '26 edited Jan 25 '26

I was presenting a library solution. You can certainly extend it with language support for the common operators. GCC actually does this for vectors.

However, what does a vec2 * vec2 mean?

It's ambiguous. The "product" of two vectors could mean several things: The direct product, the dot product, the cross product.

In math, and particularly computer geometry where we're most likely to use the types, the dot product is the operation we're going to use most frequently.

But in the case of SIMD operations, the direct (element-wise) product is what we would expect.

So I prefer to not just promote scalar operators to work on vectors to prevent any such confusion.

1

u/[deleted] Jan 25 '26

Using the same arguments, we could say that <complex.h>, <math.h> and <tgmath.h> should not be included in C

I would be inclined to agree, actually. In my view, in 2026, a standard library's main purpose is to provide access to things that users can't implement in the language itself (like primitive types and intrinsic functions), as well as the bare bones necessities that virtually every program will use (like a dynamic array type, an allocator, etc.)

The "batteries included" standard library model, in my opinion, is outdated. Any additional functionality is one package manager command away.

4

u/dcpugalaxy Jan 25 '26

I can't disagree more. But what I find particularly odious about your comment is that it is an example of a trend I see more and more these days. It's not enough to just say you prefer one thing over another. Instead, it's all couched in this almost moralistic language. It's not that you prefer things being left to third party libraries. No, it's "outdated" to have a decent standard library. What complete rubbish.

2

u/[deleted] Jan 25 '26

Instead of engaging with my point (that the language should provide the bare necessities and allow third party libraries to fill in the rest) you just default to name calling. I'm not sure what kind of productive conversation you expect to foster here.

3

u/WittyStick Jan 25 '26 edited Jan 25 '26

I partially agree - a standard library should be fairly minimal - but we can hardly call C's standard library "batteries included". It certainly has its warts and things that could be deprecated, but is otherwise quite minimal. <math.h> provides only the basic operations you'd expect on a calculator - it's certainly not a complete library if you are doing any more advanced numerical computing.

The advantage of these useful common libraries coming with the language is that they're packaged with the compiler so you can expect the library to function on each platform the compiler can. Imagine you were trying to write a portable C program only to require a separate <third-party/math.h> on each platform and had to write a wrapper to handle the varying implementations.

Or imagine if GLSL didn't provide a standard vec2 type and you had to import a different library for each GPU the code might run on. Your game would never ship.

C has the benefit that it's ABI is the operating system's and we can trivially link code written in hand optimized assembly, requiring only a header file to expose functionality - so we can implement any of this without overhead - sometimes faster than we could be writing in C directly (sans non-standard inline assembly).

The way many languages are implemented these days would require an FFI to leverage intrinsics which aren't provided by the language, and would add unnecessary overhead. In these cases we need to provide more built-in functionality for anything performance related.

As a counter point to the minimal standard library - D tried this when it was initially released with phobos as its library. To do anything useful in the language you needed other libraries, and tango ended up competing with phobos because it was more useful, but it kind of split the ecosystem into two, where one set of libraries would depend on phobos and another would depend on tango because they were incompatible. You basically had to pick a side and stick with it. The situation was partially fixed with the release of D version 2 which had a std library that phobos and tango shared, but the damage was already done by then. These days tango and anything built on it are obsolete because phobos added more useful things and people prefer using the library that ships with the compiler.

So we need to strike a balance between minimalist and practical. A completely bare-bones standard library will mean nobody can build reusable libraries in your language because they don't share any common base, as evidenced by D. A package manager ain't going to help if every library you import is built on a different "base" library and they're all incompatible implementations of trivial functionality.

In regards to including vec in C for example, this is somewhat the case. If you're building a game for example, the graphics library, the physics library, the audio library etc you depend on all provide their own vectors. A game engine inevitably has to implement its own vec type which wraps the varying implementations in its dependencies, or pick one and provide conversions to and from the others - which can add unnecessary overhead which diminishes the effort put into making them SIMD-optimized, if at all, and it wastes a ton of developer time. If a standard implementation works 80% of the time and 20% of the time people use an alternative, that's worth doing. On the other hand, if people only used a standard implementation 20% of the time and 80% of the time used an alternative, clearly you wouldn't want that in your standard library.

Perhaps a better approach is where languages offer a tiered standard library, where a core provides the essential features for the language, a base provides a small set of useful features carefully curated by the language author, and extras provides a more optional "batteries included" set of features. In this case the vec types would belong in base, but core would provide the necessary built-in intrinsics to leverage SIMD so that the vectors could be implemented optimally.

With GCC we can use -nostdlib for example to not depend on any standard libraries, or even -ffreestanding to not depend on crt, but we still have to link libgcc.a because compiler can emit code that depends on it.

1

u/flatfinger Jan 26 '26

A well designed standard math library for C should have provided multiple functions to perform various operations, which would treat operand scaling, precision, and corner cases differently. There are some tasks for which a version of sin that performs argument reduction using the mathematical value of pi would be more or less useful than a faster version that might performs argument reduction using any value between (float)pi and pi, or one which uses arguments that are pre-scaled by a factor of 2*pi. Similar principles would apply to log, exponent, and power functions.

If on some platform, a function specified as "for values of x in the range +/-100pi, compute sin(x+episilon) for some epsilon in the range +/- 1E-6" could be much faster and more compact than one that would compute the value of sin(x) precisely, and any epsilon in the range +/-1E-5 would equally satisfy application requirements, an implementation that could let the programmer specify that the faster but imprecise operation would be acceptable could be more useful than one that could only specify the more precise form.