xref: /netbsd/common/lib/libc/arch/sh3/string/ffs.S (revision 080852e7)
1*080852e7Smrg/*	$NetBSD: ffs.S,v 1.3 2011/07/04 12:18:05 mrg Exp $	*/
237c9f0a6Schristos
337c9f0a6Schristos/*-
437c9f0a6Schristos * Copyright (c) 2002 The NetBSD Foundation, Inc.
537c9f0a6Schristos * All rights reserved.
637c9f0a6Schristos *
737c9f0a6Schristos * This code is derived from software contributed to The NetBSD Foundation
837c9f0a6Schristos * by ITOH Yasufumi.
937c9f0a6Schristos *
1037c9f0a6Schristos * Redistribution and use in source and binary forms, with or without
1137c9f0a6Schristos * modification, are permitted provided that the following conditions
1237c9f0a6Schristos * are met:
1337c9f0a6Schristos * 1. Redistributions of source code must retain the above copyright
1437c9f0a6Schristos *    notice, this list of conditions and the following disclaimer.
1537c9f0a6Schristos * 2. Redistributions in binary form must reproduce the above copyright
1637c9f0a6Schristos *    notice, this list of conditions and the following disclaimer in the
1737c9f0a6Schristos *    documentation and/or other materials provided with the distribution.
1837c9f0a6Schristos *
1937c9f0a6Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2037c9f0a6Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2137c9f0a6Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2237c9f0a6Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2337c9f0a6Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2437c9f0a6Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2537c9f0a6Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2637c9f0a6Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2737c9f0a6Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2837c9f0a6Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2937c9f0a6Schristos * POSSIBILITY OF SUCH DAMAGE.
3037c9f0a6Schristos */
3137c9f0a6Schristos
3237c9f0a6Schristos#include <machine/asm.h>
3337c9f0a6Schristos
3437c9f0a6Schristos#if defined(LIBC_SCCS) && !defined(lint)
35*080852e7Smrg	RCSID("$NetBSD: ffs.S,v 1.3 2011/07/04 12:18:05 mrg Exp $")
3637c9f0a6Schristos#endif
3737c9f0a6Schristos
3837c9f0a6Schristos/*
3937c9f0a6Schristos * ffs - find first bit set
4037c9f0a6Schristos *
4137c9f0a6Schristos * This code makes use of ``test 8bit'' and ``shift 8bit'' instructions.
4237c9f0a6Schristos * The remaining 8bit is tested in every 2bit.
4337c9f0a6Schristos */
4437c9f0a6Schristos
45*080852e7SmrgWEAK_ALIAS(__ffssi2,ffs)
4637c9f0a6SchristosENTRY(ffs)
4737c9f0a6Schristos	mov	r4,r0		! using r0 specific instructions
4837c9f0a6Schristos	tst	#0xff,r0
4937c9f0a6Schristos	bf/s	L8bit
5037c9f0a6Schristos	mov	#0+1,r1		! ret = 1..8
5137c9f0a6Schristos
5237c9f0a6Schristos	tst	r0,r0		! ffs(0) is 0
5337c9f0a6Schristos	bt	Lzero		! testing here to accelerate ret=1..8 cases
5437c9f0a6Schristos
5537c9f0a6Schristos	shlr8	r0
5637c9f0a6Schristos	tst	#0xff,r0
5737c9f0a6Schristos	bf/s	L8bit
5837c9f0a6Schristos	mov	#8+1,r1		! ret = 9..16
5937c9f0a6Schristos
6037c9f0a6Schristos	shlr8	r0
6137c9f0a6Schristos	tst	#0xff,r0
6237c9f0a6Schristos	bf/s	L8bit
6337c9f0a6Schristos	mov	#16+1,r1	! ret = 17..24
6437c9f0a6Schristos
6537c9f0a6Schristos	shlr8	r0
6637c9f0a6Schristos	mov	#24+1,r1	! ret = 25..32
6737c9f0a6Schristos
6837c9f0a6SchristosL8bit:
6937c9f0a6Schristos	tst	#0x0f,r0
7037c9f0a6Schristos	bt	4f
7137c9f0a6Schristos
7237c9f0a6Schristos	tst	#0x03,r0
7337c9f0a6Schristos	bt	2f
7437c9f0a6Schristos	tst	#0x01,r0	! not bit 0 -> T
7537c9f0a6Schristos	mov	#0,r0
7637c9f0a6Schristos	rts
7737c9f0a6Schristos	 addc	r1,r0		! 0 + r1 + T -> r0
7837c9f0a6Schristos
7937c9f0a6Schristos2:	tst	#0x04,r0
8037c9f0a6Schristos	mov	#2,r0
8137c9f0a6Schristos	rts
8237c9f0a6Schristos	 addc	r1,r0
8337c9f0a6Schristos
8437c9f0a6Schristos4:	tst	#0x30,r0
8537c9f0a6Schristos	bt	6f
8637c9f0a6Schristos	tst	#0x10,r0
8737c9f0a6Schristos	mov	#4,r0
8837c9f0a6Schristos	rts
8937c9f0a6Schristos	 addc	r1,r0
9037c9f0a6Schristos
9137c9f0a6Schristos6:	tst	#0x40,r0
9237c9f0a6Schristos	mov	#6,r0
9337c9f0a6Schristos	rts
9437c9f0a6Schristos	 addc	r1,r0
9537c9f0a6Schristos
9637c9f0a6SchristosLzero:	rts
9737c9f0a6Schristos	 nop
98