1/*
2 * Copyright (c) 2018, Cornell University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * Neither the name of Cornell University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Authors: Tuan Ta
36 */
37
38//------------------------------------------------------------------------
39// This code tests amoadd_d instruction in multi-threading system.
40// Each thread increments a globally shared variable LOOP_COUNT times.
41// Once a thread completes, it signals its completition by atomically
42// incrementing a barrier variable.
43// Master thread (i.e., thread 0) waits for all threads to complete by
44// spinning on the barrier variable until all threads update the variable.
45// Then, the master thread checks the shared variable's value.
46//------------------------------------------------------------------------
47
48#include "riscv_test.h"
49#include "test_macros.h"
50#include "test_macros_mt.h"
51
52  RVTEST_RV64U
53  RVTEST_CODE_BEGIN
54
55#define LOOP_COUNT  1000
56#define RESULT      LOOP_COUNT * NUM_THREADS
57
58//------------------------------------------------------------------------
59// Master thread creates new threads, waits for all threads to complete,
60// deallocates threads and checks result
61//------------------------------------------------------------------------
62  call _create_threads
63  call _join
64  call _delete_threads
65  call _check
66
67  RVTEST_CODE_END
68
69//------------------------------------------------------------------------
70// mt_test function executed in child threads
71// A child thread signals its completion by atomicaly adding 1 to barrier
72//------------------------------------------------------------------------
73_mt_test:
74  li        t0, 1               // one operand of amoadd_w
75  li        t1, LOOP_COUNT      // loop count
76  la        a0, shared_var
771:
78  amoadd.d  zero, t0, (a0)
79  addi      t1, t1, -1
80  bnez      t1, 1b
81
82  la        a0, barrier
83  amoadd.d  zero, t0, (a0)
84
85  RVTEST_CODE_END
86
87//------------------------------------------------------------------------
88// Master thread checks result
89//------------------------------------------------------------------------
90_check:
91  la        a0, shared_var
92  li        a1, RESULT
93  ld        a0, (a0)
94  bne       a0, a1, _fail
95  li        a0, SUCCESS
96  ret
97
98_fail:
99  li        a0, FAILURE
100  ret
101
102  .data
103
104MT_DATA
105