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

12

u/dcpugalaxy Jan 24 '26

This is very limited. Compilers can't assume your float[4][4] is properly aligned for example. They often can't deal with arrays etc very well because of aliasing. Autovectorisation is very fickle and easy to break.

All serious mathematical libraries have handwritten SIMD. Often assembly but intrinsics are used too.

5

u/alphaglosined Jan 24 '26

Alignment of arrays is a big issue, since you can't really guarantee that at a language level.

But static arrays are different, those can be aligned correctly by the language, with the assumption that their container is also aligned correctly. At least in D this isn't really an issue.

There is restrict annotation that handles aliasing in terms of optimization but that isn't backed by any guarantees.

As for mathematical libraries, things like ffmpeg, they want to codegen out to be the best for a given target, regardless of compiler flags. They use cpuid to pick the function to call. They are a very different beast from something like linear algebra for general usage.

But yeah, if you care about exact codegen, I'd recommend intrinsics instead of writing assembly itself. Intrinsics inline as they don't conflict with other optimisations.

1

u/aerismio Feb 17 '26

OxCaml actually nails this by baking memory layout directly into the type system. You don’t manually handle alignment; if you use a type like float32x4#, the compiler and allocator automatically enforce the 16-byte alignment.

It handles aliasing through uniqueness modes, which is basically a verified, stronger version of C’srestrict forSIMD, it uses explicit intrinsics rather than fickle autovectorization. You write the blend, and you get that exact instruction using unboxed registers. The best part is that the type system checks everything, so it all composes safely without unsafe hacks.