Skip to content
Snippets Groups Projects
Select Git revision
  • 08d5a9584daa81f3f01f84c76278f1e400a23ffc
  • master default protected
  • karapiperis/plasma_beta_rms_in_tensile_instability_correction_taper_function
  • stars_sidm_iact
  • darwin/gear_preSN_fbk_merge
  • darwin/gear_mechanical_feedback
  • karapiperis/consistent_treatment_of_vsig_in_MHD
  • FS_VP_m2_allGrad
  • reyz/gear_preSN_feedback
  • examples-GravityTests-HydroStatic-halo-issue
  • fof_props
  • zoom-mesh-considerations
  • FS_VP_m2
  • FS_m2
  • darwin/gear_chemistry_fluxes
  • zoom_mpi_redux
  • mladen/rt_limit_star_timesteps
  • zoom-missing-rebuild-time
  • moving_mesh
  • zoom_truncate_bkg
  • sidm_merge protected
  • v2025.10 protected
  • v2025.04 protected
  • v2025.01 protected
  • v1.0.0 protected
  • v0.9.0 protected
  • v0.8.5 protected
  • v0.8.4 protected
  • v0.8.3 protected
  • v0.8.2 protected
  • v0.8.1 protected
  • v0.8.0 protected
  • v0.7.0 protected
  • v0.6.0 protected
  • v0.5.0 protected
  • v0.4.0 protected
  • v0.3.0 protected
  • v0.2.0 protected
  • v0.1.0-pre protected
  • v0.1 protected
  • v0.0 protected
41 results

INSTALL.swift

Blame
  • intrinsics.h 3.45 KiB
    /*******************************************************************************
     * This file is part of SWIFT.
     * Copyright (c) 2015 Matthieu Schaller matthieu.schaller@durham.ac.uk)
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Lesser General Public License as published
     * by the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     *
     ******************************************************************************/
    #ifndef SWIFT_INTRINSICS_H
    #define SWIFT_INTRINSICS_H
    
    /* Config parameters. */
    #include "../config.h"
    
    /* Local headers. */
    #include "inline.h"
    
    /**
     * @brief Returns the number of leading 0-bits in x, starting at the most
     * significant bit position. If x is 0, the result is undefined.
     *
     * This is a wrapper for the GNU intrinsic with an implementation (from
     * Hacker's Delight) if the compiler intrinsics are not available.
     */
    __attribute__((always_inline)) INLINE static int intrinsics_clz(
        unsigned int x) {
    
    #ifdef __GNUC__
      /* Use GCC intrinsics if possible */
      return __builtin_clz(x);
    #else
      int n;
    
      if (x == 0) return (32);
      n = 0;
      if (x <= 0x0000FFFF) {
        n = n + 16;
        x = x << 16;
      }
      if (x <= 0x00FFFFFF) {
        n = n + 8;
        x = x << 8;
      }
      if (x <= 0x0FFFFFFF) {
        n = n + 4;
        x = x << 4;
      }
      if (x <= 0x3FFFFFFF) {
        n = n + 2;
        x = x << 2;
      }
      if (x <= 0x7FFFFFFF) {
        n = n + 1;
      }
      return n;
    #endif
    }
    
    /**
     * @brief Returns the number of leading 0-bits in x, starting at the most
     * significant bit position. If x is 0, the result is undefined.
     *
     * This is a wrapper for the GNU intrinsic with an implementation.
     */
    __attribute__((always_inline)) INLINE static int intrinsics_clzll(
        unsigned long long x) {
    
    #ifdef __GNUC__
      /* Use GCC intrinsics if possible */
      return __builtin_clzll(x);
    #else
    #error "Missing definition of clz for long long on this platform."
    #endif
    }
    
    /**
     * @brief Returns the number of 1-bits in x.
     *
     * This is a wrapper for the GNU intrinsic with an implementation (from
     * Hacker's Delight) if the compiler intrinsics are not available.
     */
    __attribute__((always_inline)) INLINE static int intrinsics_popcount(
        unsigned int x) {
    
    #ifdef __GNUC__
      /* Use GCC intrinsics if possible */
      return __builtin_popcount(x);
    #else
      x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
      x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
      x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
      x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
      return x;
    #endif
    }
    
    /**
     * @brief Returns the number of 1-bits in x.
     *
     * This is a wrapper for the GNU intrinsic with an implementation (from
     * Hacker's Delight) if the compiler intrinsics are not available.
     */
    __attribute__((always_inline)) INLINE static int intrinsics_popcountll(
        unsigned long long x) {
    
    #ifdef __GNUC__
      /* Use GCC intrinsics if possible */
      return __builtin_popcountll(x);
    #else
    #error "Missing definition of popcount for long long on this platform."
    #endif
    }
    
    #endif /* SWIFT_INTRINSICS_H */