xref: /netbsd-src/common/lib/libc/arch/alpha/string/ffs.S (revision 37c9f0a654f1f14465cded15c4577522438c5585)
1*37c9f0a6Schristos/* $NetBSD: ffs.S,v 1.1 2005/12/20 19:28:49 christos Exp $ */
2*37c9f0a6Schristos
3*37c9f0a6Schristos/*
4*37c9f0a6Schristos * Copyright (c) 1995 Christopher G. Demetriou
5*37c9f0a6Schristos * All rights reserved.
6*37c9f0a6Schristos *
7*37c9f0a6Schristos * Redistribution and use in source and binary forms, with or without
8*37c9f0a6Schristos * modification, are permitted provided that the following conditions
9*37c9f0a6Schristos * are met:
10*37c9f0a6Schristos * 1. Redistributions of source code must retain the above copyright
11*37c9f0a6Schristos *    notice, this list of conditions and the following disclaimer.
12*37c9f0a6Schristos * 2. Redistributions in binary form must reproduce the above copyright
13*37c9f0a6Schristos *    notice, this list of conditions and the following disclaimer in the
14*37c9f0a6Schristos *    documentation and/or other materials provided with the distribution.
15*37c9f0a6Schristos * 3. All advertising materials mentioning features or use of this software
16*37c9f0a6Schristos *    must display the following acknowledgement:
17*37c9f0a6Schristos *          This product includes software developed for the
18*37c9f0a6Schristos *          NetBSD Project.  See http://www.NetBSD.org/ for
19*37c9f0a6Schristos *          information about NetBSD.
20*37c9f0a6Schristos * 4. The name of the author may not be used to endorse or promote products
21*37c9f0a6Schristos *    derived from this software without specific prior written permission.
22*37c9f0a6Schristos *
23*37c9f0a6Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24*37c9f0a6Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25*37c9f0a6Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26*37c9f0a6Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27*37c9f0a6Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28*37c9f0a6Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29*37c9f0a6Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30*37c9f0a6Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*37c9f0a6Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32*37c9f0a6Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*37c9f0a6Schristos *
34*37c9f0a6Schristos * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35*37c9f0a6Schristos */
36*37c9f0a6Schristos
37*37c9f0a6Schristos#include <machine/asm.h>
38*37c9f0a6Schristos
39*37c9f0a6SchristosLEAF(ffs, 1)
40*37c9f0a6Schristos	addl	a0, 0, t0
41*37c9f0a6Schristos	beq	t0, Lallzero
42*37c9f0a6Schristos
43*37c9f0a6Schristos	/*
44*37c9f0a6Schristos	 * Initialize return value (v0), and set up t1 so that it
45*37c9f0a6Schristos	 * contains the mask with only the lowest bit set.
46*37c9f0a6Schristos	 */
47*37c9f0a6Schristos	subl	zero, t0, t1
48*37c9f0a6Schristos	ldil	v0, 1
49*37c9f0a6Schristos	and	t0, t1, t1
50*37c9f0a6Schristos
51*37c9f0a6Schristos	and	t1, 0xff, t2
52*37c9f0a6Schristos	bne	t2, Ldo8
53*37c9f0a6Schristos
54*37c9f0a6Schristos	/*
55*37c9f0a6Schristos	 * If lower 16 bits empty, add 16 to result and use upper 16.
56*37c9f0a6Schristos	 */
57*37c9f0a6Schristos	zapnot	t1, 0x03, t3
58*37c9f0a6Schristos	bne	t3, Ldo16
59*37c9f0a6Schristos	sra	t1, 16, t1
60*37c9f0a6Schristos	addl	v0, 16, v0
61*37c9f0a6Schristos
62*37c9f0a6SchristosLdo16:
63*37c9f0a6Schristos	/*
64*37c9f0a6Schristos	 * If lower 8 bits empty, add 8 to result and use upper 8.
65*37c9f0a6Schristos	 */
66*37c9f0a6Schristos	and	t1, 0xff, t4
67*37c9f0a6Schristos	bne	t4, Ldo8
68*37c9f0a6Schristos	sra	t1, 8, t1
69*37c9f0a6Schristos	addl	v0, 8, v0
70*37c9f0a6Schristos
71*37c9f0a6SchristosLdo8:
72*37c9f0a6Schristos	and	t1, 0x0f, t5		/* lower 4 of 8 empty? */
73*37c9f0a6Schristos	and	t1, 0x33, t6		/* lower 2 of each 4 empty? */
74*37c9f0a6Schristos	and	t1, 0x55, t7		/* lower 1 of each 2 empty? */
75*37c9f0a6Schristos
76*37c9f0a6Schristos	/* If lower 4 bits empty, add 4 to result. */
77*37c9f0a6Schristos	bne	t5, Ldo4
78*37c9f0a6Schristos	addl	v0, 4, v0
79*37c9f0a6Schristos
80*37c9f0a6SchristosLdo4:	/* If lower 2 bits of each 4 empty, add 2 to result. */
81*37c9f0a6Schristos	bne	t6, Ldo2
82*37c9f0a6Schristos	addl	v0, 2, v0
83*37c9f0a6Schristos
84*37c9f0a6SchristosLdo2:	/* If lower bit of each 2 empty, add 1 to result. */
85*37c9f0a6Schristos	bne	t7, Ldone
86*37c9f0a6Schristos	addl	v0, 1, v0
87*37c9f0a6Schristos
88*37c9f0a6SchristosLdone:
89*37c9f0a6Schristos	RET
90*37c9f0a6Schristos
91*37c9f0a6SchristosLallzero:
92*37c9f0a6Schristos	bis	zero, zero, v0
93*37c9f0a6Schristos	RET
94*37c9f0a6SchristosEND(ffs)
95