1*880f1047Srin/* $NetBSD: ffs.S,v 1.8 2020/03/10 08:15:44 rin Exp $ */ 237c9f0a6Schristos 337c9f0a6Schristos/*- 437c9f0a6Schristos * Copyright (c) 1990 The Regents of the University of California. 537c9f0a6Schristos * All rights reserved. 637c9f0a6Schristos * 737c9f0a6Schristos * This code is derived from software contributed to Berkeley by 837c9f0a6Schristos * the Systems Programming Group of the University of Utah Computer 937c9f0a6Schristos * Science Department. 1037c9f0a6Schristos * 1137c9f0a6Schristos * Redistribution and use in source and binary forms, with or without 1237c9f0a6Schristos * modification, are permitted provided that the following conditions 1337c9f0a6Schristos * are met: 1437c9f0a6Schristos * 1. Redistributions of source code must retain the above copyright 1537c9f0a6Schristos * notice, this list of conditions and the following disclaimer. 1637c9f0a6Schristos * 2. Redistributions in binary form must reproduce the above copyright 1737c9f0a6Schristos * notice, this list of conditions and the following disclaimer in the 1837c9f0a6Schristos * documentation and/or other materials provided with the distribution. 1937c9f0a6Schristos * 3. Neither the name of the University nor the names of its contributors 2037c9f0a6Schristos * may be used to endorse or promote products derived from this software 2137c9f0a6Schristos * without specific prior written permission. 2237c9f0a6Schristos * 2337c9f0a6Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2437c9f0a6Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2537c9f0a6Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2637c9f0a6Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2737c9f0a6Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2837c9f0a6Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2937c9f0a6Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3037c9f0a6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3137c9f0a6Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3237c9f0a6Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3337c9f0a6Schristos * SUCH DAMAGE. 3437c9f0a6Schristos */ 3537c9f0a6Schristos 3637c9f0a6Schristos#include <machine/asm.h> 3737c9f0a6Schristos 3837c9f0a6Schristos#if defined(LIBC_SCCS) && !defined(lint) 3937c9f0a6Schristos#if 0 4037c9f0a6Schristos RCSID("from: @(#)ffs.s 5.1 (Berkeley) 5/12/90") 4137c9f0a6Schristos#else 42*880f1047Srin RCSID("$NetBSD: ffs.S,v 1.8 2020/03/10 08:15:44 rin Exp $") 4337c9f0a6Schristos#endif 4437c9f0a6Schristos#endif /* LIBC_SCCS and not lint */ 4537c9f0a6Schristos 4637c9f0a6Schristos/* bit = ffs(value) */ 4737c9f0a6Schristos 48*880f1047Srin#ifdef _LIBC 49046047efSmrgWEAK_ALIAS(__ffssi2,ffs) 50*880f1047Srin#else /* KERNEL */ 51*880f1047Srin/* 52*880f1047Srin * Our in-kernel linker does not understand weak references, which 53*880f1047Srin * prevents modules depended on __ffssi2 from being loaded. Also, 54*880f1047Srin * we do not provide ffs(9) as a kernel routine. Let's rename it! 55*880f1047Srin */ 56*880f1047Srin#define ffs __ffssi2 57*880f1047Srin#endif 58046047efSmrg 591d398a17Smatt#if (!defined(__mc68010__) && !defined(__mcoldfire__)) || defined(__mcfisac__) 6037c9f0a6Schristos 6137c9f0a6SchristosENTRY(ffs) 6207a0a325Smatt movl 4(%sp),%d0 6337c9f0a6Schristos movl %d0,%d1 6437c9f0a6Schristos negl %d0 6537c9f0a6Schristos andl %d0,%d1 6637c9f0a6Schristos movql #32,%d0 671d398a17Smatt#if defined(__mcfisac__) 681d398a17Smatt ff1.l %d1 | count leading zeros 691d398a17Smatt#else 7037c9f0a6Schristos bfffo %d1{#0:#32},%d1 711d398a17Smatt#endif 7237c9f0a6Schristos subl %d1,%d0 7337c9f0a6Schristos rts 7465726debSmattEND(ffs) 7537c9f0a6Schristos 761d398a17Smatt#elif defined(__mcoldfire__) 771d398a17Smatt 781d398a17SmattENTRY(ffs) 791d398a17Smatt clrl %d0 801d398a17Smatt movl 4(%sp),%d1 8118ec38eaSchs jeq .L2 | return 0 if 0 8218ec38eaSchs.L1: 831d398a17Smatt addql #1,%d0 841d398a17Smatt lsrl #1,%d1 | shift low bit into carry 8518ec38eaSchs jcc .L1 | keep looping while carry is clear. 8618ec38eaSchs.L2: 871d398a17Smatt rts 88ee127cc5SrinEND(ffs) 891d398a17Smatt 9037c9f0a6Schristos#else /* __mc68010__ */ 9137c9f0a6Schristos 9237c9f0a6SchristosENTRY(ffs) 9307a0a325Smatt movl 4(%sp),%d0 9418ec38eaSchs jeq .L2 9537c9f0a6Schristos movql #31,%d1 9618ec38eaSchs.L1: 9737c9f0a6Schristos lsrl #1,%d0 9818ec38eaSchs dbcs %d1,.L1 9937c9f0a6Schristos movql #32,%d0 10037c9f0a6Schristos subl %d1,%d0 10118ec38eaSchs.L2: 10237c9f0a6Schristos rts 10365726debSmattEND(ffs) 10437c9f0a6Schristos 10537c9f0a6Schristos#endif /* __mc68010__ */ 106