<feed xmlns='http://www.w3.org/2005/Atom'>
<title>user/sven/linux.git/include/asm-generic/getorder.h, branch v4.7</title>
<subtitle>Linux Kernel
</subtitle>
<id>https://git.stealer.net/cgit.cgi/user/sven/linux.git/atom?h=v4.7</id>
<link rel='self' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/atom?h=v4.7'/>
<link rel='alternate' type='text/html' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/'/>
<updated>2012-02-24T18:39:27Z</updated>
<entry>
<title>bitops: Add missing parentheses to new get_order macro</title>
<updated>2012-02-24T18:39:27Z</updated>
<author>
<name>Joerg Roedel</name>
<email>joerg.roedel@amd.com</email>
</author>
<published>2012-02-24T12:58:15Z</published>
<link rel='alternate' type='text/html' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/commit/?id=b893485db994b17402524d3d700b950294cb6c97'/>
<id>urn:sha1:b893485db994b17402524d3d700b950294cb6c97</id>
<content type='text'>
The new get_order macro introcuded in commit

	d66acc39c7cee323733c8503b9de1821a56dff7e

does not use parentheses around all uses of the parameter n.
This causes new compile warnings, for example in the
amd_iommu_init.c function:

drivers/iommu/amd_iommu_init.c:561:6: warning: suggest parentheses around comparison in operand of ‘&amp;’ [-Wparentheses]
drivers/iommu/amd_iommu_init.c:561:6: warning: suggest parentheses around comparison in operand of ‘&amp;’ [-Wparentheses]

Fix those warnings by adding the missing parentheses.

Reported-by: Ingo Molnar &lt;mingo@elte.hu&gt;
Cc: David Howells &lt;dhowells@redhat.com&gt;
Acked-by: Arnd Bergmann &lt;arnd@arndb.de&gt;
Signed-off-by: Joerg Roedel &lt;joerg.roedel@amd.com&gt;
Link: http://lkml.kernel.org/r/1330088295-28732-1-git-send-email-joerg.roedel@amd.com
Signed-off-by: H. Peter Anvin &lt;hpa@linux.intel.com&gt;
</content>
</entry>
<entry>
<title>bitops: Optimise get_order()</title>
<updated>2012-02-20T22:47:02Z</updated>
<author>
<name>David Howells</name>
<email>dhowells@redhat.com</email>
</author>
<published>2012-02-20T22:39:29Z</published>
<link rel='alternate' type='text/html' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/commit/?id=d66acc39c7cee323733c8503b9de1821a56dff7e'/>
<id>urn:sha1:d66acc39c7cee323733c8503b9de1821a56dff7e</id>
<content type='text'>
Optimise get_order() to use bit scanning instructions if such exist rather than
a loop.  Also, make it possible to use get_order() in static initialisations
too by building it on top of ilog2() in the constant parameter case.

