#@intFromFloat must have a known result type

1 messages · Page 1 of 1 (latest)

limpid gorge
#

i'm trying to call a function with a i32 as argument, i pass this:

engine.backbuffer_height - @intFromFloat(clip.y + clip.h)
       ^i32                                   ^--------^f32

i get:

src/game/nk.zig:349:48: error: @intFromFloat must have a known result type
                    engine.backbuffer_height - @intFromFloat(clip.y + clip.h),
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/game/nk.zig:349:48: note: use @as to provide explicit result type

why can't it deduce and cast to i32 why force me to uglify my code even more

elder gyro
limpid gorge
#

i would have agreed if there was a :

@i32FromFloat()

#

even tho, i want the compiler to deduce it

elder gyro
#

there is: @as(i32, @intFromFloat(x))

limpid gorge
#

nah, that makes the function unreadable

elder gyro
#

const y: i32 = @intFromFloat(clip.y + clip.h);

limpid gorge
#

now you want me to change my code, from a simple function call, to me storing multiple variables, increasing cognitive load

#

compiler should deduce most stuff

#

if it can't, there should be utilities to deal with it

#

expecting people to stack multiple @'s is not good

elder gyro
#

the design of the zig language disagrees at the moment, maybe this sort of stuff will be less verbose in the future, but that's the way it is now

#

imo the looseness of C implicit casts lead to a lot of bugs so i appreciate zig's approach, i've learned to embrace the verbosity and stop worrying about trying to fit everything into the minimum possible vertical/horizontal source code space

limpid gorge
#

i agree, but the verbosity of these @ makes code less readable

elder gyro
#

imo

const clip_bottom: i32 = @intFromFloat(clip.y + clip.h);
const something = engine.backbuffer_height - clip_bottom;

is quite readable, up to you how you want to handle this sort of stuff, though

limpid gorge
#

again, i'm not gonna store billions of variable to make the compiler happy, my example was extracted from an existing project, of course it's not as simple as this

#

all stem from the fact that i cant rect_a + rect_b in zig already, so i have to work with bunch of things like that

elder gyro
#

could be wrong, but i don't think the above would actually lead to a difference in code gen to @as(i32, @intFromFloat(clip.y + clip.h))

limpid gorge
#

maybe, but the cognitive load is less if my function call remain a one liner

elder gyro
#

i guess i'm the opposite, i love splitting things into multiple lines these days, being able to give names to intermediate values helps a lot in being able to understand the code for me

limpid gorge
#

i now understand why AstGen.zig is one giant file

elder gyro
#

haha, FWIW i've never touched AstGen 🙂

limpid gorge
#

i have zig and zag commands

#

zag when i iterate on an idea, and zig when i move on

#

zig zag

#

maybe i should patch the @ now lol

limpid gorge
#

i'm thinking of adding @to

        ortho[0][0] = ortho[0][0] / @as(f32, @floatFromInt(engine.backbuffer_width));
        ortho[1][1] = ortho[1][1] / @as(f32, @floatFromInt(engine.backbuffer_height));

        ortho[0][0] = ortho[0][0] / @to(f32, engine.backbuffer_width);
        ortho[1][1] = ortho[1][1] / @to(f32, engine.backbuffer_height);
slate ridge
#

You are maintaining a fork of the zig compiler?

#

It's not wprth it

limpid gorge
#

i'm not a conformist, if it's worth it to me, that's all that matter

#

it's few lines patch only

alpine crescent
#

I'm baffled by the fact that you lament a cognitive load increase and suggest patching the compiler in the same thread. You could save the extra @ and make to a function that zig would almost certainly inline. Readable and simple

limpid gorge
#

that's the whole purpose of open source

#

my patch won't affect any of your projects, but will improve my sanity

#

You could save the extra @ and make to a function that zig would almost certainly inline. Readable and simple
something that i have to import everywhere? no ty, yall don't understand but that's ok, i'll have my patch

slate ridge
#

Yeah I don't understand why you need to patch a language lol

#

Zig is intentionally explicit and verbose, you gonna have a bad time I think

limpid gorge
#

i think too many people defend poor choices, wich lead to nobody having the balls to actually call out bad verbosity

#

i don't mind verbosity, when it's concise

#

if you see something bad, say it, you don't need to hide behind ideology

