0x260x26_tilde.c 9.06 KB
Newer Older
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1
/*
Ivica Bukvic's avatar
Ivica Bukvic committed
2
 * &&~: logical AND for signals
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
3
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
4
 * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
5
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
6
7
8
9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Jonathan Wilkes's avatar
Jonathan Wilkes committed
10
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Jonathan Wilkes's avatar
Jonathan Wilkes committed
15
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
16
17
18
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
19
20
21
22
23
24
25
26
27


#include "zexySIMD.h"

/* ------------------------ logical~ ----------------------------- */

/* ----------------------------- andand_tilde ----------------------------- */
static t_class *andand_tilde_class, *scalarandand_tilde_class;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
28
typedef struct _andand_tilde {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
29
30
31
32
  t_object x_obj;
  t_float x_f;
} t_andand_tilde;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
33
typedef struct _scalarandand_tilde {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
34
35
36
37
38
  t_object x_obj;
  t_float x_f;
  t_float x_g;    	    /* inlet value */
} t_scalarandand_tilde;

Ivica Bukvic's avatar
Ivica Bukvic committed
39
static void *andand_tilde_new(t_symbol * UNUSED(s), int argc, t_atom *argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
40
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  if (argc > 1) {
    post("&&~: extra arguments ignored");
  }
  if (argc) {
    t_scalarandand_tilde *x = (t_scalarandand_tilde *)pd_new(
                                scalarandand_tilde_class);
    floatinlet_new(&x->x_obj, &x->x_g);
    x->x_g = atom_getfloatarg(0, argc, argv);
    outlet_new(&x->x_obj, gensym("signal"));
    x->x_f = 0;
    return (x);
  } else {
    t_andand_tilde *x = (t_andand_tilde *)pd_new(andand_tilde_class);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("signal"), gensym("signal"));
    outlet_new(&x->x_obj, gensym("signal"));
    x->x_f = 0;
    return (x);
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
59
60
61
62
63
64
65
66
}

