glusterfs
61 строка · 1.6 Кб
1/* bit search implementation
2*
3* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4* Written by David Howells (dhowells@redhat.com)
5*
6* Copyright (C) 2008 IBM Corporation
7* 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
8* (Inspired by David Howell's find_next_bit implementation)
9*
10* Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
11* size and improve performance, 2015.
12*
13* This program is free software; you can redistribute it and/or
14* modify it under the terms of the GNU General Public License
15* as published by the Free Software Foundation; either version
16* 2 of the License, or (at your option) any later version.
17*/
18
19/**
20* @find_last_bit
21* optimized implementation of find last bit in
22*/
23
24#ifndef BITS_PER_LONG25#ifdef __LP64__26#define BITS_PER_LONG 6427#else28#define BITS_PER_LONG 3229#endif30#endif31
32unsigned long gw_tw_fls (unsigned long word)33{
34int num = BITS_PER_LONG;35
36#if BITS_PER_LONG == 6437if (!(word & (~0ul << 32))) {38num -= 32;39word <<= 32;40}41#endif42if (!(word & (~0ul << (BITS_PER_LONG-16)))) {43num -= 16;44word <<= 16;45}46if (!(word & (~0ul << (BITS_PER_LONG-8)))) {47num -= 8;48word <<= 8;49}50if (!(word & (~0ul << (BITS_PER_LONG-4)))) {51num -= 4;52word <<= 4;53}54if (!(word & (~0ul << (BITS_PER_LONG-2)))) {55num -= 2;56word <<= 2;57}58if (!(word & (~0ul << (BITS_PER_LONG-1))))59num -= 1;60return num;61}
62