This has been tested for i386 and x86_64 using the following userspace program,
and for FRV by making appropriate substitutions for fls() and fls64().  It will
abort if the case for get_order() deviates from the original except for the
order of 0, for which get_order() produces an undefined result.  This program
tests both dynamic and static parameters.

	#include &lt;stdlib.h&gt;
	#include &lt;stdio.h&gt;

	#ifdef __x86_64__
	#define BITS_PER_LONG 64
	#else
	#define BITS_PER_LONG 32
	#endif

	#define PAGE_SHIFT 12

	typedef unsigned long long __u64, u64;
	typedef unsigned int __u32, u32;
	#define noinline	__attribute__((noinline))

	static inline int fls(int x)
	{
		int bitpos = -1;

		asm("bsrl %1,%0"
		    : "+r" (bitpos)
		    : "rm" (x));
		return bitpos + 1;
	}

	static __always_inline int fls64(__u64 x)
	{
	#if BITS_PER_LONG == 64
		long bitpos = -1;

		asm("bsrq %1,%0"
		    : "+r" (bitpos)
		    : "rm" (x));
		return bitpos + 1;
	#else
		__u32 h = x &gt;&gt; 32, l = x;
		int bitpos = -1;

		asm("bsrl	%1,%0	\n"
		    "subl	%2,%0	\n"
		    "bsrl	%3,%0	\n"
		    : "+r" (bitpos)
		    : "rm" (l), "i"(32), "rm" (h));

		return bitpos + 33;
	#endif
	}

	static inline __attribute__((const))
	int __ilog2_u32(u32 n)
	{
		return fls(n) - 1;
	}

	static inline __attribute__((const))
	int __ilog2_u64(u64 n)
	{
		return fls64(n) - 1;
	}

	extern __attribute__((const, noreturn))
	int ____ilog2_NaN(void);

	#define ilog2(n)				\
	(						\
		__builtin_constant_p(n) ? (		\
			(n) &lt; 1 ? ____ilog2_NaN() :	\
			(n) &amp; (1ULL &lt;&lt; 63) ? 63 :	\
			(n) &amp; (1ULL &lt;&lt; 62) ? 62 :	\
			(n) &amp; (1ULL &lt;&lt; 61) ? 61 :	\
			(n) &amp; (1ULL &lt;&lt; 60) ? 60 :	\
			(n) &amp; (1ULL &lt;&lt; 59) ? 59 :	\
			(n) &amp; (1ULL &lt;&lt; 58) ? 58 :	\
			(n) &amp; (1ULL &lt;&lt; 57) ? 57 :	\
			(n) &amp; (1ULL &lt;&lt; 56) ? 56 :	\
			(n) &amp; (1ULL &lt;&lt; 55) ? 55 :	\
			(n) &amp; (1ULL &lt;&lt; 54) ? 54 :	\
			(n) &amp; (1ULL &lt;&lt; 53) ? 53 :	\
			(n) &amp; (1ULL &lt;&lt; 52) ? 52 :	\
			(n) &amp; (1ULL &lt;&lt; 51) ? 51 :	\
			(n) &amp; (1ULL &lt;&lt; 50) ? 50 :	\
			(n) &amp; (1ULL &lt;&lt; 49) ? 49 :	\
			(n) &amp; (1ULL &lt;&lt; 48) ? 48 :	\
			(n) &amp; (1ULL &lt;&lt; 47) ? 47 :	\
			(n) &amp; (1ULL &lt;&lt; 46) ? 46 :	\
			(n) &amp; (1ULL &lt;&lt; 45) ? 45 :	\
			(n) &amp; (1ULL &lt;&lt; 44) ? 44 :	\
			(n) &amp; (1ULL &lt;&lt; 43) ? 43 :	\
			(n) &amp; (1ULL &lt;&lt; 42) ? 42 :	\
			(n) &amp; (1ULL &lt;&lt; 41) ? 41 :	\
			(n) &amp; (1ULL &lt;&lt; 40) ? 40 :	\
			(n) &amp; (1ULL &lt;&lt; 39) ? 39 :	\
			(n) &amp; (1ULL &lt;&lt; 38) ? 38 :	\
			(n) &amp; (1ULL &lt;&lt; 37) ? 37 :	\
			(n) &amp; (1ULL &lt;&lt; 36) ? 36 :	\
			(n) &amp; (1ULL &lt;&lt; 35) ? 35 :	\
			(n) &amp; (1ULL &lt;&lt; 34) ? 34 :	\
			(n) &amp; (1ULL &lt;&lt; 33) ? 33 :	\
			(n) &amp; (1ULL &lt;&lt; 32) ? 32 :	\
			(n) &amp; (1ULL &lt;&lt; 31) ? 31 :	\
			(n) &amp; (1ULL &lt;&lt; 30) ? 30 :	\
			(n) &amp; (1ULL &lt;&lt; 29) ? 29 :	\
			(n) &amp; (1ULL &lt;&lt; 28) ? 28 :	\
			(n) &amp; (1ULL &lt;&lt; 27) ? 27 :	\
			(n) &amp; (1ULL &lt;&lt; 26) ? 26 :	\
			(n) &amp; (1ULL &lt;&lt; 25) ? 25 :	\
			(n) &amp; (1ULL &lt;&lt; 24) ? 24 :	\
			(n) &amp; (1ULL &lt;&lt; 23) ? 23 :	\
			(n) &amp; (1ULL &lt;&lt; 22) ? 22 :	\
			(n) &amp; (1ULL &lt;&lt; 21) ? 21 :	\
			(n) &amp; (1ULL &lt;&lt; 20) ? 20 :	\
			(n) &amp; (1ULL &lt;&lt; 19) ? 19 :	\
			(n) &amp; (1ULL &lt;&lt; 18) ? 18 :	\
			(n) &amp; (1ULL &lt;&lt; 17) ? 17 :	\
			(n) &amp; (1ULL &lt;&lt; 16) ? 16 :	\
			(n) &amp; (1ULL &lt;&lt; 15) ? 15 :	\
			(n) &amp; (1ULL &lt;&lt; 14) ? 14 :	\
			(n) &amp; (1ULL &lt;&lt; 13) ? 13 :	\
			(n) &amp; (1ULL &lt;&lt; 12) ? 12 :	\
			(n) &amp; (1ULL &lt;&lt; 11) ? 11 :	\
			(n) &amp; (1ULL &lt;&lt; 10) ? 10 :	\
			(n) &amp; (1ULL &lt;&lt;  9) ?  9 :	\
			(n) &amp; (1ULL &lt;&lt;  8) ?  8 :	\
			(n) &amp; (1ULL &lt;&lt;  7) ?  7 :	\
			(n) &amp; (1ULL &lt;&lt;  6) ?  6 :	\
			(n) &amp; (1ULL &lt;&lt;  5) ?  5 :	\
			(n) &amp; (1ULL &lt;&lt;  4) ?  4 :	\
			(n) &amp; (1ULL &lt;&lt;  3) ?  3 :	\
			(n) &amp; (1ULL &lt;&lt;  2) ?  2 :	\
			(n) &amp; (1ULL &lt;&lt;  1) ?  1 :	\
			(n) &amp; (1ULL &lt;&lt;  0) ?  0 :	\
			____ilog2_NaN()			\
					   ) :		\
		(sizeof(n) &lt;= 4) ?			\
		__ilog2_u32(n) :			\
		__ilog2_u64(n)				\
	 )

	static noinline __attribute__((const))
	int old_get_order(unsigned long size)
	{
		int order;

		size = (size - 1) &gt;&gt; (PAGE_SHIFT - 1);
		order = -1;
		do {
			size &gt;&gt;= 1;
			order++;
		} while (size);
		return order;
	}

	static noinline __attribute__((const))
	int __get_order(unsigned long size)
	{
		int order;
		size--;
		size &gt;&gt;= PAGE_SHIFT;
	#if BITS_PER_LONG == 32
		order = fls(size);
	#else
		order = fls64(size);
	#endif
		return order;
	}

	#define get_order(n)						\
	(								\
		__builtin_constant_p(n) ? (				\
			(n == 0UL) ? BITS_PER_LONG - PAGE_SHIFT :	\
			((n &lt; (1UL &lt;&lt; PAGE_SHIFT)) ? 0 :		\
			 ilog2((n) - 1) - PAGE_SHIFT + 1)		\
		) :							\
		__get_order(n)						\
	)

	#define order(N) \
		{ (1UL &lt;&lt; N) - 1,	get_order((1UL &lt;&lt; N) - 1)	},	\
		{ (1UL &lt;&lt; N),		get_order((1UL &lt;&lt; N))		},	\
		{ (1UL &lt;&lt; N) + 1,	get_order((1UL &lt;&lt; N) + 1)	}

	struct order {
		unsigned long n, order;
	};

	static const struct order order_table[] = {
		order(0),
		order(1),
		order(2),
		order(3),
		order(4),
		order(5),
		order(6),
		order(7),
		order(8),
		order(9),
		order(10),
		order(11),
		order(12),
		order(13),
		order(14),
		order(15),
		order(16),
		order(17),
		order(18),
		order(19),
		order(20),
		order(21),
		order(22),
		order(23),
		order(24),
		order(25),
		order(26),
		order(27),
		order(28),
		order(29),
		order(30),
		order(31),
	#if BITS_PER_LONG == 64
		order(32),
		order(33),
		order(34),
		order(35),
	#endif
		{ 0x2929 }
	};

	void check(int loop, unsigned long n)
	{
		unsigned long old, new;

		printf("[%2d]: %09lx | ", loop, n);

		old = old_get_order(n);
		new = get_order(n);

		printf("%3ld, %3ld\n", old, new);
		if (n != 0 &amp;&amp; old != new)
			abort();
	}

	int main(int argc, char **argv)
	{
		const struct order *p;
		unsigned long n;
		int loop;

		for (loop = 0; loop &lt;= BITS_PER_LONG - 1; loop++) {
			n = 1UL &lt;&lt; loop;
			check(loop, n - 1);
			check(loop, n);
			check(loop, n + 1);
		}

		for (p = order_table; p-&gt;n != 0x2929; p++) {
			unsigned long old, new;

			old = old_get_order(p-&gt;n);
			new = p-&gt;order;
			printf("%09lx\t%3ld, %3ld\n", p-&gt;n, old, new);
			if (p-&gt;n != 0 &amp;&amp; old != new)
				abort();
		}

		return 0;
	}

