text: /*!
  MIT License
  
  Copyright (c) 2018 Tamaki Kojima
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
  */
  
  
  #ifndef __COMMON__
  #define __COMMON__
  #include <cuda_runtime_api.h>
  
  /*
   * General settings
   */
  const int WARP_SIZE = 32;
  const int MAX_BLOCK_SIZE = 512;
  
  /*
   * Utility functions
   */
  template <typename T>
  __device__ __forceinline__ T WARP_SHFL_XOR(
    T value, int laneMask, int width = warpSize, unsigned int mask = 0xffffffff) {
  #if CUDART_VERSION >= 9000
    return __shfl_xor_sync(mask, value, laneMask, width);
  #else
    return __shfl_xor(value, laneMask, width);
  #endif
  }
  
  __device__ __forceinline__ int getMSB(int val) { return 31 - __clz(val); }
  
  static int getNumThreads(int nElem) {
    int threadSizes[5] = {32, 64, 128, 256, MAX_BLOCK_SIZE};
    for (int i = 0; i != 5; ++i) {
      if (nElem <= threadSizes[i]) {
        return threadSizes[i];
      }
    }
    return MAX_BLOCK_SIZE;
  }
  
  
  #endif