rust_test_helpers.c 5.1 KB
Newer Older
1 2
// Helper functions used only in tests

B
Brian Anderson 已提交
3 4
#include <stdint.h>
#include <assert.h>
5
#include <stdarg.h>
6 7 8

// These functions are used in the unit tests for C ABI calls.

9
uint32_t
10 11 12 13
rust_dbg_extern_identity_u32(uint32_t u) {
    return u;
}

14
uint64_t
15 16 17 18
rust_dbg_extern_identity_u64(uint64_t u) {
    return u;
}

19
double
20 21 22 23
rust_dbg_extern_identity_double(double u) {
    return u;
}

24
char
25 26 27 28 29 30
rust_dbg_extern_identity_u8(char u) {
    return u;
}

typedef void *(*dbg_callback)(void*);

31
void *
32 33 34 35
rust_dbg_call(dbg_callback cb, void *data) {
    return cb(data);
}

36
void rust_dbg_do_nothing() { }
37 38 39 40 41 42

struct TwoU8s {
    uint8_t one;
    uint8_t two;
};

43
struct TwoU8s
44 45 46 47 48 49 50
rust_dbg_extern_return_TwoU8s() {
    struct TwoU8s s;
    s.one = 10;
    s.two = 20;
    return s;
}

51 52
struct TwoU8s
rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) {
53 54 55 56 57 58 59 60
    return u;
}

struct TwoU16s {
    uint16_t one;
    uint16_t two;
};

61
struct TwoU16s
62 63 64 65 66 67 68
rust_dbg_extern_return_TwoU16s() {
    struct TwoU16s s;
    s.one = 10;
    s.two = 20;
    return s;
}

69 70
struct TwoU16s
rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) {
71 72 73 74 75 76 77 78
    return u;
}

struct TwoU32s {
    uint32_t one;
    uint32_t two;
};

79
struct TwoU32s
80 81 82 83 84 85 86
rust_dbg_extern_return_TwoU32s() {
    struct TwoU32s s;
    s.one = 10;
    s.two = 20;
    return s;
}

87 88
struct TwoU32s
rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) {
89 90 91 92 93 94 95 96
    return u;
}

struct TwoU64s {
    uint64_t one;
    uint64_t two;
};

97
struct TwoU64s
98 99 100 101 102 103 104
rust_dbg_extern_return_TwoU64s() {
    struct TwoU64s s;
    s.one = 10;
    s.two = 20;
    return s;
}

105 106
struct TwoU64s
rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) {
107 108 109 110 111 112 113 114
    return u;
}

struct TwoDoubles {
    double one;
    double two;
};

115 116
struct TwoDoubles
rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) {
117 118
    return u;
}
119

120 121 122 123 124 125 126 127 128
struct ManyInts {
    int8_t arg1;
    int16_t arg2;
    int32_t arg3;
    int16_t arg4;
    int8_t arg5;
    struct TwoU8s arg6;
};

129 130
// MSVC doesn't allow empty structs or unions
#ifndef _MSC_VER
131 132 133 134 135 136 137 138 139 140 141 142 143
struct Empty {
};

void
rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
    assert(v1.arg1 == v2.arg1 + 1);
    assert(v1.arg2 == v2.arg2 + 1);
    assert(v1.arg3 == v2.arg3 + 1);
    assert(v1.arg4 == v2.arg4 + 1);
    assert(v1.arg5 == v2.arg5 + 1);
    assert(v1.arg6.one == v2.arg6.one + 1);
    assert(v1.arg6.two == v2.arg6.two + 1);
}
144
#endif
145

146
intptr_t
147 148 149
rust_get_test_int() {
  return 1;
}
150

151 152 153 154 155
char *
rust_get_null_ptr() {
    return 0;
}

156
// Debug helpers strictly to verify ABI conformance.
157 158 159 160 161 162 163 164 165 166 167 168 169 170

struct quad {
    uint64_t a;
    uint64_t b;
    uint64_t c;
    uint64_t d;
};

struct floats {
    double a;
    uint8_t b;
    double c;
};

171 172 173 174 175 176
struct quad
rust_dbg_abi_1(struct quad q) {
    struct quad qq = { q.c + 1,
                       q.d - 1,
                       q.a + 1,
                       q.b - 1 };
177 178 179
    return qq;
}

180 181 182 183 184
struct floats
rust_dbg_abi_2(struct floats f) {
    struct floats ff = { f.c + 1.0,
                         0xff,
                         f.a - 1.0 };
185 186 187
    return ff;
}

188
int
R
Ricky Taylor 已提交
189
rust_dbg_static_mut = 3;
190

191
void
192 193 194
rust_dbg_static_mut_check_four() {
    assert(rust_dbg_static_mut == 4);
}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

struct S {
    uint64_t x;
    uint64_t y;
    uint64_t z;
};

uint64_t get_x(struct S s) {
    return s.x;
}

uint64_t get_y(struct S s) {
    return s.y;
}

uint64_t get_z(struct S s) {
    return s.z;
}
P
Peter Marheine 已提交
213 214 215 216

uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
    return f.c;
}
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
struct quad_floats {
    float a;
    float b;
    float c;
    float d;
};

float get_c_exhaust_sysv64_ints(
    void *a,
    void *b,
    void *c,
    void *d,
    void *e,
    void *f,
    // `f` used the last integer register, so `g` goes on the stack.
    // It also used to bring the "count of available integer registers" down to
    // `-1` which broke the next SSE-only aggregate argument (`h`) - see #62350.
    void *g,
    struct quad_floats h
) {
    return h.c;
}

241
// Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs
242 243 244
// passed as variadic arguments. There are two versions of this function: the
// variadic one, and the one that takes a `va_list`.
double rust_valist_interesting_average(uint64_t n, va_list pairs) {
245 246 247 248 249 250 251 252
    double sum = 0.0;
    int i;
    for(i = 0; i < n; i += 1) {
        sum += (double)va_arg(pairs, int64_t);
        sum += va_arg(pairs, double);
    }
    return sum / n;
}
253

254 255 256 257 258 259 260 261 262
double rust_interesting_average(uint64_t n, ...) {
    double sum;
    va_list pairs;
    va_start(pairs, n);
    sum = rust_valist_interesting_average(n, pairs);
    va_end(pairs);
    return sum;
}

263 264 265
int32_t rust_int8_to_int32(int8_t x) {
    return (int32_t)x;
}
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

typedef union LARGE_INTEGER {
  struct {
    uint32_t LowPart;
    uint32_t HighPart;
  };
  struct {
    uint32_t LowPart;
    uint32_t HighPart;
  } u;
  uint64_t QuadPart;
} LARGE_INTEGER;

LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
    li.LowPart += 1;
    li.HighPart += 1;
    li.u.LowPart += 1;
    li.u.HighPart += 1;
    li.QuadPart += 1;
    return li;
}
E
est31 已提交
287

288
#if __SIZEOF_INT128__ == 16
E
est31 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302

unsigned __int128 identity(unsigned __int128 a) {
    return a;
}

__int128 square(__int128 a) {
    return a * a;
}

__int128 sub(__int128 a, __int128 b) {
    return a - b;
}

#endif