Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | /*
* Copyright (c) 2016 Jean-Paul Etienne <fractalclone@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _ASM_INLINE_GCC_H
#define _ASM_INLINE_GCC_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* The file must not be included directly
* Include arch/cpu.h instead
* NOTE: only pulpino-specific NOT RISCV32
*/
#ifndef _ASMLANGUAGE
#include <toolchain.h>
/*
* Account for pulpino-specific bit manipulation opcodes only when
* CONFIG_RISCV_GENERIC_TOOLCHAIN is not set
*/
#ifndef CONFIG_RISCV_GENERIC_TOOLCHAIN
/**
*
* @brief find least significant bit set in a 32-bit word
*
* This routine finds the first bit set starting from the least significant bit
* in the argument passed in and returns the index of that bit. Bits are
* numbered starting at 1 from the least significant bit. A return value of
* zero indicates that the value passed is zero.
*
* @return least significant bit set, 0 if @a op is 0
*/
static ALWAYS_INLINE unsigned int find_lsb_set(u32_t op)
{
unsigned int ret;
if (!op)
return 0;
__asm__ volatile ("p.ff1 %[d], %[a]"
: [d] "=r" (ret)
: [a] "r" (op));
return ret + 1;
}
/**
*
* @brief find most significant bit set in a 32-bit word
*
* This routine finds the first bit set starting from the most significant bit
* in the argument passed in and returns the index of that bit. Bits are
* numbered starting at 1 from the least significant bit. A return value of
* zero indicates that the value passed is zero.
*
* @return most significant bit set, 0 if @a op is 0
*/
static ALWAYS_INLINE unsigned int find_msb_set(u32_t op)
{
unsigned int ret;
if (!op)
return 0;
__asm__ volatile ("p.fl1 %[d], %[a]"
: [d] "=r" (ret)
: [a] "r" (op));
return ret + 1;
}
#else /* CONFIG_RISCV_GENERIC_TOOLCHAIN */
/*
* When compiled with a riscv32 generic toolchain, use
* __builtin_ffs and __builtin_clz to handle respectively
* find_lsb_set and find_msb_set.
*/
static ALWAYS_INLINE unsigned int find_lsb_set(u32_t op)
{
return __builtin_ffs(op);
}
static ALWAYS_INLINE unsigned int find_msb_set(u32_t op)
{
if (!op)
return 0;
return 32 - __builtin_clz(op);
}
#endif /* CONFIG_RISCV_GENERIC_TOOLCHAIN */
#endif /* _ASMLANGUAGE */
#ifdef __cplusplus
}
#endif
#endif /* _ASM_INLINE_GCC_PUBLIC_GCC_H */
|