/*
 * 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1986
 * LICENSED MATERIALS - PROPERTY OF IBM
 * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
 */
/* $Header:ffs.s 12.0$ */
/* $ACIS:ffs.s 12.0$ */
/* $Source: /ibm/acis/usr/src/lib/libc/ca/gen/RCS/ffs.s,v $ */

	.data
rcsid:	.asciz	"$Header:ffs.s 12.0$"
	.text

#include "LINKG.h"

/*
 * ffs(x); int x;
 * Find First Set -- returns bit number (1-32, right to left) of first
 * bit set in integer argument; ffs(0) == 0.
 */

ENTRY(ffs)
	twoc	r3,r2		/* AND	0110101000 */
	n	r3,r2		/* with 1001011000 */
	clz	r2,r3		/* get  0000001000 */
	cis	r2,15		/* if 16, then try upper half */
	blerx	r15		/* was in lower; convert 0 thru 15 */
	 sfi	r2,r2,16	/* to 16 thru 1 */
	sri16	r3,0		/* Try upper half */
	clz	r2,r3
	cis	r2,15		/* if 16, then arg was 0 */
	blerx	r15		/* convert 0 thru 15 */
	 sfi	r2,r2,32	/* to 32 thru 17 */
	brx	r15		/* was zero, */
	 lis	r2,0		/* so return zero */

	TTNOFRM

