aboutsummaryrefslogtreecommitdiff
path: root/liboffloadmic/include/coi/common/COIMacros_common.h
blob: 07c9b8cb356b7ed6e8cd98a11d1ab2154115de78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright 2010-2015 Intel Corporation.
 *
 * This library 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, version 2.1.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 *
 * Disclaimer: The codes contained in these modules may be specific
 * to the Intel Software Development Platform codenamed Knights Ferry,
 * and the Intel product codenamed Knights Corner, and are not backward
 * compatible with other Intel products. Additionally, Intel will NOT
 * support the codes or instruction set in future products.
 *
 * Intel offers no warranty of any kind regarding the code. This code is
 * licensed on an "AS IS" basis and Intel is not obligated to provide
 * any support, assistance, installation, training, or other services
 * of any kind. Intel is also not obligated to provide any updates,
 * enhancements or extensions. Intel specifically disclaims any warranty
 * of merchantability, non-infringement, fitness for any particular
 * purpose, and any other warranty.
 *
 * Further, Intel disclaims all liability of any kind, including but
 * not limited to liability for infringement of any proprietary rights,
 * relating to the use of the code, even if Intel is notified of the
 * possibility of such liability. Except as expressly stated in an Intel
 * license agreement provided with this code and agreed upon with Intel,
 * no license, express or implied, by estoppel or otherwise, to any
 * intellectual property rights is granted herein.
 */

#ifndef _COIMACROS_COMMON_H
#define _COIMACROS_COMMON_H

#include <string.h>
#include "../source/COIPipeline_source.h"
#include "../common/COITypes_common.h"

/// @file common/COIMacros_common.h
/// Commonly used macros

// Note that UNUSUED_ATTR means that it is "possibly" unused, not "definitely".
// This should compile out in release mode if indeed it is unused.
    #define UNUSED_ATTR __attribute__((unused))
    #include <sched.h>
#ifndef UNREFERENCED_CONST_PARAM
#define UNREFERENCED_CONST_PARAM(P)     { void* x UNUSED_ATTR = \
                                                 (void*)(uint64_t)P; \
                                        }
#endif

// This seems to work on everything. 
#ifndef UNREFERENCED_PARAM
#define UNREFERENCED_PARAM(P)          (P = P)
#endif

#ifndef SYMBOL_VERSION

/* Linux support: */

    #define SYMBOL_VERSION( SYMBOL , VERSION ) SYMBOL ## VERSION

#endif

/* The following are static inline definitions of functions used for manipulating
   COI_CPU_MASK info (The COI_CPU_MASK type is declared as an array of 16 uint64_t's
   in COITypes_common.h "typedef uint64_t COI_CPU_MASK[16]").

   These static inlined functions are intended on being roughly the same as the Linux
   CPU_* macros defined in sched.h - with the important difference being a different
   fundamental type difference: cpu_set_t versus COI_CPU_MASK.

   The motivation for writing this code was to ease portability on the host side of COI
   applications to both Windows and Linux.
*/

/* Roughly equivalent to CPU_ISSET(). */
static inline uint64_t COI_CPU_MASK_ISSET(int bitNumber, const COI_CPU_MASK cpu_mask)
{
  if ((size_t)bitNumber < sizeof(COI_CPU_MASK)*8)
        return ((cpu_mask)[bitNumber/64] & (((uint64_t)1) << (bitNumber%64)));
    return 0;
}

/* Roughly equivalent to CPU_SET(). */
static inline void COI_CPU_MASK_SET(int bitNumber, COI_CPU_MASK cpu_mask)
{
  if ((size_t)bitNumber < sizeof(COI_CPU_MASK)*8)
        ((cpu_mask)[bitNumber/64] |= (((uint64_t)1) << (bitNumber%64)));
}

/* Roughly equivalent to CPU_ZERO(). */
static inline void COI_CPU_MASK_ZERO(COI_CPU_MASK cpu_mask)
{
    memset(cpu_mask,0,sizeof(COI_CPU_MASK));
}

