change a_float to a_real
[liba.git] / lua / src / a.c
blob54d1869e7af4c0e32a53206a42b4dadd5bf4a450
1 /***
2 algorithm library
3 @module liba
4 */
6 #include "a.h"
8 /***
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
13 @function hash_bkdr
15 static int liba_hash_bkdr(lua_State *L)
17 a_u32 val = 0;
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); }
21 lua_u32_new(L, val);
22 return 1;
25 /***
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
30 @function hash_sdbm
32 static int liba_hash_sdbm(lua_State *L)
34 a_u32 val = 0;
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); }
38 lua_u32_new(L, val);
39 return 1;
42 #include "a/math.h"
44 /***
45 square root of an unsigned integer
46 @tparam integer ... independent variables
47 @treturn integer calculated result
48 @function isqrt
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);
63 return n;
66 /***
67 reciprocal of square-root
68 @tparam number ... independent variables
69 @treturn number calculated result
70 @function rsqrt
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 */
85 lua_pushnumber(L, x);
87 return n;
90 #if !defined A_VERSION
91 #include "a/version.h"
92 #endif /* A_VERSION */
94 int luaopen_liba(lua_State *L)
96 /***
97 algorithm library
98 @field VERSION algorithm library version string
99 @table liba
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);
121 lua_rawset(L, -3);
123 lua_pushstring(L, "crc16");
124 luaopen_liba_crc16(L);
125 lua_rawset(L, -3);
127 lua_pushstring(L, "crc32");
128 luaopen_liba_crc32(L);
129 lua_rawset(L, -3);
131 lua_pushstring(L, "crc64");
132 luaopen_liba_crc64(L);
133 lua_rawset(L, -3);
135 lua_pushstring(L, "crc8");
136 luaopen_liba_crc8(L);
137 lua_rawset(L, -3);
139 lua_pushstring(L, "hpf");
140 luaopen_liba_hpf(L);
141 lua_rawset(L, -3);
143 lua_pushstring(L, "lpf");
144 luaopen_liba_lpf(L);
145 lua_rawset(L, -3);
147 lua_pushstring(L, "mf");
148 luaopen_liba_mf(L);
149 lua_rawset(L, -3);
151 lua_pushstring(L, "pid");
152 luaopen_liba_pid(L);
153 lua_rawset(L, -3);
155 lua_pushstring(L, "pid_fuzzy");
156 luaopen_liba_pid_fuzzy(L);
157 lua_rawset(L, -3);
159 lua_pushstring(L, "pid_neuro");
160 luaopen_liba_pid_neuro(L);
161 lua_rawset(L, -3);
163 lua_pushstring(L, "regress_linear");
164 luaopen_liba_regress_linear(L);
165 lua_rawset(L, -3);
167 lua_pushstring(L, "regress_simple");
168 luaopen_liba_regress_simple(L);
169 lua_rawset(L, -3);
171 lua_pushstring(L, "tf");
172 luaopen_liba_tf(L);
173 lua_rawset(L, -3);
175 lua_pushstring(L, "trajbell");
176 luaopen_liba_trajbell(L);
177 lua_rawset(L, -3);
179 lua_pushstring(L, "trajpoly3");
180 luaopen_liba_trajpoly3(L);
181 lua_rawset(L, -3);
183 lua_pushstring(L, "trajpoly5");
184 luaopen_liba_trajpoly5(L);
185 lua_rawset(L, -3);
187 lua_pushstring(L, "trajpoly7");
188 luaopen_liba_trajpoly7(L);
189 lua_rawset(L, -3);
191 lua_pushstring(L, "trajtrap");
192 luaopen_liba_trajtrap(L);
193 lua_rawset(L, -3);
195 lua_pushstring(L, "version");
196 luaopen_liba_version(L);
197 lua_rawset(L, -3);
199 return 1;