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 i
, n
= lua_gettop(L
);
53 for (i
= 1; i
<= n
; ++i
)
55 lua_Integer x
= luaL_checkinteger(L
, i
);
56 #if A_SIZE_MAX == 0xFFFFFFFFUL
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 i
, n
= lua_gettop(L
);
75 for (i
= 1; i
<= n
; ++i
)
77 lua_Number x
= luaL_checknumber(L
, i
);
78 #if A_REAL_TYPE + 0 == A_REAL_DOUBLE
79 x
= (lua_Number
)a_f64_rsqrt((a_f64
)x
);
80 #elif A_REAL_TYPE + 0 == A_REAL_SINGLE
81 x
= (lua_Number
)a_f32_rsqrt((a_f32
)x
);
82 #else /* !A_REAL_TYPE */
83 x
= (lua_Number
)(1 / sqrt((double)x
));
84 #endif /* A_REAL_TYPE */
90 #if !defined A_VERSION
91 #include "a/version.h"
92 #endif /* A_VERSION */
94 int luaopen_liba(lua_State
*L
)
98 @field VERSION algorithm library version string
101 static lua_fun
const funcs
[] = {
102 {"hash_bkdr", liba_hash_bkdr
},
103 {"hash_sdbm", liba_hash_sdbm
},
104 {"isqrt", liba_isqrt
},
105 {"rsqrt", liba_rsqrt
},
107 luaL_checkversion(L
);
108 #if !defined LUA_VERSION_NUM || (LUA_VERSION_NUM <= 501)
110 lua_fun l
= {NULL
, NULL
};
111 luaL_register(L
, "liba", &l
);
113 #else /* !LUA_VERSION_NUM */
114 lua_createtable(L
, 0, A_LEN(funcs
) + 1);
115 #endif /* LUA_VERSION_NUM */
116 lua_fun_reg(L
, -1, funcs
, A_LEN(funcs
));
117 lua_str_set(L
, -1, "VERSION", A_VERSION
);
119 lua_pushstring(L
, "complex");
120 luaopen_liba_complex(L
);
123 lua_pushstring(L
, "crc16");
124 luaopen_liba_crc16(L
);
127 lua_pushstring(L
, "crc32");
128 luaopen_liba_crc32(L
);
131 lua_pushstring(L
, "crc64");
132 luaopen_liba_crc64(L
);
135 lua_pushstring(L
, "crc8");
136 luaopen_liba_crc8(L
);
139 lua_pushstring(L
, "hpf");
143 lua_pushstring(L
, "lpf");
147 lua_pushstring(L
, "mf");
151 lua_pushstring(L
, "pid");
155 lua_pushstring(L
, "pid_fuzzy");
156 luaopen_liba_pid_fuzzy(L
);
159 lua_pushstring(L
, "pid_neuro");
160 luaopen_liba_pid_neuro(L
);
163 lua_pushstring(L
, "regress_linear");
164 luaopen_liba_regress_linear(L
);
167 lua_pushstring(L
, "regress_simple");
168 luaopen_liba_regress_simple(L
);
171 lua_pushstring(L
, "tf");
175 lua_pushstring(L
, "trajbell");
176 luaopen_liba_trajbell(L
);
179 lua_pushstring(L
, "trajpoly3");
180 luaopen_liba_trajpoly3(L
);
183 lua_pushstring(L
, "trajpoly5");
184 luaopen_liba_trajpoly5(L
);
187 lua_pushstring(L
, "trajpoly7");
188 luaopen_liba_trajpoly7(L
);
191 lua_pushstring(L
, "trajtrap");
192 luaopen_liba_trajtrap(L
);
195 lua_pushstring(L
, "version");
196 luaopen_liba_version(L
);