* gcc.target/powerpc/builtins-1-be.c <vclzb>: Rename duplicate test
[official-gcc.git] / liboffloadmic / runtime / offload_common.cpp
blobaf569ae3376e44a53f5c57d2dbe7f719fbff408b
1 /*
2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LINUX) || defined(FREEBSD)
32 #include <mm_malloc.h>
33 #endif
35 #include "offload_common.h"
37 // The debug routines
39 #if OFFLOAD_DEBUG > 0
41 void __dump_bytes(
42 int trace_level,
43 const void *data,
44 int len
47 if (console_enabled > trace_level) {
48 const uint8_t *arr = (const uint8_t*) data;
49 char buffer[4096];
50 char *bufferp;
51 int count = 0;
53 bufferp = buffer;
54 while (len--) {
55 sprintf(bufferp, "%02x", *arr++);
56 bufferp += 2;
57 count++;
58 if ((count&3) == 0) {
59 sprintf(bufferp, " ");
60 bufferp++;
62 if ((count&63) == 0) {
63 OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer);
64 bufferp = buffer;
65 count = 0;
68 if (count) {
69 OFFLOAD_DEBUG_TRACE(trace_level, "%s\n", buffer);
73 #endif // OFFLOAD_DEBUG
75 // The Marshaller and associated routines
77 void Marshaller::send_data(
78 const void *data,
79 int64_t length
82 OFFLOAD_DEBUG_TRACE(2, "send_data(%p, %lld)\n",
83 data, length);
84 memcpy(buffer_ptr, data, (size_t)length);
85 buffer_ptr += length;
86 tfr_size += length;
89 void Marshaller::receive_data(
90 void *data,
91 int64_t length
94 OFFLOAD_DEBUG_TRACE(2, "receive_data(%p, %lld)\n",
95 data, length);
96 memcpy(data, buffer_ptr, (size_t)length);
97 buffer_ptr += length;
98 tfr_size += length;
101 // Send function pointer
102 void Marshaller::send_func_ptr(
103 const void* data
106 const char* name;
107 size_t length;
109 if (data != 0) {
110 name = __offload_funcs.find_name(data);
111 if (name == 0) {
112 #if OFFLOAD_DEBUG > 0
113 if (console_enabled > 2) {
114 __offload_funcs.dump();
116 #endif // OFFLOAD_DEBUG > 0
118 LIBOFFLOAD_ERROR(c_send_func_ptr, data);
119 exit(1);
121 length = strlen(name) + 1;
123 else {
124 name = "";
125 length = 1;
128 memcpy(buffer_ptr, name, length);
129 buffer_ptr += length;
130 tfr_size += length;
133 // Receive function pointer
134 void Marshaller::receive_func_ptr(
135 const void** data
138 const char* name;
139 size_t length;
141 name = (const char*) buffer_ptr;
142 if (name[0] != '\0') {
143 *data = __offload_funcs.find_addr(name);
144 if (*data == 0) {
145 #if OFFLOAD_DEBUG > 0
146 if (console_enabled > 2) {
147 __offload_funcs.dump();
149 #endif // OFFLOAD_DEBUG > 0
151 LIBOFFLOAD_ERROR(c_receive_func_ptr, name);
152 exit(1);
154 length = strlen(name) + 1;
156 else {
157 *data = 0;
158 length = 1;
161 buffer_ptr += length;
162 tfr_size += length;
165 // End of the Marshaller and associated routines
167 extern void *OFFLOAD_MALLOC(
168 size_t size,
169 size_t align
172 void *ptr;
173 int err;
175 OFFLOAD_DEBUG_TRACE(2, "%s(%lld, %lld)\n", __func__, size, align);
177 if (align < sizeof(void*)) {
178 align = sizeof(void*);
181 ptr = _mm_malloc(size, align);
182 if (ptr == NULL) {
183 LIBOFFLOAD_ERROR(c_offload_malloc, size, align);
184 exit(1);
187 OFFLOAD_DEBUG_TRACE(2, "%s returned %p\n", __func__, ptr);
189 return ptr;