source: trunk/lib_c/builtins.h @ 4046

Last change on this file since 4046 was 3391, checked in by linmengl, 6 years ago

check in IDISA C library and other support libraries. Some template features still remain.

File size: 2.9 KB
Line 
1/* Generated by cpp2c.rb from ./builtins.hpp
2 * Use IDISA C support
3*/
4
5#ifndef BUILTINS_H_
6#define BUILTINS_H_
7
8/*=============================================================================
9    builtin - Compiler dependent builtin function wrappers.
10
11    Copyright (C) 2011, Robert D. Cameron, Kenneth S. Herdy.
12    Licensed to the public under the Open Software License 3.0.
13    Licensed to International Characters Inc.
14       under the Academic Free License version 3.0.
15=============================================================================*/
16
17#include "config.h"
18
19static IDISA_ALWAYS_INLINE long likely(long x);
20static IDISA_ALWAYS_INLINE long unlikely(long x);
21
22#if defined (_MSC_VER)
23        IDISA_ALWAYS_INLINE long likely(long x) {
24                return x;
25        }
26        IDISA_ALWAYS_INLINE long unlikely(long x) {
27                return x;
28        }
29
30#elif defined (__GNUC__)
31
32        IDISA_ALWAYS_INLINE long likely(long x) {
33                return __builtin_expect(x, 1);
34        }
35
36        IDISA_ALWAYS_INLINE long unlikely(long x) {
37                return __builtin_expect(x, 0);
38        }
39
40#endif
41
42#include <stdint.h>
43#if defined (_MSC_VER)
44        #include <intrin.h>
45
46        #ifdef _M_X64
47        typedef unsigned __int64 ScanWord;
48        #define ScanForwardIntrinsic _BitScanForward64
49        #define ScanReverseIntrinsic _BitScanReverse64
50        #else
51        typedef unsigned __int32 ScanWord;
52        #define ScanForwardIntrinsic _BitScanForward
53        #define ScanReverseIntrinsic _BitScanReverse
54        #endif
55
56        #pragma intrinsic(ScanForwardIntrinsic)
57        #pragma intrinsic(ScanReverseIntrinsic)
58        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(ScanWord x) { // Precondition: x != 0
59                unsigned long zeroes;
60                ScanForwardIntrinsic(&zeroes, x);
61                return (uint32_t) zeroes;
62        }
63
64        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(ScanWord x) { // Precondition: x != 0
65                unsigned long pos;
66                ScanReverseIntrinsic(&pos, x);
67                return (uint32_t) (8* sizeof(ScanWord) - pos - 1);
68        }
69
70        #undef ScanForwardIntrinsic
71        #undef ScanReverseIntrinsic
72
73#elif defined (__GNUC__)
74
75        #if __x86_64__ /* 64-bit architecture */
76        typedef uint64_t ScanWord;
77        #define ScanForwardIntrinsic __builtin_ctzll
78        #define ScanReverseIntrinsic __builtin_clzll
79
80        #else
81        typedef uint32_t ScanWord;
82        #define ScanForwardIntrinsic __builtin_ctzl
83        #define ScanReverseIntrinsic __builtin_clzl
84        #endif
85
86        IDISA_ALWAYS_INLINE uint32_t scan_forward_zeroes(ScanWord x) { // Precondition: x != 0
87                return (uint32_t) ScanForwardIntrinsic((ScanWord) x);
88        }
89
90        IDISA_ALWAYS_INLINE uint32_t scan_backward_zeroes(ScanWord x) { // Precondition: x != 0
91                return (uint32_t) ScanReverseIntrinsic((ScanWord) x);
92        }
93
94        #undef ScanForwardIntrinsic
95        #undef ScanReverseIntrinsic
96
97#else
98  #warning "Neither _MSC_VER nor __GNUC__ defined: scan_forward/backward_zeroes not implemented."
99#endif
100
101#endif // BUILTINS_H
102
Note: See TracBrowser for help on using the repository browser.