source: trunk/lib/builtins.hpp @ 1815

Last change on this file since 1815 was 1550, checked in by ksherdy, 8 years ago

Add count_forward_zeroes, count_backward_zeroes.

File size: 2.1 KB
RevLine 
[1509]1/*
[1528]2    builtin - Compiler dependent builtin function wrappers.
[1509]3
4    Idealized SIMD Operations with SSE versions
[1528]5    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
[1509]6    Licensed to the public under the Open Software License 3.0.
7    Licensed to International Characters Inc.
8       under the Academic Free License version 3.0.
9 */
10
[1528]11#ifndef BUILTINS_HPP_
12#define BUILTINS_HPP_
[1509]13
14#include "config.hpp"
15
[1550]16static IDISA_ALWAYS_INLINE long likely(long x);
17static IDISA_ALWAYS_INLINE long unlikely(long x);
18static IDISA_ALWAYS_INLINE unsigned long cfzl(unsigned long x);
19static IDISA_ALWAYS_INLINE unsigned long cbzl(unsigned long x);
20static IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x);
21static IDISA_ALWAYS_INLINE unsigned long cbzll(unsigned long long x);
[1509]22
[1528]23#if defined (_MSC_VER)
[1550]24        IDISA_ALWAYS_INLINE long likely(long x) {
[1509]25                return x;
26        }
[1550]27        IDISA_ALWAYS_INLINE long unlikely(long x) {
[1509]28                return x;
29        }
[1528]30
[1509]31        #include <intrin.h>
32        #pragma intrinsic(_BitScanForward)
[1550]33        IDISA_ALWAYS_INLINE unsigned long cfzl(unsigned long x) { // Precondition: x > 0
[1509]34                unsigned long zeroes;
35                _BitScanForward(&zeroes, x);
36                return zeroes;
37        }
[1528]38
[1550]39        IDISA_ALWAYS_INLINE unsigned long cbzl(unsigned long x) {
[1509]40                unsigned long zeroes;
41                _BitScanReverse(&zeroes, x);
42                return zeroes;
43        }
[1514]44
[1550]45        IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x) { // TODO - test
[1528]46                unsigned __int64 zeroes;
47                _BitScanForward(&zeroes, x);
48                return zeroes;
[1509]49        }
[1528]50
[1550]51        IDISA_ALWAYS_INLINE unsigned long cbzll(unsigned long long x) { // TODO - test
[1528]52                unsigned __int64 zeroes;
53                _BitScanReverse(&zeroes, x);
54                return zeroes;
[1509]55        }
[1514]56
[1528]57#elif defined (__GNUC__)
[1550]58
59        IDISA_ALWAYS_INLINE long likely(long x) {
[1509]60                return __builtin_expect(x, 1);
61        }
[1528]62
[1550]63        IDISA_ALWAYS_INLINE long unlikely(long x) {
[1509]64                return __builtin_expect(x, 0);
65        }
[1528]66
[1550]67        IDISA_ALWAYS_INLINE unsigned long cfzl(unsigned long x) {
[1514]68                return __builtin_ctzl(x);
[1509]69        }
[1528]70
[1550]71        IDISA_ALWAYS_INLINE unsigned long cbzl(unsigned long x) {
[1514]72                return __builtin_clzl(x);
[1509]73        }
74
[1550]75        IDISA_ALWAYS_INLINE unsigned long cfzll(unsigned long long x) {
[1528]76                return __builtin_ctzll(x);
77        }
78
[1550]79        IDISA_ALWAYS_INLINE unsigned long cbzll(unsigned long long x) {
[1528]80                return __builtin_clzll(x);
81        }
82
[1509]83#endif
84
[1528]85#endif /* BUILTINS_HPP */
Note: See TracBrowser for help on using the repository browser.