#Pointer arithmetic question

1 messages · Page 1 of 1 (latest)

echo sage
#

memcpy(view->shadow_data.color, (float[]) {1.0f, 0.0f, 0.0f, 1.0f}, sizeof(float[4]));

I'm trying to translate this line to zig and I attempted to use @memcpy. Memcpy macro complained that I wasn't passing a *u8. I didn't quite understand why I can't pass a *f64?

I eventually came up with this:

@intToPtr(*f64, @ptrToInt(shadow_data.color) + (0 * @sizeOf(f64))).* = 1.0;
@intToPtr(*f64, @ptrToInt(shadow_data.color) + (1 * @sizeOf(f64))).* = 0.0;
@intToPtr(*f64, @ptrToInt(shadow_data.color) + (2 * @sizeOf(f64))).* = 0.0;
@intToPtr(*f64, @ptrToInt(shadow_data.color) + (3 * @sizeOf(f64))).* = 1.0;

Obviously this doesn't feel quite right and neither does this function correctly. Is there something obvious I'm doing wrong?

#

type of color in the c file is *float
the type of the same in zig file is *f64

rain sable
#

*f64 is a single item pointer

echo sage
#

Oh do I need to type it as *[4]f64?

rain sable
#

but for a color type, you can probably just use @Vector(4, f64), which lets you do easy assignment

echo sage
#

thing is, that's a C api interop

#

will vector translate properly to that?

midnight sparrow
#

you can also assign to an array

echo sage
#

Also is vector a 0.11 concept?

#

I haven't updated yet so I don't think I can use @Vector

echo sage
midnight sparrow
#

color: *[4]f64 color.* = .{ 1.0, 0.0, 0.0, 1.0 }

rain sable
midnight sparrow
#

no the abi of vectors is utterly different from extern structs

rain sable
#

well it works sending it to c code

midnight sparrow
#

as long as you use a vector with an abi it will work

echo sage
#

I'll try jacobs answer for now as it is conformant with 0.10. Thank you for the help @rain sable @midnight sparrow ❤️

midnight sparrow
#

also, 0.11 changed nothing with vectors

echo sage
#

vectors exist in 0.10?

#

I didn't see it in the documentation

#

Oh nevermind It does exist

#

My bad!

midnight sparrow
#

vectors exist in 0.4.0

echo sage
#

Yep just noticed. My bad, control f error.

rain sable
#

vectors are good for if you do math on the color, which at least in my code i do semi often

echo sage
#

In my case I'm just passing it to my renderer to render the window shadow

#

most of the code is on the C side

#

will vector have any performance improvements in that case?

rain sable
#

if you arent using it for math, it shouldnt change anything

#

ittl just be preference if it works

echo sage
#

Ah okay I'll check it out

midnight sparrow
#

also why is float being translated to f64

echo sage
#

or was it f32?

midnight sparrow
#

float is f32, double is f64

#

as it says in the nice Primitive Types table

echo sage
#

Woops Just noticed the error

#

Thanks for the help

#

🙏

#

Didn't notice I was type casting incorrectly all this time...that explains why the red shadow was rendering as green

echo sage
#

I don't know why but I'll work with that for now

#

Oh never mind I realized why

midnight sparrow
#

struct { color: [4]f32 } -> shadow_data.color = .{ 1.0, 0.0, 0.0, 1.0 };

echo sage
#

I'm so sorry

#

it's a very dumb error

#

Kindly ignore me

echo sage