1 1.4 mrg /* $NetBSD: ffs.S,v 1.4 2011/07/04 11:35:26 mrg Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.3 matt * Copyright (c) 2010 The NetBSD Foundation, Inc. 5 1.3 matt * All rights reserved. 6 1.1 christos * 7 1.3 matt * This code is derived from software contributed to The NetBSD Foundation 8 1.3 matt * by Matt Thomas of 3am Software Foundry. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 19 1.3 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.3 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.3 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.3 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.3 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.3 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.3 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.3 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.3 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.3 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.3 matt * POSSIBILITY OF SUCH DAMAGE. 30 1.1 christos */ 31 1.1 christos 32 1.1 christos #include <mips/asm.h> 33 1.1 christos 34 1.4 mrg RCSID("$NetBSD: ffs.S,v 1.4 2011/07/04 11:35:26 mrg Exp $") 35 1.1 christos 36 1.1 christos /* bit = ffs(value) */ 37 1.1 christos 38 1.3 matt .text 39 1.3 matt .set noreorder 40 1.3 matt 41 1.4 mrg WEAK_ALIAS(__ffssi2,ffs) 42 1.3 matt #if __mips == 64 || __mips == 32 43 1.3 matt LEAF(ffs) 44 1.3 matt #ifndef _LP64 45 1.3 matt XLEAF(ffsl) 46 1.3 matt #endif 47 1.3 matt .set push 48 1.3 matt .set mips32 49 1.3 matt li v1, 32 50 1.3 matt #if __mips == 64 51 1.3 matt sll a0, a0, 0 52 1.3 matt #endif 53 1.3 matt negu a1, a0 54 1.3 matt and a0, a1 55 1.3 matt clz v0, a0 56 1.3 matt j ra 57 1.3 matt subu v0, v1, v0 58 1.3 matt .set pop 59 1.3 matt END(ffs) 60 1.3 matt #if defined(_LP64) && __mips == 64 61 1.3 matt LEAF(ffsl) 62 1.3 matt li v1, 64 63 1.3 matt negu a1, a0 64 1.3 matt and a0, a1 65 1.3 matt dclz v0, a0 66 1.3 matt j ra 67 1.3 matt subu v0, v1, v0 68 1.3 matt END(ffsl) 69 1.3 matt #endif 70 1.3 matt #else /* __mips != 64 && __mips != 32 */ 71 1.3 matt 72 1.3 matt #ifdef _LP64 73 1.3 matt XLEAF(ffsl) 74 1.3 matt beqz a0, 6f # fast escape if 0 75 1.3 matt li v0, 0 76 1.3 matt 77 1.3 matt li v0, 1 78 1.3 matt li a3, 0xffffffff # initial mask 79 1.3 matt b 1f 80 1.3 matt li a2, 32 # bit count of mask 81 1.3 matt #endif /* _LP64 */ 82 1.1 christos LEAF(ffs) 83 1.3 matt #ifndef _LP64 84 1.3 matt XLEAF(ffsl) 85 1.3 matt #endif /* !_LP64 */ 86 1.3 matt beqz a0, 6f 87 1.3 matt li v0, 0 88 1.3 matt 89 1.3 matt li v0, 1 90 1.3 matt li a3, 0xffff # initial mask 91 1.3 matt li a2, 16 # bit count of mask 92 1.1 christos 1: 93 1.3 matt and v1, a0, a3 # focus no lower half of bits left 94 1.3 matt bnez v1, 2f # any of the lower half set? 95 1.3 matt nop 96 1.3 matt addu v0, a2 # nope, then bit is in the upper half 97 1.3 matt #ifdef _LP64 98 1.3 matt dsrlv a0, a0, a2 # discard low bits 99 1.3 matt #else 100 1.3 matt srlv a0, a0, a2 # discard low bits 101 1.3 matt #endif 102 1.3 matt 2: 103 1.3 matt srl a2, 1 # divide bit count by 2 104 1.3 matt bnez a2, 1b # still bits left to text? 105 1.3 matt srlv a3, a3, a2 # shrink mask in half 106 1.3 matt 6: 107 1.1 christos j ra 108 1.3 matt nop 109 1.1 christos END(ffs) 110 1.3 matt #endif /* __mips == 64 || __mips == 32 */ 111