| 1 |
// SPDX-License-Identifier: Apache-2.0 |
| 2 |
// SPDX-FileCopyrightText: 2026 The Ebitengine Authors |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stdbool.h> |
| 6 |
|
| 7 |
// Test: 12 int32 arguments - fills 8 int registers, then 4 go to stack |
| 8 |
// With tight packing, the 4 stack args pack into 16 bytes (4x4) |
| 9 |
// Without tight packing, they would use 32 bytes (4x8) |
| 10 |
typedef int32_t (*callback_12_int32)(int32_t, int32_t, int32_t, int32_t, |
| 11 |
int32_t, int32_t, int32_t, int32_t, |
| 12 |
int32_t, int32_t, int32_t, int32_t); |
| 13 |
|
| 14 |
int32_t callCallback12Int32(const void *fp) { |
| 15 |
// Using prime numbers to detect argument misalignment |
| 16 |
return ((callback_12_int32)(fp))(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37); |
| 17 |
} |
| 18 |
|
| 19 |
// Test: mixed int64 and int32 arguments with stack spillover |
| 20 |
// 8 int64s fill registers, then int32/int64/int32 go to stack |
| 21 |
// Tests alignment handling when mixing sizes on stack |
| 22 |
typedef int64_t (*callback_mixed_stack)(int64_t, int64_t, int64_t, int64_t, |
| 23 |
int64_t, int64_t, int64_t, int64_t, |
| 24 |
int32_t, int64_t, int32_t); |
| 25 |
|
| 26 |
int64_t callCallbackMixedStack(const void *fp) { |
| 27 |
return ((callback_mixed_stack)(fp))(1, 2, 3, 4, 5, 6, 7, 8, 100, 200, 300); |
| 28 |
} |
| 29 |
|
| 30 |
// Test: various small types on stack |
| 31 |
// 8 int64s fill registers, then bool/int8/uint8/int16/uint16/int32 go to stack |
| 32 |
// Tests byte-level packing of different small types |
| 33 |
typedef int64_t (*callback_small_types)(int64_t, int64_t, int64_t, int64_t, |
| 34 |
int64_t, int64_t, int64_t, int64_t, |
| 35 |
bool, int8_t, uint8_t, int16_t, uint16_t, int32_t); |
| 36 |
|
| 37 |
int64_t callCallbackSmallTypes(const void *fp) { |
| 38 |
return ((callback_small_types)(fp))(1, 2, 3, 4, 5, 6, 7, 8, |
| 39 |
true, -42, 200, -1000, 50000, 123456); |
| 40 |
} |
| 41 |
|
| 42 |
// Test: 10 int32 arguments - simpler case |
| 43 |
// 8 fill registers, 2 go to stack (8 bytes packed vs 16 unpacked) |
| 44 |
typedef int32_t (*callback_10_int32)(int32_t, int32_t, int32_t, int32_t, |
| 45 |
int32_t, int32_t, int32_t, int32_t, |
| 46 |
int32_t, int32_t); |
| 47 |
|
| 48 |
int32_t callCallback10Int32(const void *fp) { |
| 49 |
return ((callback_10_int32)(fp))(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 50 |
} |
| 51 |
|
| 52 |
// Test: 10 float64 arguments - fills 8 float registers, then 2 go to stack |
| 53 |
// Callback returns int64 since purego callbacks don't support float returns |
| 54 |
typedef int64_t (*callback_10_float64)(double, double, double, double, |
| 55 |
double, double, double, double, |
| 56 |
double, double); |
| 57 |
|
| 58 |
int64_t callCallback10Float64(const void *fp) { |
| 59 |
return ((callback_10_float64)(fp))(1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5); |
| 60 |
} |
| 61 |
|
| 62 |
// Test: 12 float32 arguments - fills 8 float registers, then 4 go to stack |
| 63 |
// Callback returns int64 since purego callbacks don't support float returns |
| 64 |
typedef int64_t (*callback_12_float32)(float, float, float, float, |
| 65 |
float, float, float, float, |
| 66 |
float, float, float, float); |
| 67 |
|
| 68 |
int64_t callCallback12Float32(const void *fp) { |
| 69 |
return ((callback_12_float32)(fp))(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, |
| 70 |
7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f); |
| 71 |
} |