static irqreturn_t sprd_dma_irq(int irq, void *dev_id) { int i; u32 irq_status; spin_lock(&dma_lock); irq_status = __raw_readl(DMA_INT_STS); if (unlikely(0 == irq_status)) { spin_unlock(&dma_lock); return IRQ_NONE; } __raw_writel(0xffffffff, DMA_TRANSF_INT_CLR); __raw_writel(0xffffffff, DMA_BURST_INT_CLR); __raw_writel(0xffffffff, DMA_LISTDONE_INT_CLR); spin_unlock(&dma_lock); while (irq_status) { i = DMA_CHN_MAX - __builtin_clz(irq_status); irq_status &= ~(1<<i); if (sprd_irq_handlers[i].handler) { sprd_irq_handlers[i].handler(i, sprd_irq_handlers[i].dev_id); } else { printk(KERN_ERR "DMA channel %d needs handler!\n", i); } } return IRQ_HANDLED; }

int clz (unsigned int x) { if (__builtin_clz (x)) return x; return -1; }

static int pick_sensor(struct sensors_data_context_t *dev, sensors_data_t* values) { uint32_t mask = SUPPORTED_SENSORS; while (mask) { uint32_t i = 31 - __builtin_clz(mask); mask &= ~(1<<i); if (dev->pendingSensors & (1<<i)) { dev->pendingSensors &= ~(1<<i); *values = dev->sensors[i]; values->sensor = id_to_sensor[i]; LOGV_IF(0, "%d [%f, %f, %f]", values->sensor, values->vector.x, values->vector.y, values->vector.z); #if 0 LOGE("_______________sensor is:%d [%f, %f, %f],i is:%d", values->sensor, values->vector.x, values->vector.y, values->vector.z, i); #endif return i; } } LOGE("no sensor to return: pendingSensors = %08x", dev->pendingSensors); return -1; }

uint32_t next_power_of_two(uint32_t n) { uint32_t val = 1 << (31 - __builtin_clz(n)); if (n % val) val *= 2; return val; }

void test_i32(int P) { leading = __builtin_clz(P); trailing = __builtin_ctz(P); // CHECK: @test_i32 // CHECK: call i32 @llvm.ctlz.i32(i32 {{.*}}, i1 false) // CHECK: call i32 @llvm.cttz.i32(i32 {{.*}}, i1 false) }

int get_array_position_malloc(unsigned int num_dwords) { if (num_dwords > INITIAL_HEAP_SIZE) return FREE_LIST_SIZE - 1; // last element of the free list is reserved for blocks larger than 512 dwords unsigned int num_set_bits =__builtin_popcount(num_dwords); if (num_set_bits > 1) { // not a power of 2. Return one position up return sizeof(int)*8 - __builtin_clz(num_dwords); } else if (num_set_bits == 1) { // is a power of 2. Return this position return sizeof(int)*8 - __builtin_clz(num_dwords)-1; } else { // this is a zero. Should not get here return -1; } }

void permute(type* begin, type* end, unsigned n=0) { unsigned size=end-begin; if(size==0) return; if(n==0) n=size*__builtin_clz(size)*2; for(unsigned i=0; i<n; ++i) swap(*(begin+rd()%size), *(begin+rd()%size)); }

/* We need to find at most CHUNKS_COUNT powers of two that cover size * + all required metadata (chunked_blob, block headers) with minimal * total size. This can be greatly improved, but for now it can remain * suboptimal. Unused orders array positions are set to -1. */ static void value_size_to_block_sizes(unsigned size, int orders[CHUNKS_COUNT]) { unsigned original_size = size; (void) original_size; unsigned thing; int i; /* we'll need exactly one chunked_blob struct header */ size += sizeof(struct chunked_blob); if (size <= (2U << MIN_ORDER)) { if (size <= (1U << MIN_ORDER)) { thing = (1U << MIN_ORDER); goto skip_upper_bound; } thing = (2U << MIN_ORDER); goto skip_upper_bound; } i = CHUNKS_COUNT; thing = 0; while (--i >= 0 && thing != size) { /* count leading zeros of remaining difference and * find highest set bit of it */ int order = sizeof(unsigned) * 8 - __builtin_clz(size - thing) - 1; if (order < MIN_ORDER) { thing += (1U << MIN_ORDER); goto skip_upper_bound; } thing |= (1U << order); } /* size is still smaller then thing. Add into smallest set bit * of thing to make it larger then size. */ if (thing != size) thing += (1U << __builtin_ctz(thing)); skip_upper_bound: i = 0; double prop = (double)(thing-size)/thing; /* printf("thing/original_size: %f, %d\n", (double)(thing - original_size) / thing, original_size); */ assert(prop >= 0 && prop < 1.0); while (thing) { int order = sizeof(unsigned) * 8 - __builtin_clz(thing) - 1; orders[i++] = order; thing -= (1U << order); } for (;i < CHUNKS_COUNT; i++) orders[i] = -1; }

/** \brief Return the number of leading zeros bits in a nonzero unsigned integer. */ unsigned nlz_core(unsigned x) { SASSERT(x != 0); #ifdef __GNUC__ return __builtin_clz(x); #else return 31 - msb_pos(x); #endif }

void spiSetBitRate(int prescaler, int divider) { prescaler = MIN(MAX(0, prescaler - 1), 7); divider = 30 - __builtin_clz(divider); divider = MIN(MAX(0, divider), 8); SPI1_BR = SPI_BR_SPPR(prescaler) | SPI_BR_SPR(divider); }

void bsp_interrupt_dispatch(void) { rtems_vector_number vector = 31 - __builtin_clz(VICIRQStatus); bsp_interrupt_handler_dispatch(vector); VICVectAddr = 0; }

static inline uint32_t _uint32_cntlz( uint32_t x ) { /* On PowerPC, this will map to: cntlzw */ /* On Pentium, this will map to: clz */ uint32_t nlz = __builtin_clz( x ); return (nlz); }

void mpi_mul_u32(const mpi *a, uint32_t b, mpi *p) { if (mpi_is_zero(a) || b == 0) { mpi_zero(p); return; } else if (b == 1) { if (a != p) mpi_set_mpi(p, a); return; } else if ((b & (b-1)) == 0) { /* B is a power of 2 */ mpi_lshift(a, __builtin_ctz(b), p); return; } else if (b == (mp_digit)b) { /* B fits in an mp_digit */ if (a == p) { mp_digit cy = mp_dmuli(p->digits, p->size, (mp_digit)b); if (cy) { MPI_MIN_ALLOC(p, p->size + 1); p->digits[p->size++] = cy; } } else { MPI_MIN_ALLOC(p, a->size); mp_digit cy = mp_dmul(a->digits, a->size, (mp_digit)b, p->digits); if (cy) { MPI_MIN_ALLOC(p, a->size + 1); p->digits[a->size] = cy; p->size = a->size + 1; } else { p->size = a->size; } } } else { unsigned bits = CHAR_BIT * sizeof(uint32_t) - __builtin_clz(b); mp_size size = (bits + MP_DIGIT_BITS - 1) / MP_DIGIT_BITS; mp_digit *bp = MP_TMP_ALLOC(size); #if MP_DIGIT_BITS >= 32 bp[0] = b; #else for (mp_size j=0; j<size; j++) { bp[j] = (mp_digit)b; b >>= MP_DIGIT_BITS; } #endif if (a == p) { mp_digit *tmp = MP_TMP_ALLOC(p->size + size); mp_mul(p->digits, p->size, bp, size, tmp); MPI_MIN_ALLOC(p, p->size + size); mp_copy(tmp, p->size + size, p->digits); p->size = mp_rsize(p->digits, p->size + size); MP_TMP_FREE(tmp); } else { MPI_MIN_ALLOC(p, a->size + size); mp_mul(a->digits, a->size, bp, size, p->digits); p->size = mp_rsize(p->digits, a->size + size); } MP_TMP_FREE(bp); } }

static inline DItype __udivmoddi4 (UDItype x, UDItype y, DItype *res) { static unsigned char unrt[256] = {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20, -22, -23, -25, -27, -29, -31, -32, -34, -36, -38, -39, -41, -43, -44, -46, -48, -49, -51, -53, -54, -56, -57, -59, -61, -62, -64, -65, -67, -68, -70, -71, -73, -74, -76, -77, -78, -80, -81, -83, -84, -86, -87, -88, -90, -91, -92, -94, -95, -96, -98, -99, -100, -102, -103, -104, -105, -107, -108, -109, -110, -112, -113, -114, -115, -117, -118, -119, -120, -121, -122, -124, -125, -126, -127, -128, 127, 126, 125, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 88, 87, 86, 85, 84, 83, 82, 81, 80, 80, 79, 78, 77, 76, 75, 74, 74, 73, 72, 71, 70, 70, 69, 68, 67, 66, 66, 65, 64, 63, 62, 62, 61, 60, 59, 59, 58, 57, 56, 56, 55, 54, 53, 53, 52, 51, 50, 50, 49, 48, 48, 47, 46, 46, 45, 44, 43, 43, 42, 41, 41, 40, 39, 39, 38, 37, 37, 36, 35, 35, 34, 33, 33, 32, 32, 31, 30, 30, 29, 28, 28, 27, 27, 26, 25, 25, 24, 24, 23, 22, 22, 21, 21, 20, 19, 19, 18, 18, 17, 17, 16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 10, 10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0}; BIT_RESIZE(y,32); // table lookup start #define W 64 unsigned char k32 =__builtin_clz(y); UDItype lshifted_y = ((UDItype)(((USItype)y) << k32))<<32; /// remove the leading 1 lshifted_y = lshifted_y << 1; UDItype ty = lsr64(lshifted_y, W-8 ); // prescaling UDItype t = unrt[ ty ] | 256; // table lookup UDItype z = lsr64(lsl64(t,W-9), (W-32-1)-k32 ); // postscaling // z recurrence UDItype my = 0-y; //#define NR_UNROLLED #ifdef NR_UNROLLED z = z + umulh64(z,mul64(my,z)); z = z + umulh64(z,mul64(my,z)); z = z + umulh64(z,mul64(my,z)); #else unsigned int index; for (index = 0; index < 3; ++index) { UDItype zd = umulh64(z,mul64(my,z)); //if (zd == 0) break; z = z + zd; } #endif // q estimate UDItype q = umulh64(x,z); UDItype r = x - mul64(y,q); // q refinement if (r >= y) { r = r - y; q = q + 1; if (r >= y) { r = r - y; q = q + 1; if (r >= y) { //add this in case of three iterations r = r - y; q = q + 1; } } } if (res != 0) *res = r; return q; }

si_int __clzdi2(di_int a) { dwords x; x.all = a; const si_int f = -(x.s.high == 0); return __builtin_clz((x.s.high & ~f) | (x.s.low & f)) + (f & ((si_int)(sizeof(si_int) * CHAR_BIT))); }

static int highestPriAudio(void) { int i; if (audioActive == 0) return -1; return (31 - __builtin_clz(audioActive)); }

void BuildRangeQueries() { int L = 31 - __builtin_clz(N) + 1; RLCP = vector<VI>(L, VI(N)); RLCP[0] = LCP; for (int i = 1; i < L; ++i) { for (int j = 0; j+(1<<(i-1)) < N; ++j) RLCP[i][j] = min(RLCP[i - 1][j], RLCP[i-1][j + (1<<(i-1))]); } }

int sip_send_wmm_params(struct esp_pub *epub, u8 aci, const struct ieee80211_tx_queue_params *params) { struct sk_buff *skb = NULL; struct sip_cmd_set_wmm_params* bsscmd; skb = sip_alloc_ctrl_skbuf(epub->sip, sizeof(struct sip_cmd_set_wmm_params) + sizeof(struct sip_hdr), SIP_CMD_SET_WMM_PARAM); if (!skb) return -EINVAL; bsscmd = (struct sip_cmd_set_wmm_params *)(skb->data + sizeof(struct sip_hdr)); bsscmd->aci= aci; bsscmd->aifs=params->aifs; bsscmd->txop_us=params->txop*32; bsscmd->ecw_min = 32 - __builtin_clz(params->cw_min); bsscmd->ecw_max= 32 -__builtin_clz(params->cw_max); return sip_cmd_enqueue(epub->sip, skb); }

int __clrsbhi2 (word_type x) { if (x < 0) x = ~x; if (x == 0) return 15; return __builtin_clz (x) - 1; }

void test_i32(int P) { leading = __builtin_clz(P); trailing = __builtin_ctz(P); pop = __builtin_popcount(P); // CHECK: @test_i32 // CHECK: call i32 @llvm.ctlz.i32 // CHECK: call i32 @llvm.cttz.i32 // CHECK: call i32 @llvm.ctpop.i32 }

size_t RingBuffer_countLeadingZeroes(size_t arg) { if (arg == 0) { return 32; } return __builtin_clz((UInt32)arg); }

inline size_t countLeadingZeroes(size_t arg) { if (arg == 0) { return 32; } return __builtin_clz((UInt32)arg); }

LowestCommonAncestor( int root, const AdjacencyList<EdgeType> &graph) : m_depth_table(graph.size(), -1) , m_skip_table( graph.size(), std::vector<int>(32 - __builtin_clz(graph.size()), -1)) { std::vector<int> s; build_tables(root, s, graph); }

unsigned log2_roundup(unsigned n) { /* Calculate the floor of log2(n) */ unsigned l2 = 31 - __builtin_clz(n); /* If n == 2^log2(n), floor(n) == n so we can return l2. */ if (n == 1U<<l2) return l2; /* else floor(n) != n, so return l2+1 to round up. */ return l2+1; }

/* Convert IP address's netmask into integer. We assume netmask is sequential one. Argument netmask should be network byte order. */ u_char ip_masklen(struct in_addr netmask) { uint32_t tmp = ~ntohl(netmask.s_addr); if (tmp) /* clz: count leading zeroes. sadly, the behaviour of this builtin * is undefined for a 0 argument, even though most CPUs give 32 */ return __builtin_clz(tmp); else return 32; }

void check_clz(int n) { // RECOVER: builtins.cpp:[[@LINE+1]]:17: runtime error: passing zero to clz(), which is not a valid argument __builtin_clz(n); // RECOVER: builtins.cpp:[[@LINE+1]]:18: runtime error: passing zero to clz(), which is not a valid argument __builtin_clzl(n); // RECOVER: builtins.cpp:[[@LINE+1]]:19: runtime error: passing zero to clz(), which is not a valid argument __builtin_clzll(n); }

void bsp_interrupt_dispatch(void) { unsigned reg_ie = GBA_REG_IE; unsigned reg_if = GBA_REG_IF & reg_ie; rtems_vector_number vector = 31 - __builtin_clz(reg_if); bsp_interrupt_handler_dispatch(vector); GBA_REG_IF = 1 << vector; }

/* Returns floor(log(n)/log(2)) * @n any unsigned integer * @return nonzero if n is a power of two */ int ilog2(unsigned int n) { # if defined(__GNUC__) || defined(__clang__) return (n) ? (31 - __builtin_clz(n)) : (n); # else int y = 0; while (n >>= 1) ++y; return y; # endif }

inline int bits(int a) { unsigned int aa = abs(a); // int b=0; if (a==0) return 0; return 32-__builtin_clz(aa); // while ( (aa/=2) > 0 ) ++b; // return (a>0) ? b : -b; }

static size_t timeInterp(uint32_t bytes, size_t* lut) { int lz = __builtin_clz(bytes / 4); int log_size = 32 - lz; size_t x2 = 1 << (log_size + 2); size_t y2 = lut[log_size]; size_t x1 = 1 << (log_size + 1); size_t y1 = lut[log_size - 1]; return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1; }