#

experimentation leads to improvements, sometimes

#

pursue that

#

i may be wrong, but i'll find out

slate ridge
#

well then

#

zig won't change that 99%

limpid gorge
#

said jakub

#

author and owner of zig

slate ridge
#

well, you can try if you still want

limpid gorge
#

there are many accepted proposals on the zig issue tracker, they are willing to change things if they think it's a good idea

slate ridge
#

yeah, if they do

limpid gorge
#

exact

#

for now, i'll settle with a patch i'll maintain

slate ridge
limpid gorge
#

oh, yeah, that i agree, i started this zag thing because i agree with them, so i made it a zag command instead

slate ridge
#

wait, what?

limpid gorge
#

the fork, i set an alias zag

slate ridge
#

i started this zag thing because i agree with them
so why you started something that does what the official compiler provides already?

#

unless you wanted the ability to turn it off sometimes

limpid gorge
#

that's exactly what i said

#

zag when i iterate on an idea, and zig when i move on

pallid nest
limpid gorge
#

brother

languid copper
pallid nest
limpid gorge
pallid nest
#

one is less code, idk how the other is a "core fix"

limpid gorge
#

but again, that won't affect any of your projects, it's ok if you think that's a bad idea

pallid nest
#

patching compiler sounds fine (for unused). just explaining why it sounds inefficient to patch the to one specifically when it can be a function

languid copper
#

How is it inefficient?

pallid nest
#

that assumes ur using multiple projects

#

u could even just patch stdlib

languid copper
#

When have programmers ever just had one project??

pallid nest
#

i use 0.13.0 specifically for TigerBeetle

languid copper
pallid nest
#

most programs have dev environments specific to the project

limpid gorge
#

you should take a moment to think about it, at some point before suggesting something, you have to understnd what problem i am trying to solve

#

i'm not tyring to do a HTTP request

#

i'm not trying to save something to a database

#

importing something is out of question

#

creating a function? same, why should i do that

#

you have to stop putting the problem under the carpet and ignore it

#

i choose to solve it

pallid nest
#

DrakeNo add a function to stdlib or something else u already import
DrakeYes patch in a new builtin to a fork of the compiler

limpid gorge
#

patch it once, solve it forever

#

you don't want to realize how anything other than a builtin is a bad suggestion

slate ridge
#

maybe not you ig

#

for a hobby project you can even waste some extra time

limpid gorge
slate ridge
#

Debug build performance?

#

Idk, I would use the language the way they want you to use it
Or otherwise go use a different language

limpid gorge
#

again, said jakub, author and owner of the zig programming langauge

#

i'm pretty sure andrew would agree with me

#

i understand the reasoning behind it, i however not agree with the solution

#

i'm porting my engine to zig, and already i can see drop in FPS in debug build

slate ridge
#

What does drop in FPS have to do with any of this? I'm confused

limpid gorge
#

ok, so you are clueless yet you make suggestions

slate ridge
#

All I know is you patched unused variables error and were planning to add new builtin

#

No idea what that has to do eith performance

limpid gorge
#

you suggest me to create a function to do a cast

#

and that kind of shit is all over the std too, small little function to do basic things

#

it's compounding

slate ridge
#

And you think there must be a better way that Andrew would accept

limpid gorge
#

he won't accept, hence why my patch

#

i'm not forcing you to agree with me, i'm asking you to not force me to comply

slate ridge
#

You want something impossible, an ideal language

limpid gorge
#

i never said i want anything

#

i wrote my patch

slate ridge
#

Yeah, you wrote it for a reason

limpid gorge
#

because i need, that's different 😉

limpid gorge
#

2 times slower, just for the login screen

#

left: zig, right: D

#

let's fire the profiler

slate ridge
#

And does your patch make it faster or what

limpid gorge
#

no, but it'll help not make it much slower

slate ridge
#

Huh?

#

How tho

viscid kraken
limpid gorge
#

not much, graphics part is mostly 1:1, assets is async io (zig) vs thread (d) but that doesn't matter, the issue happens inside the UI code, wich is C (nuklear), the difference is how zig translates C vs D's importc, i suspect it does bunch of useless copies, that's what the profiler suggests

#

i rewired the memcpy call from nuklear to zig, that helps but still slow

