aboutsummaryrefslogtreecommitdiff
path: root/iemlib1/src/iem_sqrt4~.c
blob: 0b4049d1ebcd07d9046cf057d7c6ce7cec41790e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib1 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2012 */

#include "m_pd.h"
#include "iemlib.h"
#include <math.h>

#define IEMSQRT4TAB1SIZE 256
#define IEMSQRT4TAB2SIZE 1024

/* ------------------------ iem_sqrt4~ ----------------------------- */

static t_float iem_rsqrt4_tilde_exptab[IEMSQRT4TAB1SIZE];
static t_float iem_rsqrt4_tilde_mantissatab[IEMSQRT4TAB2SIZE];

static t_class *iem_sqrt4_tilde_class;

typedef struct _iem_sqrt4_tilde
{
  t_object  x_obj;
  t_float   x_float_sig_in;
} t_iem_sqrt4_tilde;

static t_int *iem_sqrt4_tilde_perform(t_int *w)
{
  t_sample *in = (t_sample *)(w[1]);
  t_sample *out = (t_sample *)(w[2]);
  t_int n = (t_int)(w[3])/4;
  
  while(n--)
  {
    t_sample f = *in;
    t_sample g, h;
    union tabfudge_f tf;
    
    if(f <= 0.0)
    {
      *out++ = 0.0;
      *out++ = 0.0;
      *out++ = 0.0;
      *out++ = 0.0;
    }
    else
    {
      tf.tf_f = f;
      g = iem_rsqrt4_tilde_exptab[((tf.tf_l) >> 23) & 0xff] * iem_rsqrt4_tilde_mantissatab[((tf.tf_l) >> 13) & 0x3ff];
      h = f * (1.5f * g - 0.5f * g * g * g * f);
      *out++ = h;
      *out++ = h;
      *out++ = h;
      *out++ = h;
    }
    in += 4;
  }
  return(w+4);
}

static void iem_sqrt4_tilde_dsp(t_iem_sqrt4_tilde *x, t_signal **sp)
{
  dsp_add(iem_sqrt4_tilde_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void iem_sqrt4_tilde_init(void)
{
  int i;
  t_float f;
  union tabfudge_f tf;
  
  for(i=0; i<IEMSQRT4TAB1SIZE; i++)
  {
    tf.tf_l = (i ? (i == IEMSQRT4TAB1SIZE-1 ? IEMSQRT4TAB1SIZE-2 : i) : 1)<< 23;
    iem_rsqrt4_tilde_exptab[i] = 1.0/sqrt(tf.tf_f); 
  }
  for(i=0; i<IEMSQRT4TAB2SIZE; i++)
  {
    f = 1.0 + (1.0/(t_float)IEMSQRT4TAB2SIZE) * (t_float)i;
    iem_rsqrt4_tilde_mantissatab[i] = 1.0/sqrt(f);  
  }
}

static void *iem_sqrt4_tilde_new(void)
{
  t_iem_sqrt4_tilde *x = (t_iem_sqrt4_tilde *)pd_new(iem_sqrt4_tilde_class);
  
  outlet_new(&x->x_obj, gensym("signal"));
  x->x_float_sig_in = 0;
  return (x);
}

void iem_sqrt4_tilde_setup(void)
{
  iem_sqrt4_tilde_init();
  iem_sqrt4_tilde_class = class_new(gensym("iem_sqrt4~"), (t_newmethod)iem_sqrt4_tilde_new, 0,
    sizeof(t_iem_sqrt4_tilde), 0, 0);
  CLASS_MAINSIGNALIN(iem_sqrt4_tilde_class, t_iem_sqrt4_tilde, x_float_sig_in);
  class_addmethod(iem_sqrt4_tilde_class, (t_method)iem_sqrt4_tilde_dsp, gensym("dsp"), 0);
}