]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Source/CV_CoreInstr.c
CoreValidation: Fixed compiler and MISRA warnings.
[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
42 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
43 /**
44 \brief Test case: TC_CoreInstr_REV16
45 \details
46 - Check if __REV16 instrinsic swaps the bytes in a halfword.
47 */
48 void TC_CoreInstr_REV16(void) {
49   uint16_t result = __REV16(0x4711U);
50   ASSERT_TRUE(result == 0x1147U);
51
52   result = __REV16(0x4711U);
53   ASSERT_TRUE(result == 0x1147U);
54 }
55
56 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
57 /**
58 \brief Test case: TC_CoreInstr_REVSH
59 \details
60 - Check if __REVSH instrinsic swaps bytes in a signed halfword keeping the sign.
61 */
62 void TC_CoreInstr_REVSH(void) {
63   int16_t result = __REVSH(0x4711);
64   ASSERT_TRUE(result == 0x1147);
65
66   result = __REVSH(-4711);
67   ASSERT_TRUE(result == -26131);
68 }
69
70 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
71 /**
72 \brief Test case: TC_CoreInstr_ROT
73 \details
74 - Check if __ROR instrinsic moves all bits as expected.
75 */
76 void TC_CoreInstr_ROR(void) {
77   uint32_t result = __ROR(0x01U, 1U);
78   ASSERT_TRUE(result == 0x80000000U);
79
80   result = __ROR(0x80000000U, 1U);
81   ASSERT_TRUE(result == 0x40000000U);
82
83   result = __ROR(0x40000000U, 30U);
84   ASSERT_TRUE(result == 0x00000001U);
85
86   result = __ROR(0x01U, 32U);
87   ASSERT_TRUE(result == 0x00000001U);
88
89   result = __ROR(0x08154711U, 8U);
90   ASSERT_TRUE(result == 0x11081547U);
91 }
92
93 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
94 /**
95 \brief Test case: TC_CoreInstr_RBIT
96 \details
97 - Check if __RBIT instrinsic revserses the bit order of arbitrary words.
98 */
99 void TC_CoreInstr_RBIT (void) {
100   uint32_t result = __RBIT(0xAAAAAAAAU);
101   ASSERT_TRUE(result == 0x55555555U);
102
103   result = __RBIT(0x55555555U);
104   ASSERT_TRUE(result == 0xAAAAAAAAU);
105   
106   result = __RBIT(0x00000001U);
107   ASSERT_TRUE(result == 0x80000000U);
108   
109   result = __RBIT(0x80000000U); 
110   ASSERT_TRUE(result == 0x00000001U);
111   
112   result = __RBIT(0xDEADBEEFU); 
113   ASSERT_TRUE(result == 0xF77DB57BU);
114 }
115
116 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
117 /**
118 \brief Test case: TC_CoreInstr_CLZ
119 \details
120 - Check if __CLZ instrinsic counts leading zeros.
121 */
122
123 void TC_CoreInstr_CLZ (void) {
124   int32_t result = __CLZ(0x00U);
125   ASSERT_TRUE(result == 32);
126
127   result = __CLZ(0x00000001U);
128   ASSERT_TRUE(result == 31);
129
130   result = __CLZ(0x40000000U);
131   ASSERT_TRUE(result == 1);
132
133   result = __CLZ(0x80000000U);
134   ASSERT_TRUE(result == 0);
135
136   result = __CLZ(0xFFFFFFFFU);
137   ASSERT_TRUE(result == 0);
138
139   result = __CLZ(0x80000001U);
140   ASSERT_TRUE(result == 0);
141 }
142
143 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
144 /**
145 \brief Test case: TC_CoreInstr_SSAT
146 \details
147 - Check if __SSAT instrinsic saturates signed integer values.
148 */
149 void TC_CoreInstr_SSAT (void) {
150   int32_t result = __SSAT(INT32_MAX, 32U);
151   ASSERT_TRUE(result == INT32_MAX);
152
153   result = __SSAT(INT32_MAX, 16U);
154   ASSERT_TRUE(result == INT16_MAX);
155
156   result = __SSAT(INT32_MAX, 8U);
157   ASSERT_TRUE(result == INT8_MAX);
158  
159   result = __SSAT(INT32_MAX, 1U);
160   ASSERT_TRUE(result == 0);
161
162   result = __SSAT(INT32_MIN, 32U);
163   ASSERT_TRUE(result == INT32_MIN);
164
165   result = __SSAT(INT32_MIN, 16U);
166   ASSERT_TRUE(result == INT16_MIN);
167
168   result = __SSAT(INT32_MIN, 8U);
169   ASSERT_TRUE(result == INT8_MIN);
170
171   result = __SSAT(INT32_MIN, 1U);
172   ASSERT_TRUE(result == -1);
173 }
174
175 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
176 /**
177 \brief Test case: TC_CoreInstr_USAT
178 \details
179 - Check if __USAT instrinsic saturates unsigned integer values.
180 */
181 void TC_CoreInstr_USAT (void) {
182   uint32_t result = __USAT(INT32_MAX, 31U);
183   ASSERT_TRUE(result == (UINT32_MAX>>1U));
184
185   result = __USAT(INT32_MAX, 16U);
186   ASSERT_TRUE(result == UINT16_MAX);
187
188   result = __USAT(INT32_MAX, 8U);
189   ASSERT_TRUE(result == UINT8_MAX);
190
191   result = __USAT(INT32_MAX, 0U);
192   ASSERT_TRUE(result == 0U);
193
194   result = __USAT(INT32_MIN, 31U);
195   ASSERT_TRUE(result == 0U);
196
197   result = __USAT(INT32_MIN, 16U);
198   ASSERT_TRUE(result == 0U);
199
200   result = __USAT(INT32_MIN, 8U);
201   ASSERT_TRUE(result == 0U);
202
203   result = __USAT(INT32_MIN, 0U);
204   ASSERT_TRUE(result == 0U);
205 }