Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## Asterisk developer's documentation

# log2comp.h

Go to the documentation of this file.

    00001 /*! \file
    00002  * \brief log2comp.h - various base 2 log computation versions
    00003  *
    00004  * Asterisk -- A telephony toolkit for Linux.
    00005  *
    00006  * \author Alex Volkov <codepro@usa.net>
    00007  *
    00008  * Copyright (c) 2004 - 2005, Digium Inc.
    00009  *
    00010  * This program is free software, distributed under the terms of
    00011  * the GNU General Public License
    00012  *
    00013  * Define WANT_ASM before including this file to use assembly
    00014  *   whenever possible
    00015  */
    00016
    00017 #if defined(_MSC_VER)
    00018 #  define inline __inline
    00019 #elif defined(__GNUC__)
    00020 #  define inline __inline__
    00021 #else
    00022 #  define inline
    00023 #endif
    00024
    00025 #if defined(WANT_ASM) && defined(_MSC_VER) && defined(_M_IX86)
    00026 /* MS C Inline Asm */
    00027 #  pragma warning( disable : 4035 )
    00028 static inline int ilog2(int val) { __asm
    00029 {
    00030    xor      eax, eax
    00031    dec      eax
    00032    bsr      eax, val
    00033 }}
    00034 #  pragma warning( default : 4035 )
    00035 #elif defined(WANT_ASM) && defined(__GNUC__) && (defined(__i386__) || defined(i386))
    00036 /* GNU Inline Asm */
    00037 static inline int ilog2(int val)
    00038 {
    00039    int a;
    00040    __asm__
    00041    ("\
    00042       xorl  %0, %0      ;\
    00043       decl  %0       ;\
    00044       bsrl  %1, %0      ;\
    00045       "
    00046       : "=r" (a)
    00047       : "mr" (val)
    00048       : "cc"
    00049    );
    00050    return a;
    00051 }
    00052 #elif defined(WANT_ASM) && defined(__GNUC__) && defined(__powerpc__)
    00053 static inline int ilog2(int val)
    00054 {
    00055    int a;
    00056    __asm__ ("cntlzw %0,%1"
    00057        : "=r" (a)
    00058        : "r" (val)
    00059        );
    00060    return 31-a;
    00061 }
    00062 #else
    00063 /* no ASM for this compiler and/or platform */
    00064 /* rather slow base 2 log computation
    00065  * Using looped shift.
    00066  */
    00067 static inline int ilog2(int val)
    00068 {
    00069    int i;
    00070    for (i = -1; val; ++i, val >>= 1)
    00071       ;
    00072    return (i);
    00073 }
    00074 #endif