Skip to main content

luaur_vm/functions/
vector_dot.rs

1use crate::functions::lua_l_checkvector::lua_l_checkvector;
2use crate::functions::lua_pushnumber::lua_pushnumber;
3use crate::macros::lua_vector_size::LUA_VECTOR_SIZE;
4use crate::type_aliases::lua_state::lua_State;
5
6#[allow(non_snake_case)]
7pub unsafe fn vector_dot(L: *mut lua_State) -> core::ffi::c_int {
8    let a = lua_l_checkvector(L, 1);
9    let b = lua_l_checkvector(L, 2);
10
11    if LUA_VECTOR_SIZE == 4 {
12        lua_pushnumber(
13            L,
14            ((*a.offset(0)) * (*b.offset(0))
15                + (*a.offset(1)) * (*b.offset(1))
16                + (*a.offset(2)) * (*b.offset(2))
17                + (*a.offset(3)) * (*b.offset(3))) as f64,
18        );
19    } else {
20        lua_pushnumber(
21            L,
22            ((*a.offset(0)) * (*b.offset(0))
23                + (*a.offset(1)) * (*b.offset(1))
24                + (*a.offset(2)) * (*b.offset(2))) as f64,
25        );
26    }
27
28    1
29}