//return integer with only the highest previosly set bit 1, rest - 0, which results in 2^((int)log2(n))
uint32_t hibit(uint32_t n) { //13 bitwise instructions -> 13 time units on GPUs
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return n - (n >> 1); // return (n & ~(n >> 1));
}
/* Convert iterator value to the value of the argument in the range (0.0..1.0)
* Consecutive iterations will produce more and more fine-grained sampling of argument.
*/
float argValue(uint32_t i) {
uint32_t p, q;
uint32_t h = hibit(i);
p = ((i & (h - 1)) << 1) + 1;
q = h << 1;
return (float) p / q;
}
int main(int argc, char *argv[]) {
unsigned N1 = 0xFF;
for (unsigned i = 1; i < N1; i++) {
std::cout << argValue(i) << ", ";
}
return false;
}
//0.5, 0.25, 0.75, 0.125, 0.375, 0.625, 0.875, 0.0625, 0.1875, 0.3125, 0.4375, 0.5625, 0.6875, 0.8125, 0.9375, 0.03125, 0.09375...
uint32_t hibit(uint32_t n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return(n & ~(n >> 1));//return n - (n >> 1);
}
int main(int argc, char *argv[])
{
const int Nd=1;
const int n[Nd]={66};
const int d=0;
for(uint32_t i=1; i<n[d]; i++)
{
uint32_t h=hibit(i);
uint32_t p= (i&(h-1))*2 + 1;
std::cout<<"h:"<<h<<'\t'<<"p:"<<p<<'\n';
}
}
Над приведением его к единственному итератору и обобщению на N-мерный случай еще думаю.