1 | ; $Id: ASMBitLastSetU64.asm 69111 2017-10-17 14:26:02Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; IPRT - ASMBitLastSetU64().
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | ; General Public License (GPL) as published by the Free Software
|
---|
13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | ;
|
---|
17 | ; The contents of this file may alternatively be used under the terms
|
---|
18 | ; of the Common Development and Distribution License Version 1.0
|
---|
19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | ; CDDL are applicable instead of those of the GPL.
|
---|
22 | ;
|
---|
23 | ; You may elect to license modified versions of this file under the
|
---|
24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | ;
|
---|
26 |
|
---|
27 |
|
---|
28 | ;*******************************************************************************
|
---|
29 | ;* Header Files *
|
---|
30 | ;*******************************************************************************
|
---|
31 | %include "iprt/asmdefs.mac"
|
---|
32 |
|
---|
33 | BEGINCODE
|
---|
34 |
|
---|
35 | ;;
|
---|
36 | ; Finds the last bit which is set in the given 64-bit integer.
|
---|
37 | ;
|
---|
38 | ; Bits are numbered from 1 (least significant) to 64.
|
---|
39 | ;
|
---|
40 | ; @returns (xAX) index [1..64] of the last set bit.
|
---|
41 | ; @returns (xAX) 0 if all bits are cleared.
|
---|
42 | ; @param msc:rcx gcc:rdi x86:stack u64 Integer to search for set bits.
|
---|
43 | ;
|
---|
44 | ; @cproto DECLASM(unsigned) ASMBitLastSetU64(uint64_t u64);
|
---|
45 | ;
|
---|
46 | BEGINPROC_EXPORTED ASMBitLastSetU64
|
---|
47 | %if ARCH_BITS == 16
|
---|
48 | CPU 8086
|
---|
49 | push bp
|
---|
50 | mov bp, sp
|
---|
51 |
|
---|
52 | ; 63:48
|
---|
53 | mov ax, 64
|
---|
54 | mov cx, [bp + 2 + 2 + 6]
|
---|
55 | test cx, cx
|
---|
56 | jnz .next_bit
|
---|
57 |
|
---|
58 | ; 47:32
|
---|
59 | mov al, 48
|
---|
60 | or cx, [bp + 2 + 2 + 4]
|
---|
61 | jnz .next_bit
|
---|
62 |
|
---|
63 | ; 31:16
|
---|
64 | mov al, 32
|
---|
65 | or cx, [bp + 2 + 2 + 2]
|
---|
66 | jnz .next_bit
|
---|
67 |
|
---|
68 | ; 16:0
|
---|
69 | mov al, 16
|
---|
70 | or cx, [bp + 2 + 2 + 0]
|
---|
71 | jz .return_zero
|
---|
72 |
|
---|
73 | ; find the bit that was set.
|
---|
74 | .next_bit:
|
---|
75 | shl cx, 1
|
---|
76 | jc .return
|
---|
77 | dec ax
|
---|
78 | jmp .next_bit
|
---|
79 |
|
---|
80 | .return_zero:
|
---|
81 | xor ax, ax
|
---|
82 | .return:
|
---|
83 | pop bp
|
---|
84 | ret
|
---|
85 |
|
---|
86 | %else
|
---|
87 | %if ARCH_BITS == 64
|
---|
88 | %ifdef ASM_CALL64_GCC
|
---|
89 | bsr rax, rsi
|
---|
90 | %else
|
---|
91 | bsr rax, rcx
|
---|
92 | %endif
|
---|
93 | jz .return_zero
|
---|
94 | inc eax
|
---|
95 | .return:
|
---|
96 | ret
|
---|
97 |
|
---|
98 | %elif ARCH_BITS == 32
|
---|
99 | ; Check the 2nd dword then the first one.
|
---|
100 | bsr eax, dword [esp + 4 + 4]
|
---|
101 | jz .check_1st_dword
|
---|
102 | add eax, 32
|
---|
103 | ret
|
---|
104 |
|
---|
105 | .check_1st_dword:
|
---|
106 | bsr eax, dword [esp + 4 + 0]
|
---|
107 | jz .return_zero
|
---|
108 | inc eax
|
---|
109 | ret
|
---|
110 |
|
---|
111 | %else
|
---|
112 | %error "Missing or invalid ARCH_BITS."
|
---|
113 | %endif
|
---|
114 |
|
---|
115 | .return_zero:
|
---|
116 | xor eax, eax
|
---|
117 | ret
|
---|
118 | %endif
|
---|
119 | ENDPROC ASMBitLastSetU64
|
---|
120 |
|
---|