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