1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2// 3// Permission to use, copy, modify, and/or distribute this software for any 4// purpose with or without fee is hereby granted, provided that the above 5// copyright notice and this permission notice appear in all copies. 6// 7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 15// ---------------------------------------------------------------------------- 16// Count leading zero bits in a single word 17// Input a; output function return 18// 19// extern uint64_t word_clz (uint64_t a); 20// 21// Standard x86-64 ABI: RDI = a, returns RAX 22// Microsoft x64 ABI: RCX = a, returns RAX 23// ---------------------------------------------------------------------------- 24 25#include "s2n_bignum_internal.h" 26 27 .intel_syntax noprefix 28 S2N_BN_SYM_VISIBILITY_DIRECTIVE(word_clz) 29 S2N_BN_SYM_PRIVACY_DIRECTIVE(word_clz) 30 .text 31 32S2N_BN_SYMBOL(word_clz): 33 endbr64 34 35#if WINDOWS_ABI 36 push rdi 37 push rsi 38 mov rdi, rcx 39#endif 40 41// First do rax = 63 - bsr(a), which is right except (maybe) for zero inputs 42 43 bsr rax, rdi 44 xor rax, 63 45 46// Force return of 64 in the zero-input case 47 48 mov edx, 64 49 test rdi, rdi 50 cmove rax, rdx 51 52#if WINDOWS_ABI 53 pop rsi 54 pop rdi 55#endif 56 ret 57 58#if defined(__linux__) && defined(__ELF__) 59.section .note.GNU-stack,"",%progbits 60#endif 61