1 /* 2 * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 #include <stdint.h> 19 20 int64_t __mth_i_kpopcnt(uint64_t u64)21__mth_i_kpopcnt(uint64_t u64) 22 { 23 uint64_t r64; 24 25 #if defined(TARGET_X8664) 26 asm("popcnt %1, %0" 27 : "=r"(r64) 28 : "r"(u64) 29 : 30 ); 31 #elif defined(TARGET_LINUX_POWER) 32 asm("popcntd %0, %1" 33 : "=r"(r64) 34 : "r"(u64) 35 : 36 ); 37 #else 38 static const uint64_t u5s = 0x5555555555555555ul; 39 static const uint64_t u3s = 0x3333333333333333ul; 40 static const uint64_t u7s = 0x0707070707070707ul; 41 static const uint64_t u1s = 0x0101010101010101ul; 42 r64 = u64; 43 r64 = (r64 & u5s) + (r64 >> 1 & u5s); 44 r64 = (r64 & u3s) + (r64 >> 2 & u3s); 45 r64 = (r64 & u7s) + (r64 >> 4 & u7s); 46 r64 *= u1s; 47 r64 >>= 56; 48 #endif 49 50 return r64; 51 } 52