#

left zig, right D, both in the login screen same UI code

viscid kraken
#

Is this in release mode or debug?

limpid gorge
#

debug

#

ReleaseFast is on par with D's debug

viscid kraken
#

Is D's optimised faster?

limpid gorge
#

looks like it is

#

but release build is almost 2x faster than ReleastFast too

#

perhaps a bug in zig translate-c

viscid kraken
#

Yeah

limpid gorge
#

nuklear code base is massive

viscid kraken
#

Wait are you translatec on the source or

#

The header

limpid gorge
#

no, that's what zig does

viscid kraken
#

What does d do?

#

Does it do it like zig

limpid gorge
#
    // nuklear
    exe.addIncludePath(b.path("src/c/nuklear/"));
    exe.addCSourceFile(.{ .file = b.path("src/c/nuklear/nuklear.c"), .flags = &.{ "-std=c11", "-Wl,--allow-multiple-definition" } });
#

it translate C to zig

viscid kraken
#

so just the header

limpid gorge
#
pub const c = @cImport({
    @cDefine("NK_INCLUDE_VERTEX_BUFFER_OUTPUT", "");
    @cInclude("nuklear_all.h");
});
viscid kraken
#

Does d transpile the code to d code or does it do it like zig and only get the header translated?

limpid gorge
#

D parses C code directly

#

it's like zig translate-c except it doesn't save to disk

viscid kraken
#

Oh yeah I think that would be what's making it slower

#

d looks like it translates the C into d without invoking a C compiler

#

I think if zig could do that it could benefit from optimisations in the same compilation unit

#

But it can't so I think that's why it's slow

#

If you had a zig port 1:1 of nuclear it would match d I think

limpid gorge
#

oh i thought zig was doing the same

viscid kraken
#

Oh nah zig invokes clang

#

Without goto like d I don't think C code can be 1:1 converted to zig code

limpid gorge
#

compiling nukear myself solves the performance issue

#
// nuklear
    exe.addIncludePath(b.path("src/c/nuklear/"));
    // exe.addCSourceFile(.{ .file = b.path("src/c/nuklear/nuklear.c"), .flags = &.{ "-std=c11", "-Wl,--allow-multiple-definition" } });
    exe.addObjectFile(b.path("src/c/nuklear/lib/nuklear.a"));
viscid kraken
#

how are you compiling it?

limpid gorge
#
clang -c -Wall -std=c99 -fPIC -O2 -o tmp/nuklear.o nuklear.c
llvm-ar rc tmp/nuklear.a tmp/nuklear.o
viscid kraken
#

what if you put those flags into zig's addCSourceFile?

limpid gorge
#

good idea, even tho i tried all kind of optimize= modes

#

:1:1: error: unknown argument '-fPIC '; did you mean '-fPIC'? lol

#

oh i had a white space

viscid kraken
#

space?

#

yeah

limpid gorge
#

INFO: fps: 6517 0.000144 with same flags
INFO: fps: 8311 0.000118 with the static lib built myself, same flags

#

but looks better already

#

it's x86 backend

viscid kraken
#

I think it still calls to clang for c source files but I'm not sure

limpid gorge
#

i don't think it does

viscid kraken
#

idk if aro is being used

limpid gorge
#

because ReleaseFast also was slow

viscid kraken
#

does non x86 backend change it?

#

because the x86 backend has almost no optimisations right now

limpid gorge
#

INFO: fps: 6897 0.000147
INFO: fps: 8828 0.000113

better but same gap

#

all tests in debug

viscid kraken
#

what about in release fast?

#

because the lib you compiled yourself with clang should be as fast as the one zig compiles since its just calling to clang for c compilation

#

unless you've overriden the target

limpid gorge
#

INFO: fps: 10353 0.000095
INFO: fps: 10011 0.000115

same, interesting

viscid kraken
#

is it faster now?

limpid gorge
#

so x86 backend also compile c code, wich produces bad code

viscid kraken
#

its just not as optimised as something as mature as llvm

glossy tangle
#

x86_64 backend doesn’t compile c code, neither does the llvm backend. Zig always uses clang for that.

limpid gorge
#

ok, so that's reassuring

glossy tangle
#

Translate-c is for translating headers to Zig to easily link with C libraries without having to write the binding manually.