9 a hash function whose prime number is 131
10 @tparam string str string to be processed
11 @tparam[opt] integer initial value
12 @treturn string hash value
15 static int liba_hash_bkdr(lua_State
*L
)
18 int top
= lua_gettop(L
);
19 if (top
> 1) { val
= lua_u32_get(L
, 2); }
20 if (top
> 0) { val
= a_hash_bkdr(luaL_checklstring(L
, 1, A_NULL
), val
); }
26 a hash function whose prime number is 65599
27 @tparam string str string to be processed
28 @tparam[opt] integer initial value
29 @treturn string hash value
32 static int liba_hash_sdbm(lua_State
*L
)
35 int top
= lua_gettop(L
);
36 if (top
> 1) { val
= lua_u32_get(L
, 2); }
37 if (top
> 0) { val
= a_hash_sdbm(luaL_checklstring(L
, 1, A_NULL
), val
); }
45 square root of an unsigned integer
46 @tparam integer ... independent variables
47 @treturn integer calculated result
50 static int liba_isqrt(lua_State
*L
)
52 int const Ln
= lua_gettop(L
);
53 for (int Li
= 1; Li
<= Ln
; ++Li
)
55 lua_Integer x
= luaL_checkinteger(L
, Li
);
56 #if A_SIZE_MAX == A_U32_MAX
57 x
= (lua_Integer
)a_u32_sqrt((a_u32
)x
);
58 #else /* !A_SIZE_MAX */
59 x
= (lua_Integer
)a_u64_sqrt((a_u64
)x
);
60 #endif /* A_SIZE_MAX */
61 lua_pushinteger(L
, x
);
67 reciprocal of square-root
68 @tparam number ... independent variables
69 @treturn number calculated result
72 static int liba_rsqrt(lua_State
*L
)
74 int const Ln
= lua_gettop(L
);
75 for (int Li
= 1; Li
<= Ln
; ++Li
)
77 lua_Number x
= luaL_checknumber(L
, Li
);
78 #if A_FLOAT_TYPE + 0 == A_FLOAT_DOUBLE
79 x
= (lua_Number
)a_f64_rsqrt((a_f64
)x
);
80 #elif A_FLOAT_TYPE + 0 == A_FLOAT_SINGLE
81 x
= (lua_Number
)a_f32_rsqrt((a_f32
)x
);
82 #else /* !A_FLOAT_TYPE */
83 x
= (lua_Number
)(1 / sqrt((double)x
));
84 #endif /* A_FLOAT_TYPE */
90 #if !defined A_VERSION
91 #include "a/version.h"
92 #endif /* A_VERSION */
94 int luaopen_liba(lua_State
*L
)
99 @field VERSION algorithm library version string
102 static lua_fun
const funcs
[] = {
103 {"hash_bkdr", liba_hash_bkdr
},
104 {"hash_sdbm", liba_hash_sdbm
},
105 {"isqrt", liba_isqrt
},
106 {"rsqrt", liba_rsqrt
},
108 #if !defined LUA_VERSION_NUM || (LUA_VERSION_NUM <= 501)
109 lua_fun func
= {NULL
, NULL
};
110 luaL_register(L
, "liba", &func
);
111 #else /* !LUA_VERSION_NUM */
112 lua_createtable(L
, 0, A_LEN(funcs
) + 1);
113 #endif /* LUA_VERSION_NUM */
114 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
115 lua_str_set(L
, -1, "VERSION", A_VERSION
);
117 lua_pushstring(L
, "complex");
118 luaopen_liba_complex(L
);
121 lua_pushstring(L
, "crc16");
122 luaopen_liba_crc16(L
);
125 lua_pushstring(L
, "crc32");
126 luaopen_liba_crc32(L
);
129 lua_pushstring(L
, "crc64");
130 luaopen_liba_crc64(L
);
133 lua_pushstring(L
, "crc8");
134 luaopen_liba_crc8(L
);
137 lua_pushstring(L
, "hpf");
141 lua_pushstring(L
, "lpf");
145 lua_pushstring(L
, "mf");
149 lua_pushstring(L
, "pid");
153 lua_pushstring(L
, "pid_fuzzy");
154 luaopen_liba_pid_fuzzy(L
);
157 lua_pushstring(L
, "pid_neuro");
158 luaopen_liba_pid_neuro(L
);
161 lua_pushstring(L
, "tf");
165 lua_pushstring(L
, "trajbell");
166 luaopen_liba_trajbell(L
);
169 lua_pushstring(L
, "trajpoly3");
170 luaopen_liba_trajpoly3(L
);
173 lua_pushstring(L
, "trajpoly5");
174 luaopen_liba_trajpoly5(L
);
177 lua_pushstring(L
, "trajpoly7");
178 luaopen_liba_trajpoly7(L
);
181 lua_pushstring(L
, "trajtrap");
182 luaopen_liba_trajtrap(L
);
185 lua_pushstring(L
, "version");
186 luaopen_liba_version(L
);