summaryrefslogtreecommitdiff
path: root/apps/plugins/pdbox/PDa/intern/rsqrt~.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/pdbox/PDa/intern/rsqrt~.c')
-rw-r--r--apps/plugins/pdbox/PDa/intern/rsqrt~.c105
1 files changed, 0 insertions, 105 deletions
diff --git a/apps/plugins/pdbox/PDa/intern/rsqrt~.c b/apps/plugins/pdbox/PDa/intern/rsqrt~.c
index e33a21a0e9..dd7e2a135b 100644
--- a/apps/plugins/pdbox/PDa/intern/rsqrt~.c
+++ b/apps/plugins/pdbox/PDa/intern/rsqrt~.c
@@ -103,108 +103,3 @@ void rsqrt_tilde_setup(void)
103 class_addmethod(sigrsqrt_class, (t_method)sigrsqrt_dsp, gensym("dsp"), 0); 103 class_addmethod(sigrsqrt_class, (t_method)sigrsqrt_dsp, gensym("dsp"), 0);
104} 104}
105 105
106#include <m_pd.h>
107#include <m_fixed.h>
108
109/* sigrsqrt - reciprocal square root good to 8 mantissa bits */
110
111#define DUMTAB1SIZE 256
112#define DUMTAB2SIZE 1024
113
114static float rsqrt_exptab[DUMTAB1SIZE], rsqrt_mantissatab[DUMTAB2SIZE];
115
116static void init_rsqrt(void)
117{
118 int i;
119 for (i = 0; i < DUMTAB1SIZE; i++)
120 {
121 float f;
122 long l = (i ? (i == DUMTAB1SIZE-1 ? DUMTAB1SIZE-2 : i) : 1)<< 23;
123 *(long *)(&f) = l;
124 rsqrt_exptab[i] = 1./sqrt(f);
125 }
126 for (i = 0; i < DUMTAB2SIZE; i++)
127 {
128 float f = 1 + (1./DUMTAB2SIZE) * i;
129 rsqrt_mantissatab[i] = 1./sqrt(f);
130 }
131}
132
133 /* these are used in externs like "bonk" */
134
135float q8_rsqrt(float f)
136{
137 long l = *(long *)(&f);
138 if (f < 0) return (0);
139 else return (rsqrt_exptab[(l >> 23) & 0xff] *
140 rsqrt_mantissatab[(l >> 13) & 0x3ff]);
141}
142
143float q8_sqrt(float f)
144{
145 long l = *(long *)(&f);
146 if (f < 0) return (0);
147 else return (f * rsqrt_exptab[(l >> 23) & 0xff] *
148 rsqrt_mantissatab[(l >> 13) & 0x3ff]);
149}
150
151 /* the old names are OK unless we're in IRIX N32 */
152
153#ifndef N32
154float qsqrt(float f) {return (q8_sqrt(f)); }
155float qrsqrt(float f) {return (q8_rsqrt(f)); }
156#endif
157
158
159
160typedef struct sigrsqrt
161{
162 t_object x_obj;
163 float x_f;
164} t_sigrsqrt;
165
166static t_class *sigrsqrt_class;
167
168static void *sigrsqrt_new(void)
169{
170 t_sigrsqrt *x = (t_sigrsqrt *)pd_new(sigrsqrt_class);
171 outlet_new(&x->x_obj, gensym("signal"));
172 x->x_f = 0;
173 return (x);
174}
175
176static t_int *sigrsqrt_perform(t_int *w)
177{
178 float *in = *(t_float **)(w+1), *out = *(t_float **)(w+2);
179 t_int n = *(t_int *)(w+3);
180 while (n--)
181 {
182 float f = *in;
183 long l = *(long *)(in++);
184 if (f < 0) *out++ = 0;
185 else
186 {
187 float g = rsqrt_exptab[(l >> 23) & 0xff] *
188 rsqrt_mantissatab[(l >> 13) & 0x3ff];
189 *out++ = 1.5 * g - 0.5 * g * g * g * f;
190 }
191 }
192 return (w + 4);
193}
194
195static void sigrsqrt_dsp(t_sigrsqrt *x, t_signal **sp)
196{
197 dsp_add(sigrsqrt_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
198}
199
200void rsqrt_tilde_setup(void)
201{
202 init_rsqrt();
203 sigrsqrt_class = class_new(gensym("rsqrt~"), (t_newmethod)sigrsqrt_new, 0,
204 sizeof(t_sigrsqrt), 0, 0);
205 /* an old name for it: */
206 class_addcreator(sigrsqrt_new, gensym("q8_rsqrt~"), 0);
207 CLASS_MAINSIGNALIN(sigrsqrt_class, t_sigrsqrt, x_f);
208 class_addmethod(sigrsqrt_class, (t_method)sigrsqrt_dsp, gensym("dsp"), 0);
209}
210