Disassembling the x86_64 version of the above code shows:

	0000000000400510 &lt;old_get_order&gt;:
	  400510:       48 83 ef 01             sub    $0x1,%rdi
	  400514:       b8 ff ff ff ff          mov    $0xffffffff,%eax
	  400519:       48 c1 ef 0b             shr    $0xb,%rdi
	  40051d:       0f 1f 00                nopl   (%rax)
	  400520:       83 c0 01                add    $0x1,%eax
	  400523:       48 d1 ef                shr    %rdi
	  400526:       75 f8                   jne    400520 &lt;old_get_order+0x10&gt;
	  400528:       f3 c3                   repz retq
	  40052a:       66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

	0000000000400530 &lt;__get_order&gt;:
	  400530:       48 83 ef 01             sub    $0x1,%rdi
	  400534:       48 c7 c0 ff ff ff ff    mov    $0xffffffffffffffff,%rax
	  40053b:       48 c1 ef 0c             shr    $0xc,%rdi
	  40053f:       48 0f bd c7             bsr    %rdi,%rax
	  400543:       83 c0 01                add    $0x1,%eax
	  400546:       c3                      retq
	  400547:       66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
	  40054e:       00 00

As can be seen, the new __get_order() function is simpler than the
old_get_order() function.

Signed-off-by: David Howells &lt;dhowells@redhat.com&gt;
Link: http://lkml.kernel.org/r/20120220223928.16199.29548.stgit@warthog.procyon.org.uk
Acked-by: Arnd Bergmann &lt;arnd@arndb.de&gt;
Signed-off-by: H. Peter Anvin &lt;hpa@zytor.com&gt;
</content>
</entry>
<entry>
<title>bitops: Adjust the comment on get_order() to describe the size==0 case</title>
<updated>2012-02-20T22:46:55Z</updated>
<author>
<name>David Howells</name>
<email>dhowells@redhat.com</email>
</author>
<published>2012-02-20T22:39:18Z</published>
<link rel='alternate' type='text/html' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/commit/?id=e0891a9816316b5e05fd5b0453ffe9fd6a56f489'/>
<id>urn:sha1:e0891a9816316b5e05fd5b0453ffe9fd6a56f489</id>
<content type='text'>
Adjust the comment on get_order() to note that the result of passing a size of
0 results in an undefined value.

