File: integer_manip.c

package info (click to toggle)
python-ltfatpy 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,412 kB
  • sloc: ansic: 8,546; python: 6,470; makefile: 15
file content (286 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download | duplicates (5)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "ltfat.h"


LTFAT_EXTERN_TOO void
fftindex(const ltfatInt N, ltfatInt *indexout)
{
    ltfatInt ii;

    if (N%2==0)
    {
        for (ii=0; ii<N/2+1; ii++)
        {
            indexout[ii]=ii;
        }
        for (ii=N/2; ii<N-1; ii++)
        {
            indexout[ii+1]=-N+ii+1;
        }
    }
    else
    {
        for (ii=0; ii<(N-1)/2+1; ii++)
        {
            indexout[ii]=ii;
        }
        for (ii=(N-1)/2; ii<N-1; ii++)
        {
            indexout[ii+1]=-N+ii+1;
        }
    }

}

LTFAT_EXTERN_TOO ltfatInt
imax(const ltfatInt a, const ltfatInt b)
{
   return (a > b ? a : b);
}

LTFAT_EXTERN_TOO ltfatInt
imin(const ltfatInt a, const ltfatInt b)
{
   return (a < b ? a : b);
}

LTFAT_EXTERN_TOO ltfatInt
makelarger(const ltfatInt L, const ltfatInt K)
{
    /* This is a floor operation */
    ltfatInt o = (L/K)*K;

    /* Make it a ceil */
    if (L%K>0)
    {
        o += K;
    }

    return o;
}

/* Extended Euclid algorithm. */
LTFAT_EXTERN_TOO ltfatInt
gcd (const ltfatInt a, const ltfatInt b, ltfatInt *r, ltfatInt *s )
{
    ltfatInt a1 = a;
    ltfatInt b1 = b;
    ltfatInt a2 = 1;
    ltfatInt b2 = 0;
    ltfatInt a3 = 0;
    ltfatInt b3 = 1;
    ltfatInt c, d;
    while ( b1 != 0 )
    {
        d=a1/b1;
        c = a1;
        a1 = b1;
        b1 = c-d*b1;

        c = a2;
        a2 = b2;
        b2 = c-d*b2;

        c = a3;
        a3 = b3;
        b3 = c-d*b3;

    }

    *r=a2;
    *s=a3;
    return a1;
}

LTFAT_EXTERN_TOO ltfatInt
lcm(const ltfatInt a, const ltfatInt b)
{
    ltfatInt junk_r, junk_s;

    ltfatInt c = gcd(a, b, &junk_r, &junk_s);

    return (a*b/c);
}


LTFAT_EXTERN_TOO void
gabimagepars(const ltfatInt Ls, const ltfatInt x, const ltfatInt y,
             ltfatInt *a, ltfatInt *M, ltfatInt *L, ltfatInt *N, ltfatInt *Ngood)
{


    *M = imin(y,Ls);
    *N = imax(x,Ls);

    /* Determine the minimum transform size. */
    ltfatInt K = lcm(*M,*N);

    /* This L is good, but is it not the same as DGT will choose. */
    ltfatInt Llong = makelarger(Ls,K);

    /* Fix a from the long L */
    *a=Llong/(*N);

    /* Now we have fixed a and M, so we can use the standard method of choosing L. */
    ltfatInt Lsmallest=lcm(*a,*M);
    *L = makelarger(Ls, Lsmallest);

    /* We did not get N as desired. */
    *N=*L/(*a);

    /* Number of columns to display */
    *Ngood=(Ls/(*a));
}

/* Determine the size of the output array of wfacreal and iwfacreal */
LTFAT_EXTERN_TOO ltfatInt
wfacreal_size(const ltfatInt L, const ltfatInt a, const ltfatInt M)
{

    ltfatInt h_a, h_m;

    const ltfatInt b=L/M;
    const ltfatInt c=gcd(a, M,&h_a, &h_m);
    const ltfatInt p=a/c;
    const ltfatInt d=b/p;

    /* This is a floor operation. */
    const ltfatInt d2= d/2+1;

    return d2*p*M;

}

LTFAT_EXTERN_TOO ltfatInt
nextPow2(const ltfatInt y)
{
    ltfatInt x = (ltfatInt) y;
    ltfatInt bits = sizeof(x)*8;

    if(x==0)
        return 1;

    x--;
    (x) = ((x)>>1)  | (x);
    (x) = ((x)>>2)  | (x);
    (x) = ((x)>>4)  | (x);
    (x) = ((x)>>8)  | (x);
    (x) = ((x)>>16) | (x);
    if(bits>32)
        (x) = ((x)>>32) | (x);

    (x)++;
    return x;
}

LTFAT_EXTERN_TOO ltfatInt
nextfastfft(const ltfatInt x)
{
   ltfatInt xtmp = x;
    while (1)
    {
        ltfatInt m = xtmp;

        while ((m % 2) == 0)
            m /= 2;
        while ((m % 3) == 0)
            m /= 3;
        while ((m % 5) == 0)
            m /= 5;
        if (m <= 1)
            break;                    /* n is completely factorable by twos, threes, and fives */
        xtmp++;
    }
    return xtmp;
}

LTFAT_EXTERN_TOO ltfatInt
pow2(const ltfatInt x)
{
    return ((1)<<(x));
}

LTFAT_EXTERN_TOO ltfatInt
modPow2(const ltfatInt x, const ltfatInt pow2var)
{
    return ((x)&(pow2var-1));
}

LTFAT_EXTERN_TOO int
isPow2(const ltfatInt x)
{
    return x==nextPow2(x);
}

LTFAT_EXTERN_TOO int
ilog2(const ltfatInt x)
{
    ltfatInt tmp = 0;
    ltfatInt xtmp = x;
     while (xtmp >>= 1) ++tmp;
    return tmp;
}

// integer power by squaring
LTFAT_EXTERN_TOO ltfatInt
ipow(const ltfatInt base, const ltfatInt exp)
{
    ltfatInt baseTmp = (ltfatInt) base;
    ltfatInt expTmp = (ltfatInt) exp;
    ltfatInt result = 1;

    while (expTmp)
    {
        if (expTmp & 1)
            result *= baseTmp;
        expTmp >>= 1;
        baseTmp *= baseTmp;
    }

    return result;
}

LTFAT_EXTERN_TOO ltfatInt
filterbank_td_size(const ltfatInt L, const ltfatInt a, const ltfatInt gl,
                   const ltfatInt offset, const ltfatExtType ext)
{
    ltfatInt Lc = 0;
    if(ext==PER)
    {
        Lc = (ltfatInt) ceil( L/((double)a) );
    }
    else if(ext==VALID)
    {
        Lc = (ltfatInt) ceil( (L-(gl-1))/((double)a) );

    }
    else
    {
        Lc = (ltfatInt) ceil( (L + gl - 1 + offset )/((double)a) );
    }
    return Lc;
}

LTFAT_EXTERN_TOO ltfatInt
ltfat_round(const double x)
{
    if (x < 0.0)
        return (ltfatInt)(x - 0.5);
    else
        return (ltfatInt)(x + 0.5);
}

LTFAT_EXTERN_TOO ltfatInt
positiverem(const ltfatInt a,const ltfatInt b)
{
    const ltfatInt c = a%b;
    return(c<0 ? c+b : c);
}

LTFAT_EXTERN_TOO ltfatInt
rangelimit(const ltfatInt a, const ltfatInt amin, const ltfatInt amax)
{
    ltfatInt c = a < amin? amin:a;
    c = c > amax? amax: c;
    return c;
}