1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 25b17e1cdSArnd Bergmann #ifndef __ASM_GENERIC_GETORDER_H 35b17e1cdSArnd Bergmann #define __ASM_GENERIC_GETORDER_H 45b17e1cdSArnd Bergmann 55b17e1cdSArnd Bergmann #ifndef __ASSEMBLY__ 65b17e1cdSArnd Bergmann 75b17e1cdSArnd Bergmann #include <linux/compiler.h> 8d66acc39SDavid Howells #include <linux/log2.h> 9d66acc39SDavid Howells 10e0891a98SDavid Howells /** 11e0891a98SDavid Howells * get_order - Determine the allocation order of a memory size 12e0891a98SDavid Howells * @size: The size for which to get the order 13e0891a98SDavid Howells * 14e0891a98SDavid Howells * Determine the allocation order of a particular sized block of memory. This 15e0891a98SDavid Howells * is on a logarithmic scale, where: 16e0891a98SDavid Howells * 17e0891a98SDavid Howells * 0 -> 2^0 * PAGE_SIZE and below 18e0891a98SDavid Howells * 1 -> 2^1 * PAGE_SIZE to 2^0 * PAGE_SIZE + 1 19e0891a98SDavid Howells * 2 -> 2^2 * PAGE_SIZE to 2^1 * PAGE_SIZE + 1 20e0891a98SDavid Howells * 3 -> 2^3 * PAGE_SIZE to 2^2 * PAGE_SIZE + 1 21e0891a98SDavid Howells * 4 -> 2^4 * PAGE_SIZE to 2^3 * PAGE_SIZE + 1 22e0891a98SDavid Howells * ... 23e0891a98SDavid Howells * 24e0891a98SDavid Howells * The order returned is used to find the smallest allocation granule required 25e0891a98SDavid Howells * to hold an object of the specified size. 26e0891a98SDavid Howells * 27e0891a98SDavid Howells * The result is undefined if the size is 0. 28e0891a98SDavid Howells */ get_order(unsigned long size)29*0a571b08SChristophe Leroystatic __always_inline __attribute_const__ int get_order(unsigned long size) 30cbedfe11SQian Cai { 31cbedfe11SQian Cai if (__builtin_constant_p(size)) { 32cbedfe11SQian Cai if (!size) 33cbedfe11SQian Cai return BITS_PER_LONG - PAGE_SHIFT; 34cbedfe11SQian Cai 35cbedfe11SQian Cai if (size < (1UL << PAGE_SHIFT)) 36cbedfe11SQian Cai return 0; 37cbedfe11SQian Cai 38cbedfe11SQian Cai return ilog2((size) - 1) - PAGE_SHIFT + 1; 39cbedfe11SQian Cai } 40cbedfe11SQian Cai 41cbedfe11SQian Cai size--; 42cbedfe11SQian Cai size >>= PAGE_SHIFT; 43cbedfe11SQian Cai #if BITS_PER_LONG == 32 44cbedfe11SQian Cai return fls(size); 45cbedfe11SQian Cai #else 46cbedfe11SQian Cai return fls64(size); 47cbedfe11SQian Cai #endif 48cbedfe11SQian Cai } 495b17e1cdSArnd Bergmann 505b17e1cdSArnd Bergmann #endif /* __ASSEMBLY__ */ 515b17e1cdSArnd Bergmann 525b17e1cdSArnd Bergmann #endif /* __ASM_GENERIC_GETORDER_H */ 53