Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 4 | * Copyright 2015 Google Inc. |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 17 | * Alternatively, this software may be distributed under the terms of the |
| 18 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 19 | * Software Foundation. |
| 20 | * |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 34 | #include <libpayload.h> |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 35 | |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Provide platform-independent backend implementation for __builtin_clz() in |
| 38 | * <libpayload.h> in case GCC does not have an assembly version for this arch. |
| 39 | */ |
| 40 | |
| 41 | int __clzsi2(u32 a); |
| 42 | int __clzsi2(u32 a) |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 43 | { |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 44 | static const u8 four_bit_table[] = { |
| 45 | [0x0] = 4, [0x1] = 3, [0x2] = 2, [0x3] = 2, |
| 46 | [0x4] = 1, [0x5] = 1, [0x6] = 1, [0x7] = 1, |
| 47 | [0x8] = 0, [0x9] = 0, [0xa] = 0, [0xb] = 0, |
| 48 | [0xc] = 0, [0xd] = 0, [0xe] = 0, [0xf] = 0, |
| 49 | }; |
| 50 | int r = 0; |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 51 | |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 52 | if (!(a & (0xffff << 16))) { |
| 53 | r += 16; |
| 54 | a <<= 16; |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 55 | } |
Julius Werner | 7a8a4ab | 2015-05-22 16:26:40 -0700 | [diff] [blame] | 56 | |
| 57 | if (!(a & (0xff << 24))) { |
| 58 | r += 8; |
| 59 | a <<= 8; |
| 60 | } |
| 61 | |
| 62 | if (!(a & (0xf << 28))) { |
| 63 | r += 4; |
| 64 | a <<= 4; |
| 65 | } |
| 66 | |
| 67 | return r + four_bit_table[a >> 28]; |
Patrick Georgi | d08996e | 2011-03-01 07:23:49 +0000 | [diff] [blame] | 68 | } |