blob: 2e11e48446abf1bf078163575e10a33a8028febc [file] [log] [blame]
Chanho Min4df87bb2013-07-08 16:01:43 -07001/*
2 * lib/clz_ctz.c
3 *
4 * Copyright (C) 2013 Chanho Min <chanho.min@lge.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
Rashika Kheria3c516cd2014-04-03 14:49:09 -07009 * The functions in this file aren't called directly, but are required by
10 * GCC builtins such as __builtin_ctz, and therefore they can't be removed
11 * despite appearing unreferenced in kernel source.
Chanho Min4df87bb2013-07-08 16:01:43 -070012 *
13 * __c[lt]z[sd]i2 can be overridden by linking arch-specific versions.
14 */
15
16#include <linux/export.h>
17#include <linux/kernel.h>
18
Rashika Kheria3c516cd2014-04-03 14:49:09 -070019int __weak __ctzsi2(int val);
Chanho Min4df87bb2013-07-08 16:01:43 -070020int __weak __ctzsi2(int val)
21{
22 return __ffs(val);
23}
24EXPORT_SYMBOL(__ctzsi2);
25
Rashika Kheria3c516cd2014-04-03 14:49:09 -070026int __weak __clzsi2(int val);
Chanho Min4df87bb2013-07-08 16:01:43 -070027int __weak __clzsi2(int val)
28{
29 return 32 - fls(val);
30}
31EXPORT_SYMBOL(__clzsi2);
32
Rashika Kheria3c516cd2014-04-03 14:49:09 -070033int __weak __clzdi2(long val);
34int __weak __ctzdi2(long val);
Chanho Min4df87bb2013-07-08 16:01:43 -070035#if BITS_PER_LONG == 32
36
37int __weak __clzdi2(long val)
38{
39 return 32 - fls((int)val);
40}
41EXPORT_SYMBOL(__clzdi2);
42
43int __weak __ctzdi2(long val)
44{
45 return __ffs((u32)val);
46}
47EXPORT_SYMBOL(__ctzdi2);
48
49#elif BITS_PER_LONG == 64
50
51int __weak __clzdi2(long val)
52{
53 return 64 - fls64((u64)val);
54}
55EXPORT_SYMBOL(__clzdi2);
56
57int __weak __ctzdi2(long val)
58{
59 return __ffs64((u64)val);
60}
61EXPORT_SYMBOL(__ctzdi2);
62
63#else
64#error BITS_PER_LONG not 32 or 64
65#endif