• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_FP_REGS_H_
18 #define BERBERIS_FP_REGS_H_
19 
20 #include <cstring>
21 
22 #include "berberis/base/bit_util.h"
23 #include "berberis/intrinsics/intrinsics_float.h"
24 
25 namespace berberis {
26 
27 template <typename FloatType>
28 inline FloatType NanUnboxFPRegToFloat(uint64_t arg);
29 
30 template <>
NanUnboxFPRegToFloat(uint64_t arg)31 inline intrinsics::Float32 NanUnboxFPRegToFloat(uint64_t arg) {
32   // Apart from transfer operations (e.g. loads and stores), all other floating-point operations on
33   // narrower n-bit operations, n < FLEN, check if the input operands are correctly NaN-boxed, i.e.,
34   // all upper FLEN−n bits are 1. If so, the n least-significant bits of the input are used as the
35   // input value, otherwise the input value is treated as an n-bit canonical NaN.
36   if ((arg & 0xffff'ffff'0000'0000) != 0xffff'ffff'0000'0000) {
37     return bit_cast<intrinsics::Float32>(0x7fc00000);
38   }
39   intrinsics::Float32 result;
40   memcpy(&result, &arg, sizeof(intrinsics::Float32));
41   return result;
42 }
43 
44 template <>
NanUnboxFPRegToFloat(uint64_t arg)45 inline intrinsics::Float64 NanUnboxFPRegToFloat(uint64_t arg) {
46   return bit_cast<intrinsics::Float64>(arg);
47 }
48 
49 template <typename FloatType>
50 inline uint64_t NanBoxFloatToFPReg(FloatType arg);
51 
52 template <>
NanBoxFloatToFPReg(intrinsics::Float32 arg)53 inline uint64_t NanBoxFloatToFPReg(intrinsics::Float32 arg) {
54   return bit_cast<uint32_t>(arg) | 0xffff'ffff'0000'0000;
55 }
56 
57 template <>
58 inline uint64_t NanBoxFloatToFPReg(intrinsics::Float64 arg) {
59   return bit_cast<uint64_t>(arg);
60 }
61 
62 }  // namespace berberis
63 
64 #endif  // BERBERIS_FP_REGS_H_
65