GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_utility_math_sqrt.c Lines: 34 34 100.0 %
Date: 2026-03-06 19:21:09 Branches: 28 28 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** GUIX Component                                                        */
17
/**                                                                       */
18
/**   Utility (Utility)                                                   */
19
/**                                                                       */
20
/**************************************************************************/
21
22
#define GX_SOURCE_CODE
23
24
25
/* Include necessary system files.  */
26
27
#include "gx_api.h"
28
#include "gx_utility.h"
29
30
31
static GX_CONST GX_UBYTE sqrt_table[256] = {
32
    0x00, 0x10, 0x16, 0x1b, 0x20, 0x23, 0x27, 0x2a, 0x2d, 0x30, 0x32, 0x35, 0x37, 0x39, 0x3b,
33
    0x3d, 0x40, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4c, 0x4e, 0x50, 0x51, 0x53, 0x54, 0x56,
34
    0x57, 0x59, 0x5a, 0x5b, 0x5d, 0x5e, 0x60, 0x61, 0x62, 0x63, 0x65, 0x66, 0x67, 0x68, 0x6a,
35
    0x6b, 0x6c, 0x6d, 0x6e, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
36
    0x7b, 0x7c, 0x7d, 0x7e, 0x80, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
37
    0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x96,
38
    0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa0, 0xa1, 0xa2, 0xa3,
39
    0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa7, 0xa8, 0xa9, 0xaa, 0xaa, 0xab, 0xac, 0xad, 0xad, 0xae,
40
    0xaf, 0xb0, 0xb0, 0xb1, 0xb2, 0xb2, 0xb3, 0xb4, 0xb5, 0xb5, 0xb6, 0xb7, 0xb7, 0xb8, 0xb9,
41
    0xb9, 0xba, 0xbb, 0xbb, 0xbc, 0xbd, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0, 0xc1, 0xc1, 0xc2, 0xc3,
42
    0xc3, 0xc4, 0xc5, 0xc5, 0xc6, 0xc7, 0xc7, 0xc8, 0xc9, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcc,
43
    0xcd, 0xce, 0xce, 0xcf, 0xd0, 0xd0, 0xd1, 0xd1, 0xd2, 0xd3, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6,
44
    0xd6, 0xd7, 0xd7, 0xd8, 0xd9, 0xd9, 0xda, 0xda, 0xdb, 0xdb, 0xdc, 0xdd, 0xdd, 0xde, 0xde,
45
    0xdf, 0xe0, 0xe0, 0xe1, 0xe1, 0xe2, 0xe2, 0xe3, 0xe3, 0xe4, 0xe5, 0xe5, 0xe6, 0xe6, 0xe7,
46
    0xe7, 0xe8, 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xeb, 0xec, 0xec, 0xed, 0xed, 0xee, 0xee, 0xef,
47
    0xf0, 0xf0, 0xf1, 0xf1, 0xf2, 0xf2, 0xf3, 0xf3, 0xf4, 0xf4, 0xf5, 0xf5, 0xf6, 0xf6, 0xf7,
48
    0xf7, 0xf8, 0xf8, 0xf9, 0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfc, 0xfc, 0xfd, 0xfd, 0xfe, 0xfe,
49
    0xff
50
};
51
52
53
54
/**************************************************************************/
55
/*                                                                        */
56
/*  FUNCTION                                               RELEASE        */
57
/*                                                                        */
58
/*    _gx_utility_math_sqrt                               PORTABLE C      */
59
/*                                                           6.1          */
60
/*  AUTHOR                                                                */
61
/*                                                                        */
62
/*    Kenneth Maxwell, Microsoft Corporation                              */
63
/*                                                                        */
64
/*  DESCRIPTION                                                           */
65
/*                                                                        */
66
/*    This service computes the square root of the supplied value.        */
67
/*                                                                        */
68
/*  INPUT                                                                 */
69
/*                                                                        */
70
/*    value                                 Value to compute square       */
71
/*                                            root of                     */
72
/*                                                                        */
73
/*  OUTPUT                                                                */
74
/*                                                                        */
75
/*    square_root                           Square root of supplied value */
76
/*                                                                        */
77
/*  CALLS                                                                 */
78
/*                                                                        */
79
/*    None                                                                */
80
/*                                                                        */
81
/*  CALLED BY                                                             */
82
/*                                                                        */
83
/*    Application Code                                                    */
84
/*                                                                        */
85
/**************************************************************************/
86
87
2150420
UINT _gx_utility_math_sqrt(UINT value)
88
{
89
UINT square_root;
90
91
2150420
    if (value >= 0x7FFEA810)
92
    {
93
20949
        square_root = 0xB504;
94
    }
95
2129471
    else if (value >= 0x1000000)
96
    {
97
617466
        if (value >= 0x40000000)
98
        {
99
7177
            square_root = (UINT)sqrt_table[value >> 24] << 8;
100
        }
101
610289
        else if (value >= 0x10000000)
102
        {
103
1133
            square_root = (UINT)sqrt_table[value >> 22] << 7;
104
        }
105
609156
        else if (value >= 0x4000000)
106
        {
107
14317
            square_root = (UINT)sqrt_table[value >> 20] << 6;
108
        }
109
        else
110
        {
111
594839
            square_root = (UINT)sqrt_table[value >> 18] << 5;
112
        }
113
114
617466
        square_root = (square_root + 1 + (value / square_root)) >> 1;
115
617466
        square_root = (square_root + 1 + (value / square_root)) >> 1;
116
    }
117
1512005
    else if (value >= 0x10000)
118
    {
119
1341076
        if (value >= 0x400000)
120
        {
121
794335
            square_root = (UINT)sqrt_table[value >> 16] << 4;
122
        }
123
546741
        else if (value >= 0x100000)
124
        {
125
191925
            square_root = (UINT)sqrt_table[value >> 14] << 3;
126
        }
127
354816
        else if (value >= 0x40000)
128
        {
129
241973
            square_root = (UINT)sqrt_table[value >> 12] << 2;
130
        }
131
        else
132
        {
133
112843
            square_root = (UINT)sqrt_table[value >> 10] << 1;
134
        }
135
136
1341076
        square_root = (square_root + 1 + (value / square_root)) >> 1;
137
    }
138
    else
139
    {
140
170929
        if (value >= 0x4000)
141
        {
142
77324
            square_root = (UINT)(sqrt_table[value >> 8]) + 1;
143
        }
144
93605
        else if (value >= 0x1000)
145
        {
146
61266
            square_root = (UINT)(sqrt_table[value >> 6] >> 1) + 1;
147
        }
148
32339
        else if (value >= 0x400)
149
        {
150
29282
            square_root = (UINT)(sqrt_table[value >> 4] >> 2) + 1;
151
        }
152
3057
        else if (value >= 0x100)
153
        {
154
864
            square_root = (UINT)(sqrt_table[value >> 2] >> 3) + 1;
155
        }
156
        else
157
        {
158
2193
            square_root = sqrt_table[value] >> 4;
159
        }
160
    }
161
162
2150420
    if ((square_root * square_root) > value)
163
    {
164
1187917
        square_root--;
165
    }
166
167
2150420
    return square_root;
168
}
169