xref: /netbsd-src/common/lib/libc/arch/ia64/string/ffs.S (revision b83ebeba7f767758d2778bb0f9d7a76534253621)
1*b83ebebaSscole/*	$NetBSD: ffs.S,v 1.1 2016/08/05 15:06:02 scole Exp $	*/
2*b83ebebaSscole
3*b83ebebaSscole/*
4*b83ebebaSscole * Copyright (c) 1995 Christopher G. Demetriou
5*b83ebebaSscole * All rights reserved.
6*b83ebebaSscole *
7*b83ebebaSscole * Redistribution and use in source and binary forms, with or without
8*b83ebebaSscole * modification, are permitted provided that the following conditions
9*b83ebebaSscole * are met:
10*b83ebebaSscole * 1. Redistributions of source code must retain the above copyright
11*b83ebebaSscole *    notice, this list of conditions and the following disclaimer.
12*b83ebebaSscole * 2. Redistributions in binary form must reproduce the above copyright
13*b83ebebaSscole *    notice, this list of conditions and the following disclaimer in the
14*b83ebebaSscole *    documentation and/or other materials provided with the distribution.
15*b83ebebaSscole * 3. All advertising materials mentioning features or use of this software
16*b83ebebaSscole *    must display the following acknowledgement:
17*b83ebebaSscole *      This product includes software developed by Christopher G. Demetriou
18*b83ebebaSscole *	for the NetBSD Project.
19*b83ebebaSscole * 4. The name of the author may not be used to endorse or promote products
20*b83ebebaSscole *    derived from this software without specific prior written permission
21*b83ebebaSscole *
22*b83ebebaSscole * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23*b83ebebaSscole * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24*b83ebebaSscole * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25*b83ebebaSscole * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26*b83ebebaSscole * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27*b83ebebaSscole * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*b83ebebaSscole * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*b83ebebaSscole * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*b83ebebaSscole * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31*b83ebebaSscole * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*b83ebebaSscole *
33*b83ebebaSscole * __FBSDID("$FreeBSD: releng/10.1/lib/libc/ia64/string/ffs.S 111777 2003-03-03 01:09:46Z obrien $");
34*b83ebebaSscole *
35*b83ebebaSscole */
36*b83ebebaSscole
37*b83ebebaSscole#include <machine/asm.h>
38*b83ebebaSscole/*
39*b83ebebaSscole
40*b83ebebaSscole/*
41*b83ebebaSscole * int
42*b83ebebaSscole * ffs(int value);
43*b83ebebaSscole */
44*b83ebebaSscoleENTRY(ffs, 1)
45*b83ebebaSscole	sxt4	r14=in0 ;;
46*b83ebebaSscole	cmp.eq	p6,p0=r14,r0
47*b83ebebaSscole(p6)	br.dpnt.few Lallzero
48*b83ebebaSscole
49*b83ebebaSscole	/*
50*b83ebebaSscole	 * Initialize return value (ret0), and set up r15 so that it
51*b83ebebaSscole	 * contains the mask with only the lowest bit set.
52*b83ebebaSscole	 */
53*b83ebebaSscole	sub	r15=r0,r14
54*b83ebebaSscole	mov	ret0=1 ;;
55*b83ebebaSscole	and	r15=r14,r15 ;;
56*b83ebebaSscole
57*b83ebebaSscole	extr.u	r16=r15,0,8 ;;
58*b83ebebaSscole	cmp.ne	p6,p0=r0,r16
59*b83ebebaSscole(p6)	br.dptk.few Ldo8
60*b83ebebaSscole
61*b83ebebaSscole	/*
62*b83ebebaSscole	 * If lower 16 bits empty, add 16 to result and use upper 16.
63*b83ebebaSscole	 */
64*b83ebebaSscole	extr.u	r16=r15,0,16 ;;
65*b83ebebaSscole	cmp.ne	p6,p0=r0,r16
66*b83ebebaSscole(p6)	br.dptk.few Ldo16
67*b83ebebaSscole	extr.u	r15=r15,16,16
68*b83ebebaSscole	add	ret0=16,ret0 ;;
69*b83ebebaSscole
70*b83ebebaSscoleLdo16:
71*b83ebebaSscole	/*
72*b83ebebaSscole	 * If lower 8 bits empty, add 8 to result and use upper 8.
73*b83ebebaSscole	 */
74*b83ebebaSscole	extr.u	r16=r15,0,8 ;;
75*b83ebebaSscole	cmp.ne	p6,p0=r0,r16
76*b83ebebaSscole(p6)	br.dptk.few Ldo8
77*b83ebebaSscole	extr.u	r15=r15,8,24
78*b83ebebaSscole	add	ret0=8,ret0 ;;
79*b83ebebaSscole
80*b83ebebaSscoleLdo8:
81*b83ebebaSscole	and	r16=0x0f,r15		/* lower 4 of 8 empty? */
82*b83ebebaSscole	and	r17=0x33,r15		/* lower 2 of each 4 empty? */
83*b83ebebaSscole	and	r18=0x55,r15 ;;		/* lower 1 of each 2 empty? */
84*b83ebebaSscole	cmp.ne	p6,p0=r16,r0
85*b83ebebaSscole	cmp.ne	p7,p0=r17,r0
86*b83ebebaSscole	cmp.ne	p8,p0=r18,r0
87*b83ebebaSscole
88*b83ebebaSscole	/* If lower 4 bits empty, add 4 to result. */
89*b83ebebaSscole(p6)	br.dptk.few Ldo4
90*b83ebebaSscole	add	ret0=4,ret0 ;;
91*b83ebebaSscole
92*b83ebebaSscoleLdo4:	/* If lower 2 bits of each 4 empty, add 2 to result. */
93*b83ebebaSscole(p7)	br.dptk.few Ldo2
94*b83ebebaSscole	add	ret0=2,ret0 ;;
95*b83ebebaSscole
96*b83ebebaSscoleLdo2:	/* If lower bit of each 2 empty, add 1 to result. */
97*b83ebebaSscole(p8)	br.dptk.few Ldone
98*b83ebebaSscole	add	ret0=1,ret0
99*b83ebebaSscole
100*b83ebebaSscoleLdone:
101*b83ebebaSscole	br.ret.sptk.few rp
102*b83ebebaSscole
103*b83ebebaSscoleLallzero:
104*b83ebebaSscole	mov	ret0=0
105*b83ebebaSscole	br.ret.sptk.few rp
106*b83ebebaSscoleEND(ffs)
107