1*2fe8fb19SBen Gras/* $NetBSD: ffs.S,v 1.3 2004/07/18 20:30:04 chs Exp $ */ 2*2fe8fb19SBen Gras 3*2fe8fb19SBen Gras/* $OpenBSD: ffs.S,v 1.3 2001/06/04 23:14:02 mickey Exp $ */ 4*2fe8fb19SBen Gras 5*2fe8fb19SBen Gras/* 6*2fe8fb19SBen Gras * Copyright (c) 1990, 1991, 1992, 1994, The University of Utah and 7*2fe8fb19SBen Gras * the Computer Systems Laboratory at the University of Utah (CSL). 8*2fe8fb19SBen Gras * All rights reserved. 9*2fe8fb19SBen Gras * 10*2fe8fb19SBen Gras * Permission to use, copy, modify and distribute this software and its 11*2fe8fb19SBen Gras * documentation is hereby granted, provided that both the copyright 12*2fe8fb19SBen Gras * notice and this permission notice appear in all copies of the 13*2fe8fb19SBen Gras * software, derivative works or modified versions, and any portions 14*2fe8fb19SBen Gras * thereof, and that both notices appear in supporting documentation. 15*2fe8fb19SBen Gras * 16*2fe8fb19SBen Gras * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS 17*2fe8fb19SBen Gras * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF 18*2fe8fb19SBen Gras * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 19*2fe8fb19SBen Gras * 20*2fe8fb19SBen Gras * CSL requests users of this software to return to csl-dist@cs.utah.edu any 21*2fe8fb19SBen Gras * improvements that they make and grant CSL redistribution rights. 22*2fe8fb19SBen Gras * 23*2fe8fb19SBen Gras */ 24*2fe8fb19SBen Gras 25*2fe8fb19SBen Gras#include <machine/asm.h> 26*2fe8fb19SBen Gras 27*2fe8fb19SBen Gras#ifdef SYSLIBC_SCCS 28*2fe8fb19SBen Gras .text 29*2fe8fb19SBen Gras .asciz "$OpenBSD: ffs.S,v 1.3 2001/06/04 23:14:02 mickey Exp $" 30*2fe8fb19SBen Gras .align 4 31*2fe8fb19SBen Gras#endif 32*2fe8fb19SBen Gras 33*2fe8fb19SBen Gras/* 34*2fe8fb19SBen Gras * ffs(bitmask) 35*2fe8fb19SBen Gras * 36*2fe8fb19SBen Gras * Return the position of the "most significant" bit in `bitmask'. 37*2fe8fb19SBen Gras * Since this is similar to the VAX ffs instruction, bits in a word 38*2fe8fb19SBen Gras * are numbered as "32, 31, ... 1", 0 is returned if no bits are set. 39*2fe8fb19SBen Gras */ 40*2fe8fb19SBen Gras 41*2fe8fb19SBen GrasLEAF_ENTRY(ffs) 42*2fe8fb19SBen Gras comb,= %arg0,%r0,ffsdone ; If arg0 is 0 43*2fe8fb19SBen Gras or %r0,%r0,%ret0 ; return 0 44*2fe8fb19SBen Gras ldi 32,%ret0 ; Set return to high bit 45*2fe8fb19SBen Gras extru,= %arg0,31,16,%r0 ; If low 16 bits are non-zero 46*2fe8fb19SBen Gras addi,tr -16,%ret0,%ret0 ; subtract 16 from bitpos 47*2fe8fb19SBen Gras shd %r0,%arg0,16,%arg0 ; else shift right 16 bits 48*2fe8fb19SBen Gras extru,= %arg0,31,8,%r0 ; If low 8 bits are non-zero 49*2fe8fb19SBen Gras addi,tr -8,%ret0,%ret0 ; subtract 8 from bitpos 50*2fe8fb19SBen Gras shd %r0,%arg0,8,%arg0 ; else shift right 8 bits 51*2fe8fb19SBen Gras extru,= %arg0,31,4,%r0 ; If low 4 bits are non-zero 52*2fe8fb19SBen Gras addi,tr -4,%ret0,%ret0 ; subtract 4 from bitpos 53*2fe8fb19SBen Gras shd %r0,%arg0,4,%arg0 ; else shift right 4 bits 54*2fe8fb19SBen Gras extru,= %arg0,31,2,%r0 ; If low 2 bits are non-zero 55*2fe8fb19SBen Gras addi,tr -2,%ret0,%ret0 ; subtract 2 from bitpos 56*2fe8fb19SBen Gras shd %r0,%arg0,2,%arg0 ; else shift right 2 bits 57*2fe8fb19SBen Gras extru,= %arg0,31,1,%r0 ; If low bit is non-zero 58*2fe8fb19SBen Gras addi -1,%ret0,%ret0 ; subtract 1 from bitpos 59*2fe8fb19SBen Grasffsdone: 60*2fe8fb19SBen Gras bv,n %r0(%rp) 61*2fe8fb19SBen GrasEXIT(ffs) 62*2fe8fb19SBen Gras 63*2fe8fb19SBen Gras .end 64