diff --git a/gcc/config/h8300/h8300.cc b/gcc/config/h8300/h8300.cc index 02056d0ff19..c5935f602e2 100644 --- a/gcc/config/h8300/h8300.cc +++ b/gcc/config/h8300/h8300.cc @@ -5410,6 +5410,14 @@ h8300_init_libfuncs (void) set_optab_libfunc (udiv_optab, HImode, "__udivhi3"); set_optab_libfunc (smod_optab, HImode, "__modhi3"); set_optab_libfunc (umod_optab, HImode, "__umodhi3"); + +/* The comment below comes from config/mmix/mmix.cc. + + Unfortunately, by default __builtin_ffs is expanded to ffs for + targets where INT_TYPE_SIZE < BITS_PER_WORD. That together with + newlib since 2017-07-04 implementing ffs as __builtin_ffs leads to + (newlib) ffs recursively calling itself. */ + set_optab_libfunc (ffs_optab, HImode, "__ffshi2"); } /* Worker function for TARGET_FUNCTION_VALUE. diff --git a/libgcc/config/h8300/ffshi2.c b/libgcc/config/h8300/ffshi2.c new file mode 100644 index 00000000000..f08e73f5791 --- /dev/null +++ b/libgcc/config/h8300/ffshi2.c @@ -0,0 +1,42 @@ +/* The implementation of __ffshi2. + Copyright (C) 2003-2025 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +Under Section 7 of GPL version 3, you are granted additional +permissions described in the GCC Runtime Library Exception, version +3.1, as published by the Free Software Foundation. + +You should have received a copy of the GNU General Public License and +a copy of the GCC Runtime Library Exception along with this program; +see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +. */ + +int __ffshi2 (int x); + +int +__ffshi2 (int x) +{ + int i; + + if (!x) + return 0; + + i = 0; + for (;;) + { + if (((1 << i++) & x) != 0) + return i; + } + return 0; +} diff --git a/libgcc/config/h8300/t-h8300 b/libgcc/config/h8300/t-h8300 index b6448523e34..414053fa52c 100644 --- a/libgcc/config/h8300/t-h8300 +++ b/libgcc/config/h8300/t-h8300 @@ -5,6 +5,7 @@ LIB1ASMFUNCS = _cmpsi2 _ucmpsi2 _divhi3 _divsi3 _mulhi3 _mulsi3 \ LIB2ADD = \ $(srcdir)/config/h8300/clzhi2.c \ $(srcdir)/config/h8300/ctzhi2.c \ + $(srcdir)/config/h8300/ffshi2.c \ $(srcdir)/config/h8300/parityhi2.c \ $(srcdir)/config/h8300/popcounthi2.c \ $(srcdir)/config/h8300/fixunssfsi.c