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