Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbSum.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
18  File name      : _pbSum.cpp
19  System name    :
20  Subsystem name : System common functions
21  Title          : System APIs Asynchronous Data HDD Backup Processing
22  --------------------------------------------------------------------------------------
23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25 #include <vehicle_service/positioning_base_library.h>
26
27 /*
28  Function prototype declarations
29 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
30
31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
32  * MODULE    : Sum()
33  * ABSTRACT  : SUM calculation process
34  * NOTE      : Calculate the SUM value for the specified size from the SUM calculation start address.
35  * ARGUMENT  : void     *p        SUM calculation start address
36  *             int32    size      Size (bytes)
37  *             u_int8   type      Calculation width    Byte        :sizeof(u_char)
38  *                                        Word        :sizeof(u_int16)
39  *                                        Long-word:sizeof(u_int32)
40  * NOTE         : Calculate the SUM value from the SUM calculation start address by the size specified by the calculation width.
41  * RETURN    : u_int32            SUM value
42  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43 u_int32 Sum(void *p, int32 size, u_int8 type) {  // LCOV_EXCL_START 8:dead code
44     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
45     u_int32        sum = 0;                    /* SUM value            */
46     u_int32        loop_cnt;                    /* Loop counter        */
47     u_int32        loop_num;                    /* Number of loops            */
48
49     /* Pointer size check */
50     if ((p == NULL) || (size == 0)) {
51         /* nop */
52     } else {
53         /* When the calculation width is "Long word" */
54         if (type == sizeof(int32)) {
55             u_int32 * ptr32 = reinterpret_cast<u_int32 *>(p);        /* Long word pointer    */
56
57             /* Calculate the number of processes and loop for the number of Times to compute the Sum value. */
58             loop_num = static_cast<u_int32>(size / sizeof(u_int32));
59             for (loop_cnt = 0; loop_cnt < loop_num; loop_cnt++) {
60                 sum += *ptr32;
61                 ptr32++;
62             }
63         } else if (type == sizeof(int16)) {
64             /* For "Word" */
65             u_int16 * ptr16 = reinterpret_cast<u_int16 *>(p);        /* Word pointer    */
66
67             loop_num = static_cast<u_int32>(size / sizeof(u_int16));
68             for (loop_cnt = 0; loop_cnt < loop_num; loop_cnt++) {
69                 sum += *ptr16;
70                 ptr16++;
71             }
72         } else if (type == sizeof(char)) {
73             /* For "Byte" */
74             u_char * ptr8 = reinterpret_cast<u_char *>(p);        /* Byte pointer    */
75
76             loop_num = static_cast<u_int32>(size / sizeof(u_char));
77             for (loop_cnt = 0; loop_cnt < loop_num; loop_cnt++) {
78                 sum += *ptr8;
79                 ptr8++;
80             }
81         } else {
82             /* For other than above */
83             /* No processing */
84         }
85     }
86     return sum;
87 }
88 // LCOV_EXCL_STOP
89
90 /*
91 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
92  End of File : Sum.cpp
93 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
94 */
95