1 // Copyright 2015, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include <cstdio>
28 
29 #include "utils-vixl.h"
30 
31 namespace vixl {
32 
FloatToRawbits(float value)33 uint32_t FloatToRawbits(float value) {
34   uint32_t bits = 0;
35   memcpy(&bits, &value, 4);
36   return bits;
37 }
38 
39 
DoubleToRawbits(double value)40 uint64_t DoubleToRawbits(double value) {
41   uint64_t bits = 0;
42   memcpy(&bits, &value, 8);
43   return bits;
44 }
45 
46 
RawbitsToFloat(uint32_t bits)47 float RawbitsToFloat(uint32_t bits) {
48   float value = 0.0;
49   memcpy(&value, &bits, 4);
50   return value;
51 }
52 
53 
RawbitsToDouble(uint64_t bits)54 double RawbitsToDouble(uint64_t bits) {
55   double value = 0.0;
56   memcpy(&value, &bits, 8);
57   return value;
58 }
59 
60 
FloatSign(float val)61 uint32_t FloatSign(float val) {
62   uint32_t rawbits = FloatToRawbits(val);
63   return ExtractUnsignedBitfield32(31, 31, rawbits);
64 }
65 
66 
FloatExp(float val)67 uint32_t FloatExp(float val) {
68   uint32_t rawbits = FloatToRawbits(val);
69   return ExtractUnsignedBitfield32(30, 23, rawbits);
70 }
71 
72 
FloatMantissa(float val)73 uint32_t FloatMantissa(float val) {
74   uint32_t rawbits = FloatToRawbits(val);
75   return ExtractUnsignedBitfield32(22, 0, rawbits);
76 }
77 
78 
DoubleSign(double val)79 uint32_t DoubleSign(double val) {
80   uint64_t rawbits = DoubleToRawbits(val);
81   return static_cast<uint32_t>(ExtractUnsignedBitfield64(63, 63, rawbits));
82 }
83 
84 
DoubleExp(double val)85 uint32_t DoubleExp(double val) {
86   uint64_t rawbits = DoubleToRawbits(val);
87   return static_cast<uint32_t>(ExtractUnsignedBitfield64(62, 52, rawbits));
88 }
89 
90 
DoubleMantissa(double val)91 uint64_t DoubleMantissa(double val) {
92   uint64_t rawbits = DoubleToRawbits(val);
93   return ExtractUnsignedBitfield64(51, 0, rawbits);
94 }
95 
96 
FloatPack(uint32_t sign,uint32_t exp,uint32_t mantissa)97 float FloatPack(uint32_t sign, uint32_t exp, uint32_t mantissa) {
98   uint32_t bits = (sign << 31) | (exp << 23) | mantissa;
99   return RawbitsToFloat(bits);
100 }
101 
102 
DoublePack(uint64_t sign,uint64_t exp,uint64_t mantissa)103 double DoublePack(uint64_t sign, uint64_t exp, uint64_t mantissa) {
104   uint64_t bits = (sign << 63) | (exp << 52) | mantissa;
105   return RawbitsToDouble(bits);
106 }
107 
108 
Float16Classify(float16 value)109 int Float16Classify(float16 value) {
110   uint16_t exponent_max = (1 << 5) - 1;
111   uint16_t exponent_mask = exponent_max << 10;
112   uint16_t mantissa_mask = (1 << 10) - 1;
113 
114   uint16_t exponent = (value & exponent_mask) >> 10;
115   uint16_t mantissa = value & mantissa_mask;
116   if (exponent == 0) {
117     if (mantissa == 0) {
118       return FP_ZERO;
119     }
120     return FP_SUBNORMAL;
121   } else if (exponent == exponent_max) {
122     if (mantissa == 0) {
123       return FP_INFINITE;
124     }
125     return FP_NAN;
126   }
127   return FP_NORMAL;
128 }
129 
130 
CountClearHalfWords(uint64_t imm,unsigned reg_size)131 unsigned CountClearHalfWords(uint64_t imm, unsigned reg_size) {
132   VIXL_ASSERT((reg_size % 8) == 0);
133   int count = 0;
134   for (unsigned i = 0; i < (reg_size / 16); i++) {
135     if ((imm & 0xffff) == 0) {
136       count++;
137     }
138     imm >>= 16;
139   }
140   return count;
141 }
142 
143 
BitCount(uint64_t value)144 int BitCount(uint64_t value) { return CountSetBits(value); }
145 
146 
BitCount(Uint32 value)147 Int64 BitCount(Uint32 value) { return CountSetBits(value.Get()); }
148 
149 
150 }  // namespace vixl
151