1// This file is automatically generated from
2// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include "shared.rsh"
20
21// There is a C99 rule (under "Structure and union members") that
22// reads "One special guarantee is made in order to simplify the use
23// of unions: if a union contains several structures that share a
24// common initial sequence, and if the union object currently contains
25// one of these structures, it is permitted to inspect the common
26// initial part of any of them anywhere that a declaration of the
27// completed type of the union is visible. Two structures share a
28// common initial sequence if corresponding members have compatible
29// types (and, for bit-fields, the same widths) for a sequence of one
30// or more initial members."
31//
32// We want to ensure that the common initial sequences of exported
33// and non-exported types have the same layout.
34
35// An exported type (because we declare a global variable of this type)
36struct NoBitfield {
37    int I;
38    // expect 4 bytes of padding here
39    long L;
40    float F;
41    // expect 4 bytes of padding here
42};
43
44struct NoBitfield junk;  // just to make this an exported type
45
46// A non-exported type that shares a common initial sequence with NoBitfield
47struct Bitfield {
48    int I;
49    // expect 4 bytes of padding here
50    long L;
51    uint U:3;
52};
53
54union CommonInitialSequence {
55    struct NoBitfield nbf;
56    struct   Bitfield  bf;
57};
58
59static union CommonInitialSequence U, V;
60
61static struct NoBitfield *nbf;
62static struct   Bitfield * bf;
63
64// Note: Sets through the exported type (NoBitfield)
65void setUnion(long argL, int argI) {
66    nbf->L = argL;
67    nbf->I = argI;
68}
69
70// Note: Tests through the non-exported type (Bitfield)
71void testUnion(long argL, int argI) {
72    bool failed = false;
73
74    rsDebug("argI    ", argI);
75    rsDebug("bf->I   ", bf->I);
76    rsDebug("argL.lo ", (unsigned)argL & ~0U);
77    rsDebug("bf->L.lo", (unsigned)bf->L & ~0U);
78    rsDebug("argL.hi ", (unsigned)((ulong)argL >> 32));
79    rsDebug("bf->L.hi", (unsigned)((ulong)bf->L >> 32));
80
81    _RS_ASSERT(bf->I == argI);
82    _RS_ASSERT(bf->L == argL);
83
84    if (failed) {
85        rsDebug("bitfield FAILED", 0);
86        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
87    }
88    else {
89        rsDebug("bitfield PASSED", 0);
90        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
91    }
92}
93
94// Note: Prevent compiler from optimizing setUnion()/testUnion()
95//       to convert indirect accesses through nbf/bf into direct
96//       accesses through U or V.
97void choose(int i) {
98    if (i) {
99        nbf = &U.nbf;
100         bf = &U. bf;
101    } else {
102        nbf = &V.nbf;
103         bf = &V. bf;
104    }
105}
106