ChanServ changed the topic of #zig to: zig programming language | https://ziglang.org | be excellent to each other | channel logs: https://irclog.whitequark.org/zig/
lunamn has joined #zig
dingenskirchen has quit [Remote host closed the connection]
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
metaleap has quit [Quit: Leaving]
ur5us has joined #zig
ur5us has quit [Remote host closed the connection]
Aruseus has joined #zig
Aruseus has quit [Client Quit]
Xatenev has left #zig [#zig]
rjtobin has quit [Remote host closed the connection]
benjif has quit [Ping timeout: 240 seconds]
Aruseus has joined #zig
marmotini_ has joined #zig
ur5us has joined #zig
marmotini_ has quit [Ping timeout: 272 seconds]
Aruseus has quit [Quit: Aruseus]
Aruseus has joined #zig
Aruseus has quit [Client Quit]
ur5us has quit [Remote host closed the connection]
ur5us has joined #zig
ur5us has quit [Ping timeout: 248 seconds]
ur5us has joined #zig
ur5us has quit [Client Quit]
waleee-cl has quit [Quit: Connection closed for inactivity]
marmotini_ has joined #zig
marmotini_ has quit [Ping timeout: 260 seconds]
LER0ever has joined #zig
dddddd has quit [Ping timeout: 248 seconds]
_Vi has joined #zig
notjones has joined #zig
marmotini_ has joined #zig
marmotini_ has quit [Ping timeout: 268 seconds]
marmotini_ has joined #zig
<andrewrk> FireFox317, it's going well. I just pushed some prerequisite changes to master. Good night!
marmotini_ has quit [Ping timeout: 240 seconds]
metaleap has joined #zig
_Vi has quit [Ping timeout: 272 seconds]
mforney has quit [Ping timeout: 260 seconds]
mforney has joined #zig
ur5us has joined #zig
FireFox317 has joined #zig
FireFox317 has quit [Remote host closed the connection]
ur5us has quit [Ping timeout: 240 seconds]
jjido has joined #zig
snappy has quit [Quit: WeeChat 2.7]
m6w6 has quit [Quit: https://m6w6.name]
m6w6 has joined #zig
gonz_ has quit [Read error: Connection reset by peer]
gonz_ has joined #zig
<mq32> hey
<mq32> what's the current way of doing type erasure and storing untyped pointers?
<mq32> i remember something with @OpaqueType
dingenskirchen has joined #zig
omglasers has joined #zig
dddddd has joined #zig
<betawaffle> is it possible to write shaders with zig? i see that zig has targets for GPUs
riba has joined #zig
<shakesoda> betawaffle: that would be interesting
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
jjido has joined #zig
riba has quit [Ping timeout: 268 seconds]
<betawaffle> i wish i could attach methods to an opaque type
<betawaffle> i mean... i suppose this is the same complaint as not having newtype
LER0ever has quit [Remote host closed the connection]
_Vi has joined #zig
omglasers has quit [Read error: Connection reset by peer]
decentpenguin has joined #zig
<Snektron> I doubt it would be efficient to target amdgpu
Xatenev has joined #zig
<andrewrk> betawaffle, SPIR-V is a planned target of self-hosted
<andrewrk> llvm does not provide it
<betawaffle> what's that?
<andrewrk> shader language. https://www.khronos.org/spir/
<betawaffle> heh, if only the whole industry supported the "industry standards"
<metaleap> andrewrk: on zig/ast.Location is the `column` a byte-offset-based count or utf-8-based / "editor-like" (aka every char >127 still represents 1 column)?
<metaleap> (and every multibyte "char"/rune too)
<andrewrk> column is bytes
<metaleap> andrewrk thx
<andrewrk> note that zig has line-independent tokenization
<andrewrk> so an editor will only have to be unicode-aware starting from column 0 of that line
<andrewrk> also if there are no string literals or @"identifiers" on a line, it's currently guaranteed to be ascii
<Xatenev> any possibility to call `zig build-exe` with the executable name? cant find it
<andrewrk> are you looking for `--name` ?
<metaleap> andrewrk understood, i already found the std.unicode.utf8* stuff so should be able to reconstruct editor-position-info from `Location` and vice-versa
<Xatenev> andrewrk: oh my bad, thanks! i found it now in the compile options too :)
<Xatenev> that was it.
<Xatenev> one thing I wonder, im experimenting a bit with `comptime` - when I have a basic function fn add(x: i64, y: i64) i64 { return x + y; } and then call it via comptime: const len = comptime add(1,2); const arr: [len]u8 = undefined;
<Xatenev> that works fine with comptime, but without comptime it doesn't
<Xatenev> "expected type "usize", found "i64"
<Xatenev> where can i read about more info on that behavior?
<andrewrk> Xatenev, that's "type coercion": https://ziglang.org/documentation/master/#Type-Coercion
<Xatenev> andrewrk: thanks /me reads :)
<andrewrk> Xatenev, I think the docs could be improved with the rules of when automatic type coercion is allowed. but the rule you're looking at right now is - if an integer is comptime-known, then it will allow an automatic coercion to another integer if it fits into the destination type
<Xatenev> i see, that makes sense
<andrewrk> otherwise if it's runtime-known, then it has to use the types alone to decide if it's a safe conversion
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
riba has joined #zig
LER0ever has joined #zig
LER0ever has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
riba has quit [Ping timeout: 265 seconds]
Akuli has joined #zig
waleee-cl has joined #zig
zfoo has joined #zig
jjido has joined #zig
<andrewrk> here's a PR with that Batch thing working: https://github.com/ziglang/zig/pull/4478
<andrewrk> colorless functions are real! this function LibCInstallation.findNative is compiled in blocking I/O mode when it is used from the stage1 compiler, but it's compiled with async I/O when used from stage2
<fengb> Oh wow
<andrewrk> related: this is a use case for rejecting the proposal https://github.com/ziglang/zig/issues/4355
<andrewrk> it is quite convenient here for address-of-temporary to be scoped to the function
jessermeyer has joined #zig
<jessermeyer> How do you do something obviously unsafe like casting a c_ulong to a c_long to accommodate a C API?
<andrewrk> what do you want to happen if the value is larger than c_long can fit?
<andrewrk> assert that it won't happen?
<jessermeyer> I don't know. These values are all produced and consumed by Window's API.
<andrewrk> it sounds like your next task is to collect more information what these APIs expect and produce
<mq32> andrewrk: is the behaviour of "storing a pointer to temporary for later" defined behaviour in zig?
<jessermeyer> I know what they expect and produce, but they intentionally break Zig's type safety.
<andrewrk> jessermeyer, ok so can the thing producing a c_ulong produce a value that is outside the range of a c_long?
<andrewrk> mq32, yes
<mq32> oh nice
<mq32> but i remember that it failed some time to do stuff like
<andrewrk> mq32, it's a bit of a flow-chart, depending on whether the value is comptime-known, and whether it is in global or function-local scope
<mq32> const stream = &something.getStream().stream;
<andrewrk> that pattern works in function-local scope, and it *should* work in global scope but there is a bug
<mq32> which i've seen quite often in the std library, but i was scared that it will break
<mq32> my thought was: it will only keep the ".stream" object, but not the actual SomethingStream=struct{…,stream:IOStream(…)}
<mq32> so yes, the behaviour is "safe" in terms of accessing stream later, but will maybe? break when calling @fieldParentPtr
<fengb> Having escape analysis could locate most of these errors
<jessermeyer> Yes, it does.
<jessermeyer> Winerror.h just casts the signed bit away.
<andrewrk> jessermeyer, I don't have enough information to advise you. This isn't really a Zig question. It's a question of what you want your bits in memory to be. Zig lets you make your bits whatever you want them to be
decentpenguin has quit [Quit: decentpenguin]
<jessermeyer> So if these bits are produced and interpreted by a chain of black boxes, how do I continue to use Zig here?
<jessermeyer> It's actually a very simple task I'm wanting to do.
<jessermeyer> FunctionA might return an error, and its value is a HRESULT, which is a c_long, but its error code is designated as a UL, and its range, _as specified_ exceeds the c_long.
<jessermeyer> That value can then be fed into another function to tell me more information about what failure occurred.
<jessermeyer> I mean, it's a totally insane API.
<mq32> UL is unsigned long, you could bitcast those types
<jessermeyer> Ah, the bitcast ...
<jessermeyer> Here's the hilarity. Consider 0x887A0002L. That is how the error code is specified. That hex value exceeds the L range. If I 'expand' that number in VS, it automatically converts the L to UL.
<andrewrk> it sounds like you want @bitCast
<jessermeyer> Yeah
<jessermeyer> So, translate-c has no hedge against this, I imagine.
<jessermeyer> I'll open a ticket for discussion.
<andrewrk> so far it doesn't sound like a zig issue
<jessermeyer> It's a fuzzy boundary. Every Zig programmer interfacing with winerror.h will have to face this, especially if it comes through translate-c. The Zig generated code produced by translate-c _does not compile_ cleanly.
<jessermeyer> In fact, it doesn't even compile at all. Not without this @bitcast
<andrewrk> ok that sounds like a zig issue. thanks
TheLemonMan has joined #zig
<TheLemonMan> andrewrk, yo, should this work? https://termbin.com/u1tp
<andrewrk> yo
<andrewrk> TheLemonMan, [*] pointers are supposed to require index syntax and not allow .*
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<andrewrk> x[0] == y[0]
<andrewrk> the first case should be a compile error
<TheLemonMan> yeah that's what I suspected
FireFox317 has joined #zig
jjido has joined #zig
Akuli has quit [Quit: Leaving]
ur5us has joined #zig
mahmudov has quit [Ping timeout: 268 seconds]
<Dominic[m]> I'm hitting https://github.com/ziglang/zig/blob/5e37fc0746a75ed319fc57ae62d8cc966382c592/lib/std/os.zig#L590 and I'm not sure why. The file is being written too. Any hints?
<Dominic[m]> (I'm not writing much, only 5 bytes.
<andrewrk> Dominic[m], check the man pages for write - it explains when EINVAL occurs
<Dominic[m]> I had a look, but the case doesn't make much sense to me! https://linux.die.net/man/2/write How can fd be attached to an object unsuitable for writing, if the file is being written to?
<Dominic[m]> Unless zig is sending O_DIRECT, but I didn't (intentionally) ask it to do that
<andrewrk> nah zig isn't adding O_DIRECT
<andrewrk> maybe an strace would be informative
<Dominic[m]> Oh, I see. strace is awesome.
<Dominic[m]> So, what happened is that I'm writing to "/sys/class/backlight/intel_backlight/brightness", and I was copying how most scripts write to it `echo "123" > /sys..`, including the trailing newline. But that causes an EINVAL when writing the newline. In C/bash/everything else, this just silently works!
<Dominic[m]> oh, Java also silently ignores this.
<jessermeyer> Nice find!
<Dominic[m]> As an aside, is there a simple way to do (u8*u8)/100? There's a potential overflow in the middle, but doing the /100 first causes problems with rounding :)
<TheLemonMan> uh this is weird, stracing `echo` shows that write doesn't return any error
<TheLemonMan> can you share your Zig code?
<Dominic[m]> Yeah, I just tried my C code and noticed the same!
<fengb> How about extend the u8 into u16 first? That should be a “noop” in most architectures
<Dominic[m]> This is what I was doing https://clbin.com/2CErE
<Dominic[m]> fengb: Yeah? Oh that's great. Thanks. I was worried about that. By extend, do you mean `@as` or do you mean something else?
<fengb> @intCast sorry. It’s called extend in wasm >_>
<Dominic[m]> Ah, great. Thanks, I'll give that a go.
<fengb> Then you can @intCast the result back
<andrewrk> Dominic[m], @as is preferred for extending
<fengb> Oh
<andrewrk> @as is a safe cast. @intCast asserts the integer fits in the destination type
<fengb> Good point
rjtobin has joined #zig
<Dominic[m]> Here's the strace comparison for zig & C: https://clbin.com/TWPD1
<TheLemonMan> oh here's what happens, the formatter is printing the integer argument first (the {} part) and then the newline
<TheLemonMan> sysfs doesn't like that and returns an error
<andrewrk> it wants the entire write to happen atomically
_Vi has quit [Ping timeout: 272 seconds]
<andrewrk> leaving off the \n seems like a good solution
<andrewrk> assuming the \n is superfluous
<Dominic[m]> It is superfluous, yeah.
<TheLemonMan> I guess the kernel is only interested in the numerical value
<Dominic[m]> Are there any cases where splitting the writes might be bad? Perhaps with misbehaving fuse devices?
<andrewrk> it's probably calling atoi or whatever libc function which will give a value even if there is junk after the integer
<TheLemonMan> split writes are bad when the underlying device is not a normal file
<TheLemonMan> if the outStream were a buffering one you wouldn't notice this problem :)
<andrewrk> well you would if the packet was big enough
<andrewrk> in this case a buffer is one of those things that mostly makes things work but then creates a really annoying leaky abstraction
<andrewrk> IMO better for the API user to manage a buffer if the system wants certain packets
<Dominic[m]> I've avoided a buffer because I'm (for no real purpose than to try) optimizing all the things.
<TheLemonMan> indeed, a buffered stream adds one more layer to manage
<andrewrk> right, in that case zig's lack of buffering is helping you because if you do introduce a buffer, that's the only layer
FireFox317 has quit [Ping timeout: 240 seconds]
<Dominic[m]> For whatever reason, zig runs 1ms quicker than the C version, consistently. Yay zig :) It also highlights way more error points than the C one did.
jessermeyer has quit [Remote host closed the connection]
<Dominic[m]> Possibly not a question for this channel. But I'm comparing the output of strace from the C & zig version to get a feel for the timings. It looks like the C version uses malloc to allocate arguments, I noticed a comment in one of the argument examples where it mentioned the slow C allocator. It seems that in this case, zig is somehow able to avoid that altogether. I am using nextPosix() to achieve that. Does gcc not follow
<Dominic[m]> that behavior on posix platforms?
<Dominic[m]> I guess I'm assuming that's why brk() is being called, and I don't know that's why. Silly assumption.
<andrewrk> Dominic[m], you're asking about the command line arguments?
<andrewrk> when linux begins executing a binary, it has the command line arguments in stack memory. zig leaves them there and refers to them directly
<Dominic[m]> andrewrk: I think so. Does C/gcc use malloc to allocate the cli argument?
<andrewrk> however, usually there will be a heap allocation when writing cross platform software, because on windows it is necessary to convert to UTF-8
<andrewrk> if you're comparing with C then I'm guessing you're observing the dynamic linker do a lot of stuff
<andrewrk> you can make it leaner by using zig to build a simple C file statically with musl
<andrewrk> zig build-exe --c-source foo.c -lc -target x86_64-linux-musl
<andrewrk> (zig can also build glibc but it requires dynamic linking)
<Dominic[m]> Ah I see. Interesting.
<Dominic[m]> This might be a stupid question, but how do zig binaries get so small if it compiles statically against musl?
<TheLemonMan> it doesn't link against any libc, that's the trick
<andrewrk> zig only links against libc if you give -lc (or on systems whose syscall ABI *is* libc)
<andrewrk> if you compile a zig program with -lc then the relevant syscalls will go through libc, as usually expected
<Dominic[m]> Then I'll get all of the dynamic linker work that I see with the C program?
<andrewrk> but yeah without -lc zig programs just talk directly to the kernel :)
<andrewrk> yes
<TheLemonMan> is sr.ht running the whole set of tests now?
<andrewrk> TheLemonMan, yep! https://github.com/ziglang/zig/pull/4458
<andrewrk> the libclang API issue is annoying
<andrewrk> #4455
<TheLemonMan> amazing!
<TheLemonMan> yeah, that's 100% a bug on the libclang end
<andrewrk> yeah so it's just a couple hundred MiB left to reduce peak RSS and we'll have 100% CI coverage on freebsd
<Dominic[m]> Zig's compile of the C program is just as clean as the zig version. strace output is night & day.
<andrewrk> Dominic[m], could that be musl vs glibc?
<TheLemonMan> the reduced memory consumption made it possible for me to run the tests locally without earlyoom kicking in every time!
<andrewrk> try -target x86_64-linux-gnu
<Dominic[m]> That also knocks it down to 2ms from 3ms.
<Dominic[m]> Will give it a go
<Dominic[m]> The x86_64-linux-gnu output was equally "clean" & fast.
<andrewrk> what does `ldd` report on the binary for that?
<Dominic[m]> `not a dynamic executable`
<Dominic[m]> the binary size is huge.
<andrewrk> that executable is not linking glibc
<andrewrk> double check your commands
<Dominic[m]> I ran this: `./zig/zig build-exe --c-source ~/workshop/brightness.c -lc -target x86_64-linux`
<mq32> looks like the -gnu is missing in the target spec
<Dominic[m]> oh, derp. I feel stupid.
<Dominic[m]> yeah, that's back to being "slow" & noisy again.
jessermeyer has joined #zig
<andrewrk> Dominic[m], I filed a proposal inspired by your question about (u8*u8)/100: https://github.com/ziglang/zig/issues/4481
<andrewrk> with this proposal it would be: @intCast(u8, math.mul(a, b) / 100)
<Dominic[m]> Ooh, that would be handy. Subscribing!
<Dominic[m]> Thanks.
mahmudov has joined #zig
<mq32> andrewrk: that's a nice idea!
<mq32> so when i want to have truncating multiplication, i can just @truncate(u8, std.math.mul(a,b)) or use cast for overflow detection :)
<mq32> neat!
<Dominic[m]> Has anyone played around with using zig in conjunction with a RTOS?
<andrewrk> yes idea is that you can do a bunch of math with ever-increasing integer bit sizes (but they are inferred) and then at the end you decide whether you want to assert no overflow, detect overflow, or something else
<pmwhite> What are efficiency considerations? Do you think it will just optimize into the same thing?
<mq32> pmwhite, yeah probably
<fengb> But wouldn’t that differ from .add and .sub?
<mq32> C does this all the time
<andrewrk> it depends on how large the types grow. if you do too many multiplications you will quickly end up with very large integer types, in which case it would have been better off to handle overflow earlier
<mq32> if you add two uint8_t in C, they will widen to "int", add, then will be clamped again into uint8_t
<andrewrk> with add and sub it would work fairly well, because if you start with a relatively low number like u8, you could do something like 48 additions before getting an integer larger than u64
<andrewrk> in machine code that's just a bunch of register math
<TheLemonMan> I'd be wary of the code duplication
<andrewrk> how so?
<TheLemonMan> you end up with an endless set of `mul` for different input/output types
<fengb> mq32: C overflow is undefined. Most implementations wrap but that depends on the compiler :P
<mq32> fengb: yes. overflow is undefined for signed types
<mq32> but!
<mq32> it is defined that types widen suddely :D
<andrewrk> TheLemonMan, in theory they would all be inlined. but I agree, it is appropriate to be wary of this
Xatenev has quit [Quit: Leaving.]
<andrewrk> oh nice, the wine package on my system is upgraded to 5.0 and now the cross compiled zig std lib tests pass again
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<jessermeyer> ^_^
dbandstra has joined #zig
TheLemonMan has quit [Quit: "It's now safe to turn off your computer."]
jjido has joined #zig
<Flaminator> Is there an easy way to extract 10-bit values from an array of u8?
<andrewrk> I believe PackedIntArray from the std lib handles this use case
<Flaminator> I am currently doing something like http://aeug.eu/p/5a52.jpg but that seems rather cumbersome.
jessermeyer has quit [Remote host closed the connection]
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<daurnimator> Flaminator: PackedIntArray
<fengb> Why would handling wasm stackoverflow require a seg fault? https://github.com/bytecodealliance/wasmtime/issues/900
<andrewrk> they want to put a "guard page" after the stack, so that if the memory is accessed, it causes a segfault
<andrewrk> that's how stack overflow mitigation works in most languages, including zig currently
<fengb> Oh is this “cheaper at runtime” compared to bounds checking?
<andrewrk> yes, the segfault is triggered in hardware, so it's basically free
<fengb> But specifically to wasm, there are better solutions, unless it’s using the native stack as the wasm stack
<andrewrk> but it requires "probing" the stack for functions with frames larger than the page size
metaleap has quit [Quit: Leaving]
<fengb> I guess I should just implement the naive way and “discover” the better solution :P
<andrewrk> once zig has safe recursion, stack-annotated function pointers, and ability to measure stack upper bound size at compile time, it will become the only language that has all of these at the same time: (1) prevention of stack overflow (2) no bounds checking (3) no stack probing or guard pages (4) no hidden memory allocations
<fengb> I meant specifically for the wasm vm, it feels weird to let it page fault, since I think I can code the vm path to have no dynamic stack requirement
<fengb> But I suppose it makes sense if it’s cheaper than bound checking every stack push inside the VM
<fengb> Just trying to figure out what the hell I’m doing >_>
<andrewrk> if perf isn't the main goal of the vm, I think avoiding signals entirely is a cleaner design
<andrewrk> even if it is, some kind of JIT + smart bounds checking might still be the best bet
<jaredmm> What is the correct way to cast 4 bytes from an offset in a []u8 to a u32?
<andrewrk> one of the std.mem.readInt functions
<andrewrk> depending on whether you want native, little, or big endian
<fengb> Okay that makes sense. Using signals reminds me of exceptions heh
<andrewrk> one big problem of signals is that they are very global. relying on signals in a library is very problematic, for example
<andrewrk> and maybe somebody wants to embed the wasm vm in another project
<andrewrk> signals don't compose
ur5us has quit [Ping timeout: 240 seconds]