File: log2comp.h

package info (click to toggle)
asterisk 1%3A16.2.1~dfsg-1%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 59,360 kB
  • sloc: ansic: 743,813; sh: 12,460; python: 5,085; sql: 4,427; makefile: 3,117; perl: 3,103; javascript: 3,018; yacc: 2,161; cpp: 2,099; xml: 997; tcl: 113; php: 62
file content (74 lines) | stat: -rw-r--r-- 1,461 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*! \file
 * \brief log2comp.h - various base 2 log computation versions
 *
 * Asterisk -- An open source telephony toolkit.
 *
 * \author Alex Volkov <codepro@usa.net>
 *
 * Copyright (c) 2004 - 2005, Digium Inc.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 *
 * Define WANT_ASM before including this file to use assembly
 *   whenever possible
 */

#if defined(_MSC_VER)
#	define inline __inline
#elif defined(__GNUC__)
#	define inline __inline__
#else
#	define inline
#endif

#if defined(WANT_ASM) && defined(_MSC_VER) && defined(_M_IX86)
/* MS C Inline Asm */
#	pragma warning( disable : 4035 )
static inline int ilog2(int val) { __asm
{
	xor		eax, eax
	dec		eax
	bsr		eax, val
}}
#	pragma warning( default : 4035 )
#elif defined(WANT_ASM) && defined(__GNUC__) && (defined(__i386__) || defined(i386))
/* GNU Inline Asm */
static inline int ilog2(int val)
{
	int a;
	__asm__
	("\
		xorl	%0, %0		;\
		decl	%0			;\
		bsrl	%1, %0		;\
		"
		: "=&r" (a)
		: "mr" (val)
		: "cc"
	);
	return a;
}
#elif defined(WANT_ASM) && defined(__GNUC__) && defined(__powerpc__)
static inline int ilog2(int val)
{
	int a;
	__asm__ ("cntlzw %0,%1"
		 : "=r" (a)
		 : "r" (val)
		 );
	return 31-a;
}
#else
/* no ASM for this compiler and/or platform */
/* rather slow base 2 log computation
 * Using looped shift.
 */
static inline int ilog2(int val)
{
	int i;
	for (i = -1; val; ++i, val >>= 1)
		;
	return (i);
}
#endif