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

4

u/Kaisha001 Jan 24 '26

They should! I mean we all make our own but still, it'd be nice if that sort of stuff was just default.

2

u/slicxx Jan 24 '26

I see two very clear problems, first is minor but the second one is i think of more importance.

When we implement vec3 everywhere, people are going to ask for more niche things. I used vec4 and vec5 too in my job, but somewhere is a limit in terms of what a language/library should support.

But what do these types actually do? Exactly nothing. It all boils down to the functions we want to apply to those types. Add? Substract? Easy in 17 dimensions, but how about complex vector funktions? Do we want to do this inplace or return a copy? Will the memory layout be efficient for operation XYZ? Once you put your hands on a vec3/mat type, i bet efficiency is of importance, so it's best to have all the tooling for your use case in one place, without bursting the scope of standard lib.

Some languages offer more, some offer literally nothing and just provide base tooling, because you can build anything anyways with it. Of course it's also design philosophy, but types with 3 or more dimensions are more likely than not an overkill for built-ins

5

u/dcpugalaxy Jan 24 '26

vec2, vec3, and vec4 are reasonable to support. Anything else is unreasonably niche. They aren't useless but anything outside 2/3/4 is niche in comparison. Seems like a pretty clear natural limit to me. I would hardly say vec4/mat4 is niche. They're widely used in computer graphics.

I think the best reason to have them built in is that they're natural vocabulary types. Lots of different libraries and types of libraries want to use and talk about vectors. Having a separate type in each library which are all essentially the same kinda sucks.

5

u/WittyStick Jan 24 '26

I don't think it's unreasonably niche to support what the CPU supports: vec2, vec4, vec8 - for doubles and 64-bit integers and for the smaller types: vec16, vec32 and vec64.

A use case besides geometry is to optimize data structures - and the larger the vector the better.

vec64 of uint8 for example can be used to significantly improve the performance of decoding UTF-8, which is used everywhere - far from niche - but most of the software running on your computer is probably decoding one byte at a time because it's dependant on some low-level string APIs which don't use SIMD.

2

u/[deleted] Jan 25 '26

At this point you're just asking for standard library support for SIMD vectors, which is much more reasonable than spatial linear algebra vectors in my opinion.

3

u/WittyStick Jan 25 '26

Well yeah, if you have SIMD availability provided by the language you can implement linear algebra libraries efficiently.

But I would still recommend providing the basic types for linear algebra in the standard library if only to prevent a proliferation of competing implementations. Advanced linear algebra libraries can build atop the standard lib. I wouldn't expect language authors to implement the equivalent of eigen in their standard library.

2

u/[deleted] Jan 25 '26 edited Jan 25 '26

What's a basic linear algebra type? Are they based on floats or doubles? Are they register-per-vector (i.e. a vec3 is implemented as a 4d SIMD vector), or are they un-padded so they can easily be shipped off to the GPU? Do they use fast "good enough" math that would be ideal for a game, or do they use precise but potentially slow math that would be ideal for simulations? Are you lowering into the closest arch-available intrinsic and potentially have different behavior on different platforms, or do you guarantee the same behavior on all platforms at the cost of not being able to use bare SIMD intrinsics on some platforms in some cases? Do you provide a struct-of-arrays collection to maximize your SIMD potential?

It doesn't really matter what design you pick, because all of them have niches where they shine. If you don't provide all of them, there will be a proliferation of competing implementations regardless. At best you're blessing one design above the others even when it may be inappropriate, at worst you're providing an implementation nobody will use because it tries to be a jack of all trades instead of doing one thing well.

Unfortunately, you can’t standardize your way out of genuine heterogeneity.

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.

→ More replies (5)

1

u/dcpugalaxy Jan 24 '26

I think this is less necessary because these aren't vocabulary types.

1

u/slicxx Jan 24 '26

You got a point with "natural limits" being vec4/mat4 BUT computer graphics underlined my point in being something special and therefore these types may best placed in their own libs. Yes, it's a pain to not have any standards over every package, but a few libs are just "the standard".

Think about the highly optimized package numpy in python. It's not part of the standard library but basically everyone installs numpy whenever they setup their environment because it's so widely used.

A different example would be golang - nothing that is somewhat special is built in and standard packages are also very slim. And then again comes the memory layout problem, vec4 is simple, but a very "standard" implementation will hardly be usable in computer graphics, high performance computing or anything that's outside of standard calculations.

