1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  **********************************************************************
5  *   Copyright (C) 2002-2003, International Business Machines
6  *   Corporation and others.  All Rights Reserved.
7  **********************************************************************
8  */
9 
10 #include "layout/LETypes.h"
11 #include "LXUtilities.h"
12 
13 U_NAMESPACE_BEGIN
14 
15 //
16 // Finds the high bit by binary searching
17 // through the bits in n.
18 //
highBit(le_int32 value)19 le_int8 LXUtilities::highBit(le_int32 value)
20 {
21     if (value <= 0) {
22         return -32;
23     }
24 
25     le_int8 bit = 0;
26 
27     if (value >= 1 << 16) {
28         value >>= 16;
29         bit += 16;
30     }
31 
32     if (value >= 1 << 8) {
33         value >>= 8;
34         bit += 8;
35     }
36 
37     if (value >= 1 << 4) {
38         value >>= 4;
39         bit += 4;
40     }
41 
42     if (value >= 1 << 2) {
43         value >>= 2;
44         bit += 2;
45     }
46 
47     if (value >= 1 << 1) {
48         value >>= 1;
49         bit += 1;
50     }
51 
52     return bit;
53 }
54 
search(le_int32 value,const le_int32 array[],le_int32 count)55 le_int32 LXUtilities::search(le_int32 value, const le_int32 array[], le_int32 count)
56 {
57     le_int32 power = 1 << highBit(count);
58     le_int32 extra = count - power;
59     le_int32 probe = power;
60     le_int32 index = 0;
61 
62     if (value >= array[extra]) {
63         index = extra;
64     }
65 
66     while (probe > (1 << 0)) {
67         probe >>= 1;
68 
69         if (value >= array[index + probe]) {
70             index += probe;
71         }
72     }
73 
74     return index;
75 }
76 
reverse(le_int32 array[],le_int32 length)77 void LXUtilities::reverse(le_int32 array[], le_int32 length)
78 {
79     le_int32 front, back;
80 
81     for (front = 0, back = length - 1; front < back; front += 1, back -= 1) {
82         le_int32 swap = array[front];
83 
84         array[front] = array[back];
85         array[back]  = swap;
86     }
87 }
88 
reverse(float array[],le_int32 length)89 void LXUtilities::reverse(float array[], le_int32 length)
90 {
91     le_int32 front, back;
92 
93     for (front = 0, back = length - 1; front < back; front += 1, back -= 1) {
94         float swap = array[front];
95 
96         array[front] = array[back];
97         array[back]  = swap;
98     }
99 }
100 
101 U_NAMESPACE_END
102