Exemple #1
0
void *
T(aligned_alloc)(size_t alignment, size_t size, const char *here, long lineno)
{
	if (is_power_two(alignment) && sizeof (void *) <= alignment && (size / alignment) * alignment == size)
		return T(malloc)(size, here, lineno);
	return NULL;
}
Exemple #2
0
Fichier : fph.c Projet : aosp/dvp
fph_t *fph_init(size_t numBlocks, size_t blockSize, fph_key_f keyFunc)
{
    fph_t *fph = (fph_t*)malloc(sizeof(fph_t));
    if (fph)
    {
        memset(fph, 0, sizeof(fph_t));
#ifdef FPH_OPTIMIZED
        if (!is_power_two(numBlocks))
        {
            free(fph);
            return NULL;
        }
#endif
        fph->numBlocks = numBlocks;
        fph->blockSize = blockSize;
        fph->numElem = fph->numBlocks * fph->blockSize;
        if (keyFunc)
            fph->keyFunc = keyFunc;
        else
            fph->keyFunc = fph_hash;
        fph->hash = (fph_kv_t *)malloc(fph->numElem * sizeof(fph_kv_t));
        if (fph->hash)
            memset(fph->hash, 0, fph->numElem * sizeof(fph_kv_t));
#ifdef FPH_METRICS
        fph->collisions = (uint32_t *)malloc(fph->numBlocks * sizeof(uint32_t));
        if (fph->collisions)
            memset(fph->collisions, 0, fph->numBlocks * sizeof(uint32_t));
        fph->length = (uint32_t *)malloc(fph->numBlocks * sizeof(uint32_t));
        if (fph->length)
            memset(fph->length, 0, fph->numBlocks * sizeof(uint32_t));
#endif
    }
    return fph;
}
int main() {
	int x = 1;
	printf("1. divide by two recursively: %d\n", is_power_two(x));
	printf("2. count number of set bits: %d\n", count_set_bits(x));
	printf("3. subtract by one: %d\n", subtract_by_one(x));

	return 0;
}
// method 1: divide by two recursively
int is_power_two(int num) {
	
	if(num == 1 || num == 0) {
		return 0;
	}

	if(num % 2 != 0) {
		return -1;
	} 

	return is_power_two(num/2);
}