static t_int *andand_tilde_perform(t_int *w)
{
  t_sample *in1 = (t_sample *)(w[1]);
  t_sample *in2 = (t_sample *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int n = (int)(w[4]);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
67
  while (n--) {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
68
69
70
71
72
73
74
75
76
77
78
79
80
    int f=(int)*in1++;
    int g=(int)*in2++;
    *out++ = (f && g);
  }
  return (w+5);
}

static t_int *andand_tilde_perf8(t_int *w)
{
  t_sample *in1 = (t_sample *)(w[1]);
  t_sample *in2 = (t_sample *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int n = (int)(w[4]);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  for (; n; n -= 8, in1 += 8, in2 += 8, out += 8) {
    int f0 = in1[0], f1 = in1[1], f2 = in1[2], f3 = in1[3];
    int f4 = in1[4], f5 = in1[5], f6 = in1[6], f7 = in1[7];

    int g0 = in2[0], g1 = in2[1], g2 = in2[2], g3 = in2[3];
    int g4 = in2[4], g5 = in2[5], g6 = in2[6], g7 = in2[7];

    out[0] = f0 && g0;
    out[1] = f1 && g1;
    out[2] = f2 && g2;
    out[3] = f3 && g3;
    out[4] = f4 && g4;
    out[5] = f5 && g5;
    out[6] = f6 && g6;
    out[7] = f7 && g7;
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
97
98
99
100
101
102
103
104
105
  return (w+5);
}

static t_int *scalarandand_tilde_perform(t_int *w)
{
  t_sample *in = (t_sample *)(w[1]);
  int f = *(t_float *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int n = (int)(w[4]);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
106
107
108
  while (n--) {
    *out++ = (int)*in++ && f;
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
109
110
111
112
113
114
115
116
117
  return (w+5);
}

static t_int *scalarandand_tilde_perf8(t_int *w)
{
  t_sample *in = (t_sample *)(w[1]);
  int g = *(t_float *)(w[2]);
  t_sample *out = (t_sample *)(w[3]);
  int n = (int)(w[4]);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
118
119
120
121
122
123
124
125
126
127
128
129
130
  for (; n; n -= 8, in += 8, out += 8) {
    int f0 = in[0], f1 = in[1], f2 = in[2], f3 = in[3];
    int f4 = in[4], f5 = in[5], f6 = in[6], f7 = in[7];

    out[0] = f0 && g;
    out[1] = f1 && g;
    out[2] = f2 && g;
    out[3] = f3 && g;
    out[4] = f4 && g;
    out[5] = f5 && g;
    out[6] = f6 && g;
    out[7] = f7 && g;
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
131
132
133
134
  return (w+5);
}

#ifdef __SSE__
Jonathan Wilkes's avatar
Jonathan Wilkes committed
135
static int l_bitmask[]= {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
136
137
138
139
140
141
142
143

static t_int *andand_tilde_performSSE(t_int *w)
{
  __m128 *in1 = (__m128 *)(w[1]);
  __m128 *in2 = (__m128 *)(w[2]);
  __m128 *out = (__m128 *)(w[3]);
  int n = (int)(w[4])>>4;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
144
145
  const __m128  bitmask= _mm_loadu_ps((float*)
                                      l_bitmask); /* for getting the absolute value */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
146
147
148
149
150
151
  const __m128 one    = _mm_set1_ps(1.f);

  while (n--) {
    __m128 xmm0, xmm1, xmm2;
    xmm0   = _mm_and_ps  (in1[0] , bitmask); /* =abs(f); */
    xmm1   = _mm_and_ps  (in2[0] , bitmask);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
152
153
    xmm0   = _mm_cmpge_ps(xmm0   ,
                          one);     /* =(abs(f)>=1.0)=i (a weird cast to integer) */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    xmm1   = _mm_cmpge_ps(xmm1   , one);
    xmm2   = _mm_and_ps  (xmm0   , xmm1);    /* =(i0&&i1) */
    out[0] = _mm_and_ps  (xmm2   , one);     /* 0xfffffff -> 1.0 */

    xmm0   = _mm_and_ps  (in1[1] , bitmask);
    xmm1   = _mm_and_ps  (in2[1] , bitmask);
    xmm0   = _mm_cmpge_ps(xmm0   , one);
    xmm1   = _mm_cmpge_ps(xmm1   , one);
    xmm2   = _mm_and_ps  (xmm0   , xmm1);
    out[1] = _mm_and_ps  (xmm2   , one);

    xmm0   = _mm_and_ps  (in1[2] , bitmask);
    xmm1   = _mm_and_ps  (in2[2] , bitmask);
    xmm0   = _mm_cmpge_ps(xmm0   , one);
    xmm1   = _mm_cmpge_ps(xmm1   , one);
    xmm2   = _mm_and_ps  (xmm0   , xmm1);
    out[2] = _mm_and_ps  (xmm2   , one);

    xmm0   = _mm_and_ps  (in1[3] , bitmask);
    xmm1   = _mm_and_ps  (in2[3] , bitmask);
    xmm0   = _mm_cmpge_ps(xmm0   , one);
    xmm1   = _mm_cmpge_ps(xmm1   , one);
    xmm2   = _mm_and_ps  (xmm0   , xmm1);
    out[3] = _mm_and_ps  (xmm2   , one);

    in1+=4;
    in2+=4;
    out+=4;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
182
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
183
184
185
186
187
188
189
190
191
192
193

  return (w+5);
}
static t_int *scalarandand_tilde_performSSE(t_int *w)
{
  __m128 *in = (__m128 *)(w[1]);
  __m128 *out = (__m128 *)(w[3]);
  float f = *(t_float *)(w[2]);
  __m128 scalar = _mm_set1_ps(f);
  int n = (int)(w[4])>>4;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
194
195
  const __m128  bitmask= _mm_loadu_ps((float*)
                                      l_bitmask); /* for getting the absolute value */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
196
197
198
199
200
201
202
203
204
  const __m128 one    = _mm_set1_ps(1.f);

  scalar   = _mm_and_ps  (scalar, bitmask);
  scalar   = _mm_cmpge_ps(scalar, one );


  while (n--) {
    __m128 xmm0, xmm1;
    xmm0   = _mm_and_ps  (in[0], bitmask); /* =abs(f); */
Jonathan Wilkes's avatar
Jonathan Wilkes committed
205
206
    xmm0   = _mm_cmpge_ps(xmm0 ,
                          one);     /* =(abs(f)>=1.0)=i (a weird cast to integer) */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    xmm0   = _mm_and_ps  (xmm0 , scalar);  /* =(i0&&i1) */
    out[0] = _mm_and_ps  (xmm0 , one);     /* 0xfffffff -> 1.0 */

    xmm1   = _mm_and_ps  (in[1], bitmask);
    xmm1   = _mm_cmpge_ps(xmm1 , one);
    xmm1   = _mm_and_ps  (xmm1 , scalar);
    out[1] = _mm_and_ps  (xmm1 , one);

    xmm0   = _mm_and_ps  (in[2], bitmask);
    xmm0   = _mm_cmpge_ps(xmm0 , one);
    xmm0   = _mm_and_ps  (xmm0 , scalar);
    out[2] = _mm_and_ps  (xmm0 , one);

    xmm1   = _mm_and_ps  (in[3], bitmask);
    xmm1   = _mm_cmpge_ps(xmm1 , one);
    xmm1   = _mm_and_ps  (xmm1 , scalar);
    out[3] = _mm_and_ps  (xmm1 , one);

    in +=4;
    out+=4;
  }
  return (w+5);
}
#endif /* __SSE__ */

Ivica Bukvic's avatar
Ivica Bukvic committed
232
static void andand_tilde_dsp(t_andand_tilde *UNUSED(x), t_signal **sp)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
233
234
235
236
237
238
239
240
241
{
  t_sample*in1=sp[0]->s_vec;
  t_sample*in2=sp[1]->s_vec;
  t_sample*out=sp[2]->s_vec;

  int n=sp[0]->s_n;

#ifdef __SSE__
  if(
Jonathan Wilkes's avatar
Jonathan Wilkes committed
242
243
244
245
246
247
248
249
    Z_SIMD_CHKBLOCKSIZE(n)&&
    Z_SIMD_CHKALIGN(in1)&&
    Z_SIMD_CHKALIGN(in2)&&
    Z_SIMD_CHKALIGN(out)&&
    ZEXY_TYPE_EQUAL(t_sample, float)
  ) {
    dsp_add(andand_tilde_performSSE, 4, in1, in2, out, n);
  } else
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
250
#endif
Jonathan Wilkes's avatar
Jonathan Wilkes committed
251
252
253
254
255
    if (n&7) {
      dsp_add(andand_tilde_perform, 4, in1, in2, out, n);
    } else {
      dsp_add(andand_tilde_perf8, 4, in1, in2, out, n);
    }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
256
257
258
259
260
261
262
263
264
265
}

static void scalarandand_tilde_dsp(t_scalarandand_tilde *x, t_signal **sp)
{
  t_sample*in =sp[0]->s_vec;
  t_sample*out=sp[1]->s_vec;
  int n       =sp[0]->s_n;

#ifdef __SSE__
  if(
Jonathan Wilkes's avatar
Jonathan Wilkes committed
266
267
268
269
270
271
272
    Z_SIMD_CHKBLOCKSIZE(n)&&
    Z_SIMD_CHKALIGN(in)&&
    Z_SIMD_CHKALIGN(out)&&
    ZEXY_TYPE_EQUAL(t_sample, float)
  ) {
    dsp_add(scalarandand_tilde_performSSE, 4, in, &x->x_g, out, n);
  } else
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
273
#endif
Jonathan Wilkes's avatar
Jonathan Wilkes committed
274
275
276
277
278
    if (n&7) {
      dsp_add(scalarandand_tilde_perform, 4, in, &x->x_g, out, n);
    } else {
      dsp_add(scalarandand_tilde_perf8,   4, in, &x->x_g, out, n);
    }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
279
280
281
282
}

static void andand_tilde_help(t_object*x)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
283
  post("\n"HEARTSYMBOL " &&~\t\t:: logical AND operation on 2 signals");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
284
285
286
287
}

void setup_0x260x260x7e(void)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
288
289
290
291
  andand_tilde_class = class_new(gensym("&&~"),
                                 (t_newmethod)andand_tilde_new, 0,
                                 sizeof(t_andand_tilde), 0, A_GIMME, 0);
  class_addmethod(andand_tilde_class, (t_method)andand_tilde_dsp,
292
                  gensym("dsp"), A_CANT, 0);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
293
  CLASS_MAINSIGNALIN(andand_tilde_class, t_andand_tilde, x_f);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
294
295
  class_addmethod  (andand_tilde_class, (t_method)andand_tilde_help,
                    gensym("help"), A_NULL);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
296
297
298
299
  class_sethelpsymbol(andand_tilde_class, gensym("zigbinops"));


  scalarandand_tilde_class = class_new(gensym("&&~"), 0, 0,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
300
                                       sizeof(t_scalarandand_tilde), 0, 0);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
301
  CLASS_MAINSIGNALIN(scalarandand_tilde_class, t_scalarandand_tilde, x_f);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
302
  class_addmethod(scalarandand_tilde_class, (t_method)scalarandand_tilde_dsp,
303
                  gensym("dsp"), A_CANT,
Jonathan Wilkes's avatar
Jonathan Wilkes committed
304
305
306
                  0);
  class_addmethod  (scalarandand_tilde_class, (t_method)andand_tilde_help,
                    gensym("help"), A_NULL);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
307
308
309
310
  class_sethelpsymbol(scalarandand_tilde_class, gensym("zigbinops"));

  zexy_register("&&~");
}
Ivica Bukvic's avatar
Ivica Bukvic committed
311
312
313
314

#ifndef ZEXY_LIBRARY
void setup(void)
{
315
  setup_0x260x26_tilde();
Ivica Bukvic's avatar
Ivica Bukvic committed
316
317
}
#endif