summaryrefslogtreecommitdiffstats
path: root/newlib/libc/machine/riscv/ffs.c
blob: 6522077225bba6ecb21825ec1293041a3d8a0d6c (plain)
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
/* Copyright (c) 2017  SiFive Inc. All rights reserved.

   This copyrighted material is made available to anyone wishing to use,
   modify, copy, or redistribute it subject to the terms and conditions
   of the BSD License.   This program is distributed in the hope that
   it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
   including the implied warranties of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  A copy of this license is available at
   http://www.opensource.org/licenses.
*/
#include <strings.h>

int
ffs (int word)
{
#if __riscv_xlen == 32
  return (__builtin_ffs (word));
#else
  int i;

  if (!word)
    return 0;

  i = 0;
  for (;;)
    {
      if (((1 << i++) & word) != 0)
       return i;
    }
  return 0;
#endif
}