1// Copyright Contributors to the Open Shading Language project.
2// SPDX-License-Identifier: BSD-3-Clause
3// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
4
5struct vector4
6{
7    float x, y, z, w;
8};
9
10vector4 __operator__add__ (vector4 a, vector4 b) {
11    return vector4 (a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w);
12}
13
14vector4 __operator__sub__ (vector4 a, vector4 b) {
15    return vector4 (a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w);
16}
17
18vector4 __operator__mul__ (vector4 a, vector4 b) {
19    return vector4 (a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w);
20}
21
22vector4 __operator__div__ (vector4 a, vector4 b) {
23    return vector4 (a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w);
24}
25
26vector4 __operator__neg__ (vector4 a) {
27    return vector4 (-a.x, -a.y, -a.z, -a.w);
28}
29
30
31// Make a function to double check it's ok to pass the result of a
32// binary operator into a function argument
33vector4 add (vector4 a, vector4 b)
34{
35    return a+b;
36}
37
38
39
40shader test ()
41{
42    vector4 a = vector4 (.2, .3, .4, .5);
43    vector4 b = vector4 (1, 2, 3, 4);
44    printf ("a = %g %g %g %g\n", a.x, a.y, a.z, a.w);
45    printf ("b = %g %g %g %g\n", b.x, b.y, b.z, b.w);
46
47    vector4 c;
48    c = a + b;
49    printf ("a+b = %g %g %g %g\n", c.x, c.y, c.z, c.w);
50
51    c = a - b;
52    printf ("a-b = %g %g %g %g\n", c.x, c.y, c.z, c.w);
53
54    c = a * b;
55    printf ("a*b = %g %g %g %g\n", c.x, c.y, c.z, c.w);
56
57    c = a / b;
58    printf ("a/b = %g %g %g %g\n", c.x, c.y, c.z, c.w);
59
60    c = -a;
61    printf ("-a = %g %g %g %g\n", c.x, c.y, c.z, c.w);
62
63    // regression test: #762 fixed a bug where passing a struct-returning
64    // binary expression into a function argument would hit an asseriton.
65    c = add (c,c);
66}
67