xref: /openbsd/lib/libc/arch/sh/string/ffs.S (revision 404b540a)
1/*	$OpenBSD: ffs.S,v 1.2 2008/06/26 05:42:05 ray Exp $	*/
2/*	$NetBSD: ffs.S,v 1.1 2005/12/20 19:28:50 christos Exp $	*/
3
4/*-
5 * Copyright (c) 2002 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by ITOH Yasufumi.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <machine/asm.h>
34
35/*
36 * ffs - find first bit set
37 *
38 * This code makes use of ``test 8bit'' and ``shift 8bit'' instructions.
39 * The remaining 8bit is tested in every 2bit.
40 */
41
42ENTRY(ffs)
43	mov	r4,r0		! using r0 specific instructions
44	tst	#0xff,r0
45	bf/s	L8bit
46	mov	#0+1,r1		! ret = 1..8
47
48	tst	r0,r0		! ffs(0) is 0
49	bt	Lzero		! testing here to accelerate ret=1..8 cases
50
51	shlr8	r0
52	tst	#0xff,r0
53	bf/s	L8bit
54	mov	#8+1,r1		! ret = 9..16
55
56	shlr8	r0
57	tst	#0xff,r0
58	bf/s	L8bit
59	mov	#16+1,r1	! ret = 17..24
60
61	shlr8	r0
62	mov	#24+1,r1	! ret = 25..32
63
64L8bit:
65	tst	#0x0f,r0
66	bt	4f
67
68	tst	#0x03,r0
69	bt	2f
70	tst	#0x01,r0	! not bit 0 -> T
71	mov	#0,r0
72	rts
73	 addc	r1,r0		! 0 + r1 + T -> r0
74
752:	tst	#0x04,r0
76	mov	#2,r0
77	rts
78	 addc	r1,r0
79
804:	tst	#0x30,r0
81	bt	6f
82	tst	#0x10,r0
83	mov	#4,r0
84	rts
85	 addc	r1,r0
86
876:	tst	#0x40,r0
88	mov	#6,r0
89	rts
90	 addc	r1,r0
91
92Lzero:	rts
93	 nop
94