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
|
/*
* Copyright (c) 2002, 2017 Jens Keiner, Stefan Kunis, Daniel Potts
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "infft.h"
INT Y(exp2i)(const INT a)
{
return (1U << a);
}
/**
* Return floor(log2(x)) for an integer input x. If x is zero or negative this
* method returns -1.
*
* see https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
*/
INT Y(log2i)(const INT m)
{
/* Special case, zero or negative input. */
if (m <= 0)
return -1;
#if SIZEOF_PTRDIFF_T == 8
/* Hash map with return values based on De Bruijn sequence. */
static INT debruijn[64] =
{
0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49,
18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15,
8, 23, 7, 6, 5, 63
};
register uint64_t v = (uint64_t)(m);
/* Round down to one less than a power of 2. */
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v |= v >> 32;
/* 0x03f6eaf2cd271461 is a hexadecimal representation of a De Bruijn
* sequence for binary words of length 6. The binary representation
* starts with 000000111111. This is required to make it work with one less
* than a power of 2 instead of an actual power of 2.
*/
return debruijn[(uint64_t)(v * 0x03f6eaf2cd271461LU) >> 58];
#elif SIZEOF_PTRDIFF_T == 4
/* Hash map with return values based on De Bruijn sequence. */
static INT debruijn[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28,
15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
register uint32_t v = (uint32_t)(m);
/* Round down to one less than a power of 2. */
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
/* 0x07C4ACDD is a hexadecimal representation of a De Bruijn sequence for
* binary words of length 5. The binary representation starts with
* 0000011111. This is required to make it work with one less than a power of
* 2 instead of an actual power of 2.
*/
return debruijn[(uint32_t)(v * 0x07C4ACDDU) >> 27];
#else
#error Incompatible size of ptrdiff_t
#endif
}
/**
* Calculate next power of two larger or equal to a given integer value.
*
* If the input is negative, this method returns -1. As a special case, 2 is
* returned when the input is 1. In all other cases, the smallest power of 2
* larger or equal to the input is returned.
*
* see https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
*/
INT Y(next_power_of_2)(const INT x)
{
if (x < 0)
return -1;
else if (x < 2)
return x + 1;
else
{
uint64_t v = (uint64_t)x;
/* Subtract one, so we return the input if it is a power of two. */
v--;
/* Round down to one less than a power of two. */
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
#if SIZEOF_PTRDIFF_T >= 2
v |= v >> 8;
#endif
#if SIZEOF_PTRDIFF_T >= 4
v |= v >> 16;
#endif
#if SIZEOF_PTRDIFF_T >= 8
v |= v >> 32;
#endif
/* Add one to get power of two. */
v++;
return v;
}
}
/** Computes /f$n\ge N/f$ such that /f$n=2^j,\, j\in\mathhb{N}_0/f$.
*/
void Y(next_power_of_2_exp)(const INT N, INT *N2, INT *t)
{
INT n,i,logn;
INT N_is_not_power_of_2=0;
if (N == 0)
{
*N2 = 1;
*t = 0;
}
else
{
n = N;
logn = 0;
while (n != 1)
{
if (n%2 == 1)
{
N_is_not_power_of_2=1;
}
n = n/2;
logn++;
}
if (!N_is_not_power_of_2)
{
logn--;
}
for (i = 0; i <= logn; i++)
{
n = n*2;
}
*N2 = n;
*t = logn+1;
}
}
void Y(next_power_of_2_exp_int)(const int N, int *N2, int *t)
{
int n,i,logn;
int N_is_not_power_of_2=0;
if (N == 0)
{
*N2 = 1;
*t = 0;
}
else
{
n = N;
logn = 0;
while (n != 1)
{
if (n%2 == 1)
{
N_is_not_power_of_2=1;
}
n = n/2;
logn++;
}
if (!N_is_not_power_of_2)
{
logn--;
}
for (i = 0; i <= logn; i++)
{
n = n*2;
}
*N2 = n;
*t = logn+1;
}
}
|