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