GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_utility_math_sqrt.c Lines: 34 34 100.0 %
Date: 2024-12-05 08:52:37 Branches: 28 28 100.0 %

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