github.com/ebitengine/purego@v0.8.0-alpha.2.0.20240512170805-6cd12240d332/testdata/structtest/struct_test.c (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2024 The Ebitengine Authors
     3  
     4  // Empty is empty
     5  struct Empty {};
     6  
     7  // NoStruct tests that an empty struct doesn't cause issues
     8  unsigned long NoStruct(struct Empty e) {
     9      return 0xdeadbeef;
    10  }
    11  
    12  struct EmptyEmpty {
    13      struct Empty x;
    14  };
    15  
    16  unsigned long EmptyEmpty(struct EmptyEmpty e) {
    17      return 0xdeadbeef;
    18  }
    19  
    20  unsigned long EmptyEmptyWithReg(unsigned int x, struct EmptyEmpty e, unsigned int y) {
    21      return (x << 16) | y;
    22  }
    23  
    24  // GreaterThan16Bytes is 24 bytes on 64 bit systems
    25  struct GreaterThan16Bytes {
    26     long  *x, *y, *z;
    27  };
    28  
    29  // GreaterThan16Bytes is a basic test for structs bigger than 16 bytes
    30  unsigned long GreaterThan16Bytes(struct GreaterThan16Bytes g) {
    31      return *g.x + *g.y + *g.z;
    32  }
    33  
    34  // AfterRegisters tests to make sure that structs placed on the stack work properly
    35  unsigned long AfterRegisters(long a, long b, long c, long d, long e, long f, long g, long h, struct GreaterThan16Bytes bytes) {
    36      long registers = a + b + c + d + e + f + g + h;
    37      long stack =  *bytes.x + *bytes.y + *bytes.z;
    38      if (registers != stack) {
    39          return 0xbadbad;
    40      }
    41      if (stack != 0xdeadbeef) {
    42          return 0xcafebad;
    43      }
    44      return stack;
    45  }
    46  
    47  unsigned long BeforeRegisters(struct GreaterThan16Bytes bytes, long a, long b) {
    48      return *bytes.x + *bytes.y + *bytes.z + a + b;
    49  }
    50  
    51  struct GreaterThan16BytesStruct {
    52      struct {
    53          long  *x, *y, *z;
    54      } a ;
    55  };
    56  
    57  unsigned long GreaterThan16BytesStruct(struct GreaterThan16BytesStruct g) {
    58      return *(g.a.x) + *(g.a.y) + *(g.a.z);
    59  }
    60  
    61  struct IntLessThan16Bytes {
    62      long x, y;
    63  };
    64  
    65  unsigned long IntLessThan16Bytes(struct IntLessThan16Bytes l) {
    66      return l.x + l.y;
    67  }
    68  
    69  struct FloatLessThan16Bytes {
    70      float x, y;
    71  };
    72  
    73  float FloatLessThan16Bytes(struct FloatLessThan16Bytes f) {
    74      return f.x + f.y;
    75  }
    76  
    77  struct ThreeSmallFields {
    78      float x, y, z;
    79  };
    80  
    81  float ThreeSmallFields(struct ThreeSmallFields f) {
    82      return f.x + f.y + f.z;
    83  }
    84  
    85  struct FloatAndInt {
    86      float x;
    87      int   y;
    88  };
    89  
    90  float FloatAndInt(struct FloatAndInt f) {
    91      return f.x + f.y;
    92  }
    93  
    94  struct DoubleStruct {
    95      double x;
    96  };
    97  
    98  double DoubleStruct(struct DoubleStruct d) {
    99      return d.x;
   100  }
   101  
   102  struct TwoDoubleStruct {
   103      double x, y;
   104  };
   105  
   106  double TwoDoubleStruct(struct TwoDoubleStruct d) {
   107      return d.x + d.y;
   108  }
   109  
   110  struct TwoDoubleTwoStruct {
   111      struct {
   112          double x, y;
   113      } s;
   114  };
   115  
   116  double TwoDoubleTwoStruct(struct TwoDoubleTwoStruct d) {
   117      return d.s.x + d.s.y;
   118  }
   119  
   120  struct ThreeDoubleStruct {
   121      double x, y, z;
   122  };
   123  
   124  double ThreeDoubleStruct(struct ThreeDoubleStruct d) {
   125      return d.x + d.y + d.z;
   126  }
   127  
   128  struct LargeFloatStruct {
   129      double a, b, c, d, e, f;
   130  };
   131  
   132  double LargeFloatStruct(struct LargeFloatStruct s) {
   133      return s.a + s.b + s.c + s.d + s.e + s.f;
   134  }
   135  
   136  double LargeFloatStructWithRegs(double a, double b, double c, struct LargeFloatStruct s) {
   137      return a + b + c + s.a + s.b + s.c + s.d + s.e + s.f;
   138  }
   139  
   140  struct Rect {
   141      double x, y, w, h;
   142  };
   143  
   144  double Rectangle(struct Rect rect) {
   145      return rect.x + rect.y + rect.w + rect.h;
   146  }
   147  
   148  double RectangleSubtract(struct Rect rect) {
   149      return (rect.x + rect.y) - (rect.w + rect.h);
   150  }
   151  
   152  double RectangleWithRegs(double a, double b, double c, double d, double e, struct Rect rect) {
   153      return a + b + c + d + e + rect.x + rect.y + rect.w + rect.h;
   154  }
   155  
   156  struct FloatArray {
   157      double a[2];
   158  };
   159  
   160  double FloatArray(struct FloatArray f) {
   161      return f.a[0] + f.a[1];
   162  }
   163  
   164  struct UnsignedChar4Bytes {
   165      unsigned char a, b, c, d;
   166  };
   167  
   168  unsigned int UnsignedChar4Bytes(struct UnsignedChar4Bytes b) {
   169      return (((int)b.a)<<24) | (((int)b.b)<<16) | (((int)b.c)<<8) | (((int)b.d)<<0);
   170  }
   171  
   172  struct UnsignedChar4BytesStruct {
   173      struct {
   174          unsigned char a;
   175      } x;
   176      struct {
   177          unsigned char b;
   178      } y;
   179      struct {
   180          unsigned char c;
   181      } z;
   182      struct {
   183          unsigned char d;
   184      } w;
   185  };
   186  
   187  unsigned int UnsignedChar4BytesStruct(struct UnsignedChar4BytesStruct b) {
   188      return (((int)b.x.a)<<24) | (((int)b.y.b)<<16) | (((int)b.z.c)<<8) | (((int)b.w.d)<<0);
   189  }
   190  
   191  struct Short {
   192      unsigned short a, b, c, d;
   193  };
   194  
   195  unsigned long Short(struct Short s) {
   196      return (long)s.a << 48 | (long)s.b << 32 | (long)s.c << 16 | (long)s.d << 0;
   197  }
   198  
   199  struct Int {
   200      unsigned int a, b;
   201  };
   202  
   203  unsigned long Int(struct Int i) {
   204      return (long)i.a << 32 | (long)i.b << 0;
   205  }
   206  
   207  struct Long {
   208      unsigned long a;
   209  };
   210  
   211  unsigned long Long(struct Long l) {
   212      return l.a;
   213  }
   214  
   215  struct Char8Bytes {
   216      signed char a, b, c, d, e, f, g, h;
   217  };
   218  
   219  int Char8Bytes(struct Char8Bytes b) {
   220      return (int)b.a + (int)b.b + (int)b.c + (int)b.d + (int)b.e + (int)b.f + (int)b.g + (int)b.h;
   221  }
   222  
   223  struct Odd {
   224      unsigned char a, b, c;
   225  };
   226  
   227  int Odd(struct Odd o) {
   228      return (int)o.a + (int)o.b + (int)o.c;
   229  }
   230  
   231  struct Char2Short1 {
   232      unsigned char a, b;
   233      unsigned short c;
   234  };
   235  
   236  int Char2Short1s(struct Char2Short1 s) {
   237      return (int)s.a + (int)s.b + (int)s.c;
   238  }
   239  
   240  struct SignedChar2Short1 {
   241      signed char a, b;
   242      signed short c;
   243  };
   244  
   245  int SignedChar2Short1(struct SignedChar2Short1 s) {
   246      return s.a + s.b + s.c;
   247  }
   248  
   249  struct Array4UnsignedChars {
   250      unsigned char a[4];
   251  };
   252  
   253  unsigned int Array4UnsignedChars(struct Array4UnsignedChars a) {
   254      return (((int)a.a[0])<<24) | (((int)a.a[1])<<16) | (((int)a.a[2])<<8) | (((int)a.a[3])<<0);
   255  }
   256  
   257  struct Array3UnsignedChar {
   258      unsigned char a[3];
   259  };
   260  
   261  unsigned int Array3UnsignedChars(struct Array3UnsignedChar a) {
   262      return (((int)a.a[0])<<24) | (((int)a.a[1])<<16) | (((int)a.a[2])<<8) | 0xef;
   263  }
   264  
   265  struct Array2UnsignedShort {
   266      unsigned short a[2];
   267  };
   268  
   269  unsigned int Array2UnsignedShorts(struct Array2UnsignedShort a) {
   270      return (((int)a.a[0])<<16) | (((int)a.a[1])<<0);
   271  }
   272  
   273  struct Array4Chars {
   274      char a[4];
   275  };
   276  
   277  int Array4Chars(struct Array4Chars a) {
   278      return (int)a.a[0] + (int)a.a[1] + (int)a.a[2] + (int)a.a[3];
   279  }
   280  
   281  struct Array2Short {
   282      short a[2];
   283  };
   284  
   285  int Array2Shorts(struct Array2Short a) {
   286      return (int)a.a[0] + (int)a.a[1];
   287  }
   288  
   289  struct Array3Short {
   290      short a[3];
   291  };
   292  
   293  int Array3Shorts(struct Array3Short a) {
   294      return (int)a.a[0] + (int)a.a[1] + (int)a.a[2];
   295  }
   296  
   297  struct BoolStruct {
   298      _Bool b;
   299  };
   300  
   301  _Bool BoolStruct(struct BoolStruct b) {
   302      return b.b;
   303  }
   304  
   305  struct BoolFloat {
   306      _Bool b;
   307      float f;
   308  };
   309  
   310  float BoolFloat(struct BoolFloat s) {
   311      if (s.b)
   312          return s.f;
   313      return -s.f;
   314  }
   315  
   316  struct Content {
   317        struct { double x, y; } point;
   318        struct { double width, height; } size;
   319  };
   320  
   321  unsigned long InitWithContentRect(int *win, struct Content c, int style, int backing, _Bool flag) {
   322    if (win == 0)
   323        return 0xBAD;
   324    if (!flag)
   325        return 0xF1A6; // FLAG
   326    return (unsigned long)(c.point.x + c.point.y + c.size.width + c.size.height) / (style - backing);
   327  }