NFFT  3.3.2
int.c
1 /*
2  * Copyright (c) 2002, 2016 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "infft.h"
20 
21 INT Y(exp2i)(const INT a)
22 {
23  return (1U << a);
24 }
25 
26 INT Y(log2i)(const INT m)
27 {
28  INT l = 0;
29  INT mm = m;
30 
31  while (mm > 0)
32  {
33  mm = (mm >> 1);
34  l++;
35  }
36  return (l-1);
37 }
38 
41 INT Y(next_power_of_2)(const INT N)
42 {
43  INT n,i,logn;
44  INT N_is_not_power_of_2=0;
45 
46  if (N == 0)
47  return 1;
48  else if (N == 1)
49  return 2;
50  else
51  {
52  n = N;
53  logn = 0;
54  while (n != 1)
55  {
56  if (n%2 == 1)
57  N_is_not_power_of_2=1;
58  n = n/2;
59  logn++;
60  }
61 
62  if (!N_is_not_power_of_2)
63  logn--;
64 
65  for (i = 0; i <= logn; i++)
66  n = n*2;
67 
68  return n;
69  }
70 }
71 
74 void Y(next_power_of_2_exp)(const INT N, INT *N2, INT *t)
75 {
76  INT n,i,logn;
77  INT N_is_not_power_of_2=0;
78 
79  if (N == 0)
80  {
81  *N2 = 1;
82  *t = 0;
83  }
84  else
85  {
86  n = N;
87  logn = 0;
88  while (n != 1)
89  {
90  if (n%2 == 1)
91  {
92  N_is_not_power_of_2=1;
93  }
94  n = n/2;
95  logn++;
96  }
97 
98  if (!N_is_not_power_of_2)
99  {
100  logn--;
101  }
102 
103  for (i = 0; i <= logn; i++)
104  {
105  n = n*2;
106  }
107 
108  *N2 = n;
109  *t = logn+1;
110  }
111 }
112 
113 void Y(next_power_of_2_exp_int)(const int N, int *N2, int *t)
114 {
115  int n,i,logn;
116  int N_is_not_power_of_2=0;
117 
118  if (N == 0)
119  {
120  *N2 = 1;
121  *t = 0;
122  }
123  else
124  {
125  n = N;
126  logn = 0;
127  while (n != 1)
128  {
129  if (n%2 == 1)
130  {
131  N_is_not_power_of_2=1;
132  }
133  n = n/2;
134  logn++;
135  }
136 
137  if (!N_is_not_power_of_2)
138  {
139  logn--;
140  }
141 
142  for (i = 0; i <= logn; i++)
143  {
144  n = n*2;
145  }
146 
147  *N2 = n;
148  *t = logn+1;
149  }
150 }