This definitely sucks like you're saying, but i think it would be worse if you have to deal with "standard" types and library types representing the same thing. In many cases standards wouldn't be any more useful than e.g. DTOs, which are already kinda painful in many languages if you can't autogenerate them.

7

u/Kaisha001 Jan 24 '26

The same could be said of any standard library... or even any language. Perhaps we should all be programming in asm?

At some point if something is common and stable enough (ie. there is really only one proper implementation most of the time), then there doesn't seem to be any reason not to support it.

The idea that we can't support vec3/4 because someone might want vec19 seems silly to me.

1

u/arthurno1 Jan 26 '26

The idea that we can't support vec3/4 because someone might want vec19 seems silly to me.

That is exactly the case because game dev is a niche. A chemist would want their own types, and a mechanical engineer would want their own types, and a natural language researcher would want theirs, and everyone would want their own types. Guess what? Sometimes, these types would collide with each other and be similar but slightly different. Whom will you cater?

The answer is exactly: none. You give them tooling so they can introduce they own types as they need them. This talk is by the one of the most famous language designers or our time and deals exactly with these questions. Highly suggested.

1

u/Kaisha001 Jan 26 '26

Whom will you cater?

Well first vec3/4 are not 'gamedev' types, they are linear algebra types. Linear algebra is found everywhere. There's a reason reason why CPUs/GPUs have native linear algebra types and instructions is that is it near ubiquitous across computing.

The answer is exactly: none.

Linear algebra is used far more than any other field of mathematics apart from basic arithmetic. x86 has SSE/AVX, arm has NEON/SVE/SVE2, PowerPC has VMX/VSX, even the old MIPs line of chips and tiny MCUs like the ESP32 S3 (Tensilica Xtensa vector instructions). They're all basically the same thing, because linear algebra is near universal.

Even GPUs, which are used for FAR MORE than just graphics, and their associated languages, which have wildly different architectures, still have vec3/4 ect... as their base types.

For something so universal and stable, I don't see any good reason for languages to not support them.

1

u/arthurno1 Jan 26 '26

Linear algebra is used far more than any other field of mathematics apart from basic arithmetic.

You still haven't had your course on mathematical analysis I see?

x86 has SSE/AVX, arm has NEON/SVE/SVE2, PowerPC has VMX/VSX, even the old MIPs line of chips and tiny MCUs like the ESP32 S3 (Tensilica Xtensa vector instructions). They're all basically the same thing, because linear algebra is near universal.

Intel's SSE/SSE2 came as a direct effort to accelerate computer graphics after their first "multimedia extensions" were not very successful. Later on we have got extensions to those that work with bigger vectors than just 4 dimensional. I am so old that I actually remember when Intel launched them.

They're all basically the same thing, because linear algebra is near universal.

What you seem to confuse is that you equal linear algebra with three and four dimensional vectors. Three and four dimensional vectors and matrices are used in linear transformations and projections, used most famously in CG. In other words it is an application of linear algebra. They are just but a small and specialized subset, and those specialized cpu extensions dealing with 3- and 4-dimensional vectors are there so computers are better equipped with dealing with computer graphics.

There are other areas and other applications of linear algebra where we use other vector dimensions and other tools than linear transformations for projections, rotations etc.

Even GPUs, which are used for FAR MORE than just graphics, and their associated languages, which have wildly different architectures, still have vec3/4 ect... as their base types

GPU started as a tool to accelerate computer graphics and developed from there. Today they can also work with much bigger types than 3- and 4-dimensional vectors. vec3 and vec4 in shader languages and graphics APIs are for the convenience, not because they are basic building stones of gpus.

For something so universal and stable

Three and four dimensional vectors are used mostly in computer graphics, which is just but one area of applied linear algebra. They are not "so universal" as you think. There are other areas of applied lin algebra where we use much bigger vectors than three and four dimensional vectors and matrices.

I don't see any good reason for languages to not support them.

If you actually cared to look at the provided link and video you could perhaps gain some more insight instead of weaving with hands and providing word salad about topics you don't seem to even understand properly.

1

u/Kaisha001 Jan 26 '26

You still haven't had your course on mathematical analysis I see?

And here I thought we were having a discussion... Are there not enough places on reddit to troll that you have to take it here?

What you seem to confuse is that you equal linear algebra with three and four dimensional vectors.

No, it's just that is very common and also easy to implement in hardware. IE. it's the most mature implementation.

GPU started as a tool to accelerate computer graphics and developed from there.

Well aware, but just because they started in graphics, doesn't mean that's where it ended. Why that matters is beyond me.

