1/*
2Copyright (c) 2012 Advanced Micro Devices, Inc.
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. This notice may not be removed or altered from any source distribution.
13*/
14//Originally written by Takahiro Harada
15
16
17typedef unsigned int u32;
18#define GET_GROUP_IDX get_group_id(0)
19#define GET_LOCAL_IDX get_local_id(0)
20#define GET_GLOBAL_IDX get_global_id(0)
21#define GET_GROUP_SIZE get_local_size(0)
22#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
23
24typedef struct
25{
26	u32 m_key;
27	u32 m_value;
28}SortData;
29
30
31
32typedef struct
33{
34	u32 m_nSrc;
35	u32 m_nDst;
36	u32 m_padding[2];
37} ConstBuffer;
38
39
40
41__attribute__((reqd_work_group_size(64,1,1)))
42__kernel
43void SearchSortDataLowerKernel(__global SortData* src, __global u32 *dst,
44					unsigned int nSrc, unsigned int nDst)
45{
46	int gIdx = GET_GLOBAL_IDX;
47
48	if( gIdx < nSrc )
49	{
50		SortData first; first.m_key = (u32)(-1); first.m_value = (u32)(-1);
51		SortData end; end.m_key = nDst; end.m_value = nDst;
52
53		SortData iData = (gIdx==0)? first: src[gIdx-1];
54		SortData jData = (gIdx==nSrc)? end: src[gIdx];
55
56		if( iData.m_key != jData.m_key )
57		{
58//			for(u32 k=iData.m_key+1; k<=min(jData.m_key, nDst-1); k++)
59			u32 k = jData.m_key;
60			{
61				dst[k] = gIdx;
62			}
63		}
64	}
65}
66
67
68__attribute__((reqd_work_group_size(64,1,1)))
69__kernel
70void SearchSortDataUpperKernel(__global SortData* src, __global u32 *dst,
71					unsigned int nSrc, unsigned int nDst)
72{
73	int gIdx = GET_GLOBAL_IDX+1;
74
75	if( gIdx < nSrc+1 )
76	{
77		SortData first; first.m_key = 0; first.m_value = 0;
78		SortData end; end.m_key = nDst; end.m_value = nDst;
79
80		SortData iData = src[gIdx-1];
81		SortData jData = (gIdx==nSrc)? end: src[gIdx];
82
83		if( iData.m_key != jData.m_key )
84		{
85			u32 k = iData.m_key;
86			{
87				dst[k] = gIdx;
88			}
89		}
90	}
91}
92
93__attribute__((reqd_work_group_size(64,1,1)))
94__kernel
95void SubtractKernel(__global u32* A, __global u32 *B, __global u32 *C,
96					unsigned int nSrc, unsigned int nDst)
97{
98	int gIdx = GET_GLOBAL_IDX;
99
100
101	if( gIdx < nDst )
102	{
103		C[gIdx] = A[gIdx] - B[gIdx];
104	}
105}
106
107