Live data from Hacker News

Zig's new bitCast semantics and LLVM back end improvements

ziglang.org

131–140 of 159 posts

Re: Zig's new bitCast semantics and LLVM back end improvements

#131
post #112

> Consider, for instance, bitcasting a [2]u8 to a u16. Under the old semantics, the result of this operation depends on the target endian: on big-endian targets, the first array element became the 8 most significant bits, whereas on little-endian targets, the first array element became the 8 least significant bits. Under the new semantics, because we only care about logical bit representation (which is endian-agnosti…

I may be damaged from working on IC hardware design and various weird architectures, but I truly can’t comprehend why you’d think this doesn’t make sense. Yeah, if your architecture doesn’t support 24-bit int it maps to 32-bits. But it also declares that the numbers you’re storing should never be larger than 2^24. It’s about type safety, and also run time checks in safe mode I believe. Bitcasting three bytes to a 24-…

If the architecture supports 3 byte types that means it needs to support 3 byte alignments and their powers 9, 27, 81, etc. The easiest way to support this is to always map every 3-byte read operation to two 2-byte reads and then use multiplexers to recombine it into a 24 bit data type.

Of course you could also go crazy and store data in 24 bit blocks in your SRAM. That kind of ruins the 8 bit and 16 bit reads though.

Re: Zig's new bitCast semantics and LLVM back end improvements

#133
post #125

Can I convert a 300-byte message to Base64 with a single instruction? Like: in: [300]u8; out: [800]u3; out = @bitCast(in);

`u3` would be base 8, i.e. octal---I think you meant to use `[400]u6`? Aside from that: I'm not familiar with how standard base64 deals with endianness, so I'm not sure if it would match that, but this `@bitCast` would certainly give you a base64 encoding. But it would probably emit pretty terrible code to do that---our lowering of `@bitCast` isn't really optimized for moving around huge amounts of data in one operat…

> I think you meant to use [400]u6

Of course! I guess it was too early to do the maths correctly... :)

Re: Zig's new bitCast semantics and LLVM back end improvements

#134

Earlier quoted context omitted.

Network byte order has nothing to do with the kernel and you have to care about it It’s a standard because neither side of the connection knows the endianness of the other side so there must be a standard. That standard is big endian regardless of your architecture or kernel or anything else So any serialization intended go over the network should be big endian

right, so a zig app will just do little endian. in the very unlikely event you have it running on a big endian machine you have to do extra work.

And what happens if your zig app happens to be a network driver running on a microcontroller?

Re: Zig's new bitCast semantics and LLVM back end improvements

#135

Earlier quoted context omitted.

@intCast

So, since I don't write Zig I had to go look this up, to save anyone else the bother this is what Rust would call an 'as' cast or C programmers might think of as a value cast, it's going to try to make a value which has a similar meaning but of another type, which may be arbitrarily expensive. What people often want here is a transmute, Rust's core::mem::transmute which changes nothing about the bits except what thos…

There's `@ptrCast` as mentioned in the post, but I agree that it's not optimal. I would really like a `@transmute` builtin.

Alternatively I suppose `extern` unions do that, but again, not quite the same syntactically.

Re: Zig's new bitCast semantics and LLVM back end improvements

#136
post #73
post #67

Earlier quoted context omitted.

It's so interesting to read comments like this and contrast them with the "don't read the code" type of vibes out right now. It feels like half of the developer world is optimizing low-level struct packing and the other half is YOLO'ing 300 KLOC Electron apps. Very confusing.

i think it's perfect: AI allows you to go incredibly deep (you have unlimited access to context to make incredibly impactful surgical changes), or you can go incredibly broad (you have unlimited access to context to tie a mind numbing amount of components together). what shakes out is the middle layer: "infra" between "algorithms" and "product". though, to be fair, the middle layer itself is composed of this same wor…

And why do you mention AI here at all? These statements are ridiculous, world didn't start last year.

Re: Zig's new bitCast semantics and LLVM back end improvements

#137

Earlier quoted context omitted.

Same as it ever was.

yeah, remember those newfangled fancy Node.js guys who would just copy/paste from Stackoverflow without any understanding? Or the Java guys who wrote bloated apps that wasted CPU cycles on garbage collection instead of writing in C++, like God intended? Or the Fortran / Cobol guys who wrote in those God-damned, wasteful, useless high-level languages, instead of using assembly, like a proper programmer should?

Which ones made the money?

Re: Zig's new bitCast semantics and LLVM back end improvements

#138

Earlier quoted context omitted.

right, so a zig app will just do little endian. in the very unlikely event you have it running on a big endian machine you have to do extra work.

You may have never done socket programming, or do you use wrapper libs in Zig? Because you have to send the kernel big endian port numbers for example. What do you do if you program a kernel in Zig, or just generally do low level networking? My point is to refute the statement that everyone has agreed to little endian, and so there aren't use cases to want to do conversion. Programs do not exist in a vacuum, most pro…

Well you would, of course, have a mapping layer between wire types and domain types, like in any good codebase. You do the endianness conversion at that boundary, and then you can just send it out.

Re: Zig's new bitCast semantics and LLVM back end improvements

#139
post #55

Earlier quoted context omitted.

Zig is already great for this with ‘packed struct’ and arbitrary size ints. Allows for very clean protocol creation between systems with known properties. This is another great step in that direction.

you need different packed structs for little- and big-endian data. and casting with little-endian data is a nightmare - you need to reverse-cascade your struct fields to be in accordance with the little-endian bit-pattern. (or have a comptime function that does it for you, of course. but then you lose all declarations for the struct). what should be a simple writing down of a protocol is now a pedantic and error-pron…

Or you just go ahead and forget that big endian ever existed. It's not coming back.

Re: Zig's new bitCast semantics and LLVM back end improvements

#140
post #135

Earlier quoted context omitted.

So, since I don't write Zig I had to go look this up, to save anyone else the bother this is what Rust would call an 'as' cast or C programmers might think of as a value cast, it's going to try to make a value which has a similar meaning but of another type, which may be arbitrarily expensive. What people often want here is a transmute, Rust's core::mem::transmute which changes nothing about the bits except what thos…

There's `@ptrCast` as mentioned in the post, but I agree that it's not optimal. I would really like a `@transmute` builtin. Alternatively I suppose `extern` unions do that, but again, not quite the same syntactically.

For @ptrCast I also now need to care what the language's pointer provenance model is, and AFAICT the answer is Zig didn't get to that yet. If there's some kind of @transmute then we don't need to explain why the pointer cast worked because we never wrote one so that ducks the question, which is simpler.
Post reply on HN