112771Sqtt2@cornell.edu/*
212771Sqtt2@cornell.edu * Copyright (c) 2018, Cornell University
312771Sqtt2@cornell.edu * All rights reserved.
412771Sqtt2@cornell.edu *
512771Sqtt2@cornell.edu * Redistribution and use in source and binary forms, with or
612771Sqtt2@cornell.edu * without modification, are permitted provided that the following
712771Sqtt2@cornell.edu * conditions are met:
812771Sqtt2@cornell.edu *
912771Sqtt2@cornell.edu * Redistributions of source code must retain the above copyright
1012771Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer.
1112771Sqtt2@cornell.edu *
1212771Sqtt2@cornell.edu * Redistributions in binary form must reproduce the above
1312771Sqtt2@cornell.edu * copyright notice, this list of conditions and the following
1412771Sqtt2@cornell.edu * disclaimer in the documentation and/or other materials provided
1512771Sqtt2@cornell.edu * with the distribution.
1612771Sqtt2@cornell.edu *
1712771Sqtt2@cornell.edu * Neither the name of Cornell University nor the names of its
1812771Sqtt2@cornell.edu * contributors may be used to endorse or promote products derived
1912771Sqtt2@cornell.edu * from this software without specific prior written permission.
2012771Sqtt2@cornell.edu *
2112771Sqtt2@cornell.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
2212771Sqtt2@cornell.edu * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
2312771Sqtt2@cornell.edu * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2412771Sqtt2@cornell.edu * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2512771Sqtt2@cornell.edu * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
2612771Sqtt2@cornell.edu * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2712771Sqtt2@cornell.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2812771Sqtt2@cornell.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2912771Sqtt2@cornell.edu * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
3012771Sqtt2@cornell.edu * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3112771Sqtt2@cornell.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3212771Sqtt2@cornell.edu * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3312771Sqtt2@cornell.edu * POSSIBILITY OF SUCH DAMAGE.
3412771Sqtt2@cornell.edu *
3512771Sqtt2@cornell.edu * Authors: Tuan Ta
3612771Sqtt2@cornell.edu */
3712771Sqtt2@cornell.edu
3812771Sqtt2@cornell.edu//------------------------------------------------------------------------
3912771Sqtt2@cornell.edu// This code tests amomax_d instruction in multi-threading system.
4012771Sqtt2@cornell.edu// All threads execute an amomax_w instruction.
4112771Sqtt2@cornell.edu// Master thread (i.e., thread 0) waits for all threads to complete by
4212771Sqtt2@cornell.edu// spinning on the barrier variable until all threads update the variable.
4312771Sqtt2@cornell.edu// Then, the master thread checks the shared variable's value.
4412771Sqtt2@cornell.edu//------------------------------------------------------------------------
4512771Sqtt2@cornell.edu
4612771Sqtt2@cornell.edu#include "riscv_test.h"
4712771Sqtt2@cornell.edu#include "test_macros.h"
4812771Sqtt2@cornell.edu#include "test_macros_mt.h"
4912771Sqtt2@cornell.edu
5012771Sqtt2@cornell.edu  RVTEST_RV64U
5112771Sqtt2@cornell.edu  RVTEST_CODE_BEGIN
5212771Sqtt2@cornell.edu
5312771Sqtt2@cornell.edu#define RESULT      0xdeadbeefdeadbeef
5412771Sqtt2@cornell.edu
5512771Sqtt2@cornell.edu//------------------------------------------------------------------------
5612771Sqtt2@cornell.edu// Reinitialize shared_var to 0x0000000000000000
5712771Sqtt2@cornell.edu//------------------------------------------------------------------------
5812771Sqtt2@cornell.edu  la  a0, shared_var
5912771Sqtt2@cornell.edu  li  t0, 0x0000000000000000
6012771Sqtt2@cornell.edu  sd  t0, (a0)
6112771Sqtt2@cornell.edu
6212771Sqtt2@cornell.edu//------------------------------------------------------------------------
6312771Sqtt2@cornell.edu// Master thread creates new threads, waits for all threads to complete,
6412771Sqtt2@cornell.edu// deallocates threads and checks result
6512771Sqtt2@cornell.edu//------------------------------------------------------------------------
6612771Sqtt2@cornell.edu  call _create_threads
6712771Sqtt2@cornell.edu  call _join
6812771Sqtt2@cornell.edu  call _delete_threads
6912771Sqtt2@cornell.edu  call _check
7012771Sqtt2@cornell.edu
7112771Sqtt2@cornell.edu  RVTEST_CODE_END
7212771Sqtt2@cornell.edu
7312771Sqtt2@cornell.edu//------------------------------------------------------------------------
7412771Sqtt2@cornell.edu// mt_test function executed in child threads
7512771Sqtt2@cornell.edu// A child thread signals its completion by atomicaly adding 1 to barrier
7612771Sqtt2@cornell.edu//------------------------------------------------------------------------
7712771Sqtt2@cornell.edu_mt_test:
7812771Sqtt2@cornell.edu  la        a0, shared_var
7912771Sqtt2@cornell.edu  la        t0, array_index
8012771Sqtt2@cornell.edu  li        t1, 8
8112771Sqtt2@cornell.edu  amoadd.d  t1, t1, (t0)        // get my array_index
8212771Sqtt2@cornell.edu
8312771Sqtt2@cornell.edu  la        t0, array
8412771Sqtt2@cornell.edu  add       t0, t0, t1
8512771Sqtt2@cornell.edu  ld        t0, (t0)            // get array[array_index]
8612771Sqtt2@cornell.edu
8712771Sqtt2@cornell.edu  amomaxu.d zero, t0, (a0)
8812771Sqtt2@cornell.edu
8912771Sqtt2@cornell.edu  li        t0, 1
9012771Sqtt2@cornell.edu  la        a0, barrier
9112771Sqtt2@cornell.edu  amoadd.d  zero, t0, (a0)
9212771Sqtt2@cornell.edu
9312771Sqtt2@cornell.edu  RVTEST_CODE_END
9412771Sqtt2@cornell.edu
9512771Sqtt2@cornell.edu//------------------------------------------------------------------------
9612771Sqtt2@cornell.edu// Master thread checks result
9712771Sqtt2@cornell.edu//------------------------------------------------------------------------
9812771Sqtt2@cornell.edu_check:
9912771Sqtt2@cornell.edu  la        a0, shared_var
10012771Sqtt2@cornell.edu  li        a1, RESULT
10112771Sqtt2@cornell.edu  ld        a0, (a0)
10212771Sqtt2@cornell.edu  bne       a0, a1, _fail
10312771Sqtt2@cornell.edu  li        a0, SUCCESS
10412771Sqtt2@cornell.edu  ret
10512771Sqtt2@cornell.edu
10612771Sqtt2@cornell.edu_fail:
10712771Sqtt2@cornell.edu  li        a0, FAILURE
10812771Sqtt2@cornell.edu  ret
10912771Sqtt2@cornell.edu
11012771Sqtt2@cornell.edu  .data
11112771Sqtt2@cornell.edu
11212771Sqtt2@cornell.eduMT_DATA
11312771Sqtt2@cornell.eduarray_index:    .dword    0;
114