Signed-off-by: David Howells &lt;dhowells@redhat.com&gt;
Link: http://lkml.kernel.org/r/20120220223917.16199.9416.stgit@warthog.procyon.org.uk
Acked-by: Arnd Bergmann &lt;arnd@arndb.de&gt;
Signed-off-by: H. Peter Anvin &lt;hpa@zytor.com&gt;
</content>
</entry>
<entry>
<title>asm-generic: rename page.h and uaccess.h</title>
<updated>2009-06-11T19:02:17Z</updated>
<author>
<name>Arnd Bergmann</name>
<email>arnd@arndb.de</email>
</author>
<published>2009-05-13T22:56:30Z</published>
<link rel='alternate' type='text/html' href='https://git.stealer.net/cgit.cgi/user/sven/linux.git/commit/?id=5b17e1cd8928ae65932758ce6478ac6d3e9a86b2'/>
<id>urn:sha1:5b17e1cd8928ae65932758ce6478ac6d3e9a86b2</id>
<content type='text'>
The current asm-generic/page.h only contains the get_order
function, and asm-generic/uaccess.h only implements
unaligned accesses. This renames the file to getorder.h
and uaccess-unaligned.h to make room for new page.h
and uaccess.h file that will be usable by all simple
(e.g. nommu) architectures.

Signed-off-by: Remis Lima Baima &lt;remis.developer@googlemail.com&gt;
Signed-off-by: Arnd Bergmann &lt;arnd@arndb.de&gt;
</content>
</entry>
</feed>
