]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Source/CV_CoreInstr.c
RTX5: corrected typo (Capiversion in pdsc)
[cmsis] / CMSIS / CoreValidation / Source / CV_CoreInstr.c
1 /*-----------------------------------------------------------------------------
2  *      Name:         CV_CoreInstr.c 
3  *      Purpose:      CMSIS CORE validation tests implementation
4  *-----------------------------------------------------------------------------
5  *      Copyright (c) 2017 ARM Limited. All rights reserved.
6  *----------------------------------------------------------------------------*/
7
8 #include "CV_Framework.h"
9 #include "cmsis_cv.h"
10
11 /*-----------------------------------------------------------------------------
12  *      Test implementation
13  *----------------------------------------------------------------------------*/
14
15 /*-----------------------------------------------------------------------------
16  *      Test cases
17  *----------------------------------------------------------------------------*/
18
19 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
20 /**
21 \brief Test case: TC_CoreInstr_NOP
22 \details
23 - Check if __NOP instrinsic is available
24 - No real assertion is deployed, just a compile time check.
25 */
26 void TC_CoreInstr_NOP (void) {
27   __NOP();
28   ASSERT_TRUE(1U == 1U);
29 }
30
31 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
32 /**
33 \brief Test case: TC_CoreInstr_REV
34 \details
35 - Check if __REV instrinsic swaps all bytes in a word.
36 */
37 void TC_CoreInstr_REV (void) {
38   uint32_t result = __REV(0x47110815U);
39   ASSERT_TRUE(result == 0x15081147U);
40   
41   result = __REV(0x80000000U);
42   ASSERT_TRUE(result == 0x00000080U);
43
44   result = __REV(0x00000080U);
45   ASSERT_TRUE(result == 0x80000000U);
46 }
47
48 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
49 /**
50 \brief Test case: TC_CoreInstr_REV16
51 \details
52 - Check if __REV16 instrinsic swaps the bytes in both halfwords independendly.
53 */
54 void TC_CoreInstr_REV16(void) {
55   uint32_t result = __REV16(0x47110815U);
56   ASSERT_TRUE(result == 0x11471508U); 
57 }
58
59 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
60 /**
61 \brief Test case: TC_CoreInstr_REVSH
62 \details
63 - Check if __REVSH instrinsic swaps bytes in a signed halfword keeping the sign.
64 */
65 void TC_CoreInstr_REVSH(void) {
66   int16_t result = __REVSH(0x4711);
67   ASSERT_TRUE(result == 0x1147);
68
69   result = __REVSH((int16_t)0x8000);
70   ASSERT_TRUE(result == 0x0080);
71
72   result = __REVSH(0x0080);
73   ASSERT_TRUE(result == (int16_t)0x8000);
74 }
75
76 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
77 /**
78 \brief Test case: TC_CoreInstr_ROT
79 \details
80 - Check if __ROR instrinsic moves all bits as expected.
81 */
82 void TC_CoreInstr_ROR(void) {
83   uint32_t result = __ROR(0x01U, 1U);
84   ASSERT_TRUE(result == 0x80000000U);
85
86   result = __ROR(0x80000000U, 1U);
87   ASSERT_TRUE(result == 0x40000000U);
88
89   result = __ROR(0x40000000U, 30U);
90   ASSERT_TRUE(result == 0x00000001U);
91
92   result = __ROR(0x01U, 32U);
93   ASSERT_TRUE(result == 0x00000001U);
94
95   result = __ROR(0x08154711U, 8U);
96   ASSERT_TRUE(result == 0x11081547U);
97 }
98
99 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
100 /**
101 \brief Test case: TC_CoreInstr_RBIT
102 \details
103 - Check if __RBIT instrinsic revserses the bit order of arbitrary words.
104 */
105 void TC_CoreInstr_RBIT (void) {
106   uint32_t result = __RBIT(0xAAAAAAAAU);
107   ASSERT_TRUE(result == 0x55555555U);
108
109   result = __RBIT(0x55555555U);
110   ASSERT_TRUE(result == 0xAAAAAAAAU);
111   
112   result = __RBIT(0x00000001U);
113   ASSERT_TRUE(result == 0x80000000U);
114   
115   result = __RBIT(0x80000000U); 
116   ASSERT_TRUE(result == 0x00000001U);
117   
118   result = __RBIT(0xDEADBEEFU); 
119   ASSERT_TRUE(result == 0xF77DB57BU);
120 }
121
122 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
123 /**
124 \brief Test case: TC_CoreInstr_CLZ
125 \details
126 - Check if __CLZ instrinsic counts leading zeros.
127 */
128
129 void TC_CoreInstr_CLZ (void) {
130   uint32_t result = __CLZ(0x00U);
131   ASSERT_TRUE(result == 32);
132
133   result = __CLZ(0x00000001U);
134   ASSERT_TRUE(result == 31);
135
136   result = __CLZ(0x40000000U);
137   ASSERT_TRUE(result == 1);
138
139   result = __CLZ(0x80000000U);
140   ASSERT_TRUE(result == 0);
141
142   result = __CLZ(0xFFFFFFFFU);
143   ASSERT_TRUE(result == 0);
144
145   result = __CLZ(0x80000001U);
146   ASSERT_TRUE(result == 0);
147 }
148
149 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
150 /**
151 \brief Test case: TC_CoreInstr_SSAT
152 \details
153 - Check if __SSAT instrinsic saturates signed integer values.
154 */
155 void TC_CoreInstr_SSAT (void) {
156   int32_t result = __SSAT(INT32_MAX, 32U);
157   ASSERT_TRUE(result == INT32_MAX);
158
159   result = __SSAT(INT32_MAX, 16U);
160   ASSERT_TRUE(result == INT16_MAX);
161
162   result = __SSAT(INT32_MAX, 8U);
163   ASSERT_TRUE(result == INT8_MAX);
164  
165   result = __SSAT(INT32_MAX, 1U);
166   ASSERT_TRUE(result == 0);
167
168   result = __SSAT(INT32_MIN, 32U);
169   ASSERT_TRUE(result == INT32_MIN);
170
171   result = __SSAT(INT32_MIN, 16U);
172   ASSERT_TRUE(result == INT16_MIN);
173
174   result = __SSAT(INT32_MIN, 8U);
175   ASSERT_TRUE(result == INT8_MIN);
176
177   result = __SSAT(INT32_MIN, 1U);
178   ASSERT_TRUE(result == -1);
179 }
180
181 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
182 /**
183 \brief Test case: TC_CoreInstr_USAT
184 \details
185 - Check if __USAT instrinsic saturates unsigned integer values.
186 */
187 void TC_CoreInstr_USAT (void) {
188   uint32_t result = __USAT(INT32_MAX, 31U);
189   ASSERT_TRUE(result == (UINT32_MAX>>1U));
190
191   result = __USAT(INT32_MAX, 16U);
192   ASSERT_TRUE(result == UINT16_MAX);
193
194   result = __USAT(INT32_MAX, 8U);
195   ASSERT_TRUE(result == UINT8_MAX);
196
197   result = __USAT(INT32_MAX, 0U);
198   ASSERT_TRUE(result == 0U);
199
200   result = __USAT(INT32_MIN, 31U);
201   ASSERT_TRUE(result == 0U);
202
203   result = __USAT(INT32_MIN, 16U);
204   ASSERT_TRUE(result == 0U);
205
206   result = __USAT(INT32_MIN, 8U);
207   ASSERT_TRUE(result == 0U);
208
209   result = __USAT(INT32_MIN, 0U);
210   ASSERT_TRUE(result == 0U);
211 }