intrinsics.h 3.55 KB
Newer Older
1 2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2015 Matthieu Schaller matthieu.schaller@durham.ac.uk)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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

22 23 24 25 26 27
/* Config parameters. */
#include "../config.h"

/* Local headers. */
#include "inline.h"

28 29 30 31
/**
 * @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.
 *
32
 * This is a wrapper for the GNU intrinsic with an implementation (from
33
 * Hacker's Delight) if the compiler intrinsics are not available.
34
 */
35
__attribute__((always_inline, const)) INLINE static int intrinsics_clz(
36
    unsigned int x) {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#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
}

69 70 71 72
/**
 * @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.
 *
73 74
 * This is a wrapper for the GNU intrinsic with a place-holder for a future
 * version in cases where the compiler intrinsic is not available.
75
 */
76
__attribute__((always_inline, const)) INLINE static int intrinsics_clzll(
77 78 79 80 81 82 83 84 85 86
    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
}

87 88 89
/**
 * @brief Returns the number of 1-bits in x.
 *
90
 * This is a wrapper for the GNU intrinsic with an implementation (from
91
 * Hacker's Delight) if the compiler intrinsics are not available.
92
 */
93
__attribute__((always_inline, const)) INLINE static int intrinsics_popcount(
94
    unsigned int x) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108

#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
}

109 110 111 112 113 114
/**
 * @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.
 */
115
__attribute__((always_inline, const)) INLINE static int intrinsics_popcountll(
116 117 118 119 120 121 122 123 124 125
    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
}

126
#endif /* SWIFT_INTRINSICS_H */