NFFT  3.3.1
int.c
00001 /*
00002  * Copyright (c) 2002, 2016 Jens Keiner, Stefan Kunis, Daniel Potts
00003  *
00004  * This program is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but WITHOUT
00010  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00012  * details.
00013  *
00014  * You should have received a copy of the GNU General Public License along with
00015  * this program; if not, write to the Free Software Foundation, Inc., 51
00016  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include "infft.h"
00020 
00021 INT Y(exp2i)(const INT a)
00022 {
00023   return (1U << a);
00024 }
00025 
00026 INT Y(log2i)(const INT m)
00027 {
00028   INT l = 0;
00029   INT mm = m;
00030 
00031   while (mm > 0)
00032   {
00033     mm = (mm >> 1);
00034     l++;
00035   }
00036   return (l-1);
00037 }
00038 
00041 INT Y(next_power_of_2)(const INT N)
00042 {
00043   INT n,i,logn;
00044   INT N_is_not_power_of_2=0;
00045 
00046   if (N == 0)
00047     return 1;
00048   else if (N == 1)
00049     return 2;
00050   else
00051   {
00052     n = N;
00053     logn = 0;
00054     while (n != 1)
00055     {
00056       if (n%2 == 1)
00057         N_is_not_power_of_2=1;
00058       n = n/2;
00059       logn++;
00060     }
00061 
00062     if (!N_is_not_power_of_2)
00063       logn--;
00064 
00065     for (i = 0; i <= logn; i++)
00066       n = n*2;
00067 
00068     return n;
00069   }
00070 }
00071 
00074 void Y(next_power_of_2_exp)(const INT N, INT *N2, INT *t)
00075 {
00076   INT n,i,logn;
00077   INT N_is_not_power_of_2=0;
00078 
00079   if (N == 0)
00080   {
00081     *N2 = 1;
00082     *t = 0;
00083   }
00084   else
00085   {
00086     n = N;
00087     logn = 0;
00088     while (n != 1)
00089     {
00090       if (n%2 == 1)
00091       {
00092           N_is_not_power_of_2=1;
00093       }
00094       n = n/2;
00095       logn++;
00096     }
00097 
00098     if (!N_is_not_power_of_2)
00099     {
00100       logn--;
00101     }
00102 
00103     for (i = 0; i <= logn; i++)
00104     {
00105       n = n*2;
00106     }
00107 
00108     *N2 = n;
00109     *t = logn+1;
00110   }
00111 }
00112 
00113 void Y(next_power_of_2_exp_int)(const int N, int *N2, int *t)
00114 {
00115   int n,i,logn;
00116   int N_is_not_power_of_2=0;
00117 
00118   if (N == 0)
00119   {
00120     *N2 = 1;
00121     *t = 0;
00122   }
00123   else
00124   {
00125     n = N;
00126     logn = 0;
00127     while (n != 1)
00128     {
00129       if (n%2 == 1)
00130       {
00131           N_is_not_power_of_2=1;
00132       }
00133       n = n/2;
00134       logn++;
00135     }
00136 
00137     if (!N_is_not_power_of_2)
00138     {
00139       logn--;
00140     }
00141 
00142     for (i = 0; i <= logn; i++)
00143     {
00144       n = n*2;
00145     }
00146 
00147     *N2 = n;
00148     *t = logn+1;
00149   }
00150 }