00001 /*------------------------------------------------------------------------- 00002 * 00003 * fls.c 00004 * finds the last (most significant) bit that is set 00005 * 00006 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group 00007 * 00008 * 00009 * IDENTIFICATION 00010 * src/port/fls.c 00011 * 00012 * This file was taken from FreeBSD to provide an implementation of fls() 00013 * for platforms that lack it. Note that the operating system's version may 00014 * be substantially more efficient than ours, since some platforms have an 00015 * assembly instruction that does exactly this. 00016 * 00017 * The FreeBSD copyright terms follow. 00018 */ 00019 00020 /*- 00021 * Copyright (c) 1990, 1993 00022 * The Regents of the University of California. All rights reserved. 00023 * 00024 * Redistribution and use in source and binary forms, with or without 00025 * modification, are permitted provided that the following conditions 00026 * are met: 00027 * 1. Redistributions of source code must retain the above copyright 00028 * notice, this list of conditions and the following disclaimer. 00029 * 2. Redistributions in binary form must reproduce the above copyright 00030 * notice, this list of conditions and the following disclaimer in the 00031 * documentation and/or other materials provided with the distribution. 00032 * 4. Neither the name of the University nor the names of its contributors 00033 * may be used to endorse or promote products derived from this software 00034 * without specific prior written permission. 00035 * 00036 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00037 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00038 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00039 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00040 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00041 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00042 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00043 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00044 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00045 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00046 * SUCH DAMAGE. 00047 */ 00048 00049 #include "c.h" 00050 00051 /* 00052 * Find Last Set bit 00053 */ 00054 int 00055 fls(int mask) 00056 { 00057 int bit; 00058 00059 if (mask == 0) 00060 return (0); 00061 for (bit = 1; mask != 1; bit++) 00062 mask = (unsigned int) mask >> 1; 00063 return (bit); 00064 }