/* Roughly equivalent to CPU_AND(). */
static inline void COI_CPU_MASK_AND(COI_CPU_MASK dst, const COI_CPU_MASK src1, const COI_CPU_MASK src2)
{
    const unsigned int loopIterations = sizeof(COI_CPU_MASK) / sizeof(dst[0]);

    for(unsigned int i=0;i<loopIterations;++i)
        dst[i] = src1[i] & src2[i];
}

/* Roughly equivalent to CPU_XOR(). */
static inline void COI_CPU_MASK_XOR(COI_CPU_MASK dst, const COI_CPU_MASK src1, const COI_CPU_MASK src2)
{
    const unsigned int loopIterations = sizeof(COI_CPU_MASK) / sizeof(dst[0]);

    for(unsigned int i=0;i<loopIterations;++i)
        dst[i] = src1[i] ^ src2[i];
}

/* Roughly equivalent to CPU_OR(). */
static inline void COI_CPU_MASK_OR(COI_CPU_MASK dst, const COI_CPU_MASK src1, const COI_CPU_MASK src2)
{
    const unsigned int loopIterations = sizeof(COI_CPU_MASK) / sizeof(dst[0]);

    for(unsigned int i=0;i<loopIterations;++i)
        dst[i] = src1[i] | src2[i];
}

/* Utility function for COI_CPU_MASK_COUNT() below. */
static inline int __COI_CountBits(uint64_t n)
{
    int cnt=0;

    for (;n;cnt++)
        n &= (n-1);
    return cnt;
}

/* Roughly equivalent to CPU_COUNT(). */
static inline int COI_CPU_MASK_COUNT(const COI_CPU_MASK cpu_mask)
{
    int cnt=0;
    const unsigned int loopIterations = sizeof(COI_CPU_MASK) / sizeof(cpu_mask[0]);

    for(unsigned int i=0;i < loopIterations;++i)
    {
        cnt += __COI_CountBits(cpu_mask[i]);
    }
    return cnt;
}

/* Roughly equivalent to CPU_EQUAL(). */
static inline int COI_CPU_MASK_EQUAL(const COI_CPU_MASK cpu_mask1,const COI_CPU_MASK cpu_mask2)
{
    const unsigned int loopIterations = sizeof(COI_CPU_MASK) / sizeof(cpu_mask1[0]);

    for(unsigned int i=0;i < loopIterations;++i)
    {
        if (cpu_mask1[i] != cpu_mask2[i])
            return 0;
    }
    return 1;
}


/* Utility function to translate from cpu_set * to COI_CPU_MASK. */
static inline void COI_CPU_MASK_XLATE(COI_CPU_MASK dest,const cpu_set_t *src)
{
    COI_CPU_MASK_ZERO(dest);
#if 0
    /* Slightly slower version than the following #else/#endif block. Left here only to
         document the intent of the code. */
    for(unsigned int i=0;i < sizeof(cpu_set_t)*8;++i)
        if (CPU_ISSET(i,src))
            COI_CPU_MASK_SET(i,dest);
#else
    for(unsigned int i=0;i < sizeof(COI_CPU_MASK)/sizeof(dest[0]);++i)
    {
        for(unsigned int j=0;j < 64;++j)
        {
            if (CPU_ISSET(i*64+j,src))
                dest[i] |= ((uint64_t)1) << j;
        }
    }
#endif
}

/* Utility function to translate from COI_CPU_MASK to cpu_set *. */
static inline void COI_CPU_MASK_XLATE_EX(cpu_set_t *dest,const COI_CPU_MASK src)
{
    CPU_ZERO(dest);
#if 0
    /* Slightly slower version than the following #else/#endif block. Left here only to
         document the intent of the code. */
    for(unsigned int i=0;i < sizeof(COI_CPU_MASK)*8;++i)
        if (COI_CPU_MASK_ISSET(i,src))
            CPU_SET(i,dest);
#else
    for(unsigned int i=0;i < sizeof(COI_CPU_MASK)/sizeof(src[0]);++i)
    {
        const uint64_t cpu_mask = src[i];

        for(unsigned int j=0;j < 64;++j)
        {
            const uint64_t bit = ((uint64_t)1) << j;

            if (bit & cpu_mask)
                CPU_SET(i*64+j,dest);
        }
    }
#endif
}


#endif /* _COIMACROS_COMMON_H */