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