blob: 86698d8db3b550d6cf27f9797e7b7e9a4f4d030b [file] [log] [blame]
Patrick Georgid08996e2011-03-01 07:23:49 +00001/*
2 * This file is part of the libpayload project.
3 *
Julius Werner7a8a4ab2015-05-22 16:26:40 -07004 * Copyright 2015 Google Inc.
Patrick Georgid08996e2011-03-01 07:23:49 +00005 *
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 Werner7a8a4ab2015-05-22 16:26:40 -070017 * 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 Georgid08996e2011-03-01 07:23:49 +000021 * 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 Werner7a8a4ab2015-05-22 16:26:40 -070034#include <libpayload.h>
Patrick Georgi7f965832011-04-21 18:57:16 +020035
Julius Werner7a8a4ab2015-05-22 16:26:40 -070036/*
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
41int __clzsi2(u32 a);
42int __clzsi2(u32 a)
Patrick Georgid08996e2011-03-01 07:23:49 +000043{
Julius Werner7a8a4ab2015-05-22 16:26:40 -070044 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 Georgid08996e2011-03-01 07:23:49 +000051
Julius Werner7a8a4ab2015-05-22 16:26:40 -070052 if (!(a & (0xffff << 16))) {
53 r += 16;
54 a <<= 16;
Patrick Georgid08996e2011-03-01 07:23:49 +000055 }
Julius Werner7a8a4ab2015-05-22 16:26:40 -070056
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 Georgid08996e2011-03-01 07:23:49 +000068}