vec3 and vec4 in shader languages and graphics APIs are for the convenience, not because they are basic building stones of gpus

Not originally. The older GPUs used SIMD. It wasn't till a bit later that NVidia introduced SIMT. But how it's implemented under the hood is secondary to programming languages. The reality is vec3/4 are common, convenient, efficient, mature, and used in many applications beyond 3D graphics. Hence why they are still kept around.

They are not "so universal" as you think.

They're not including 4D SIMD in Arm or Tensilica cores for 3D graphics.

If you actually cared to look at the provided link and video you could perhaps gain some more insight instead of weaving with hands and providing word salad about topics you don't seem to even understand properly.

We've hit peak irony. Coming from 'vec3/4 can only be used for 3D graphics' guy? Perhaps if you made it past grade 10 math you'd realize that linear algebra spans far more than just transforming triangles.

Stupid and toxic... reddit never fails to amuse.

1

u/arthurno1 Jan 26 '26

Are there not enough places on reddit to troll that you have to take it here? Stupid and toxic... reddit never fails to amuse.

Jesus Christ.

1

u/slicxx Jan 24 '26

I see where you're going, but more often than not, things are semi-built-in. The title asks why it's not built-in. I assume my definition of built-in is too literal, meaning usable without import. But standard libs (or standardized libs) do often exist and have big functionality.

I think a great example is the C language here. They want to stay minimal and portable, and of course stay close to the machine, only one hop away from ASM. No dynamic vectors exist, because growth handling, memory and error management, performance and so on do matter but in different ways for different use cases. But a simple "type" is one makro or a few lines of header files.

And for the n-D types? Different types (int, float32/64..) can be easily implemented but they would at least need different paddings inside to be efficient in cache.. A Vector3 is used in a widespread use, but in no way are they universal. They are highly domain specific, so let's just keep it in the math library, game engine or shading language which will hopefully be "optimally" compiled for the use case. And these environments all come with the type.

All about hitting the sweet spot between implementing nothing and delivering it all.

3

u/Kaisha001 Jan 24 '26

I assume my definition of built-in is too literal, meaning usable without import.

Fair enough, it was rather ambiguous. I assumed standard library type stuff.

And for the n-D types? Different types (int, float32/64..) can be easily implemented but they would at least need different paddings inside to be efficient in cache.. A Vector3 is used in a widespread use, but in no way are they universal.

There are SSE/AVX types on near every platform now. It's not an impossible task.

2

u/slicxx Jan 24 '26

You got a very strong point with SSE/AVX, so the reason might just be legacy for many languages nowadays, but it still makes sense for some hard typed languages to focus on the minimum.

Interesting discussion, now i wonder where the limits are and why we aren't pushing further in this direction, especially since a lot of things can be hidden from the user anyways or rather aggressive, maybe be compiled out if it isn't necessary.

1

u/Kaisha001 Jan 24 '26

TBH I'd prefer we just had better compute drivers, so HLSL/GLSL could become generic parallel languages, that could be run on the CPU, or GPU, or some combination.

1

u/slicxx Jan 24 '26

Yeah no way i'd like to touch GLSL when i had other options. In university, we coded our own operating system. This was painful for an average of 7.5 hours a day for about 5 months straight. But it was almost peaceful compared to the time we were forced to learn what is happening behind CUDAs curtains in my masters program. It was then and there, i decided to only do games engineering as a minor instead of my masters subject and just stick with Intelligent Systems because i didn't want to ruin my brain. A friend from uni started to work as a firmware developer for one of nvidias vendor companies... He will retire after roughly 7 years of experience but is about to get his second burnout diagnosis, i can see it in his eyes.

All of that, and we still have no real platform that crosses GPU and CPU universally outside of big data and gaming.

2

u/Kaisha001 Jan 24 '26

All of that, and we still have no real platform that crosses GPU and CPU universally outside of big data and gaming.

It is something I would love to see.

2

u/WittyStick Jan 24 '26

The language only needs to provide sufficient support to leverage the capabilities of the CPU. It doesn't need to handle every case.

That would typically mean providing vectors of each power of 2 up to at least 256-bits (though 512-bits preferable, and 1024-bits is an option), for each of the primitive types: signed/unsigned int8, int16, int32, int64 and single/double floats.

vec3 might also be worthy of inclusion due to widespread use, and could be implemented as vec4 with masking.

For obscure cases like vec5 it obviously makes no sense to include in a general purpose language. It can make sense to expose masking intrinsics so we can use a vec8 with mask 00011111, and wrap this in an API for handling vec5.