Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I wrote a zig macro. #155

Open
muu2007 opened this issue Oct 23, 2023 · 1 comment
Open

I wrote a zig macro. #155

muu2007 opened this issue Oct 23, 2023 · 1 comment

Comments

@muu2007
Copy link

muu2007 commented Oct 23, 2023

import 'ffi'


macro zig = (code)->
	import 'md5'
	dir, fn = 'embed/', md5.sumhexa(code) -- without ext (.zig/.h/.so)
	with io.open(dir..fn..'.zig', 'w')
		\write(code)
		\close()
	cmd = code\match('^//#(.-)\n') or 'zig build-lib -dynamic -O ReleaseSmall' -- -fstrip -fsingle-threaded'
	-- os.execute(command or "pushd &>/dev/null #{dir} && #{cmd} #{fn}.zig -femit-h -target x86_64-windows-gnu && popd &>/dev/null")
	os.execute(command or "pushd &>/dev/null #{dir} && #{cmd} #{fn}.zig -femit-h && popd &>/dev/null")
	_h = io.open(dir..fn..'.h')\read('*a')\gsub('#include "zig.h"\n', '')\gsub('zig_extern ', '')--\gsub('\n','')
	"rawget({ffi.cdef\'#{_h}\', ffi.load\'#{dir}lib#{fn}.so\'}, 2)"


fib = $zig[[
export fn fib(n: i32) i32 {
    if (n == 0 or n == 1) return n;
    return fib(n - 1) + fib(n - 2);
}
]]


print fib.fib(10)
print 250 + 10
print 250 |>$zig[[export fn u8addwrap(a: u8, b:u8) u8 {return a +% b;}]].u8addwrap 10
print 250 |>$zig[[export fn u8addsatu(a: u8, b:u8) u8 {return a +| b;}]].u8addsatu 10

brighten = $zig[[
// cite: https://zenn.dev/kounoike/articles/20230120-zig-imgprocessing-simd
export fn brightenSIMD(image: [*]u8, len: usize) void {
    var idx: usize = 0;
    const add_vec = @Vector(4, u8){ 10, 10, 10, 0 };
    while (idx < len) : (idx += 4) {
        const image_vec = @Vector(4, u8){ image[idx + 0], image[idx + 1], image[idx + 2], image[idx + 3] };
        const result_vec = image_vec +| add_vec;
        image[idx + 0] = result_vec[0];
        image[idx + 1] = result_vec[1];
        image[idx + 2] = result_vec[2];
        image[idx + 3] = result_vec[3]; // ここを消すと遅くなる
    }
}
]]

image = string.char unpack [math.random(0, 255) for _ = 1, 1200]
print string.byte image, 1, 10
for _=1,9
	brighten.brightenSIMD(ffi.cast('uint8_t *', image), #image)
	print string.byte image, 1, 10


qoi = $zig[[
//# cp -f ../qoi.h qoi.h && zig build-lib -dynamic -O ReleaseSmall -I. -I/usr/include
const qoi = @cImport({@cDefine("QOI_IMPLEMENTATION", ""); @cInclude("qoi.h");});
export fn read(filename: [*:0]const u8, pwidth: *u32, pheight: *u32) ?*anyopaque {
	var desc = qoi.qoi_desc{.width = 0, .height = 0, .channels = 0, .colorspace = 0};
	var result = qoi.qoi_read(filename, &desc, 4);
	pwidth.* = desc.width;
	pheight.* = desc.height;
	return result;
	//defer free(data);
}
]]

pwidth, pheight = ffi.new('uint32_t[1]'), ffi.new('uint32_t[1]')
pixels = qoi.read('dice.qoi', pwidth, pheight)
-- gr.newImage(love.image.newImageData(pwidth[0], pheight[0], 'rgba8', ffi.string(pixels, pwidth[0] * pheight[0] * 4)))
print pwidth[0], pheight[0]
@pigpigyyy
Copy link
Owner

Wow, it is the expected usage for Yue macro functions. Doing code generations or executing some building workflows through Lua during the compile time. The zig code generation seems a little bit too hardcore though. I'm glad you tried it!

Haven't got time for testing your code, but quess you were just displaying an use case, instead of making real issue for help right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants