Skip to content

Commit fbf7cf1

Browse files
author
smallp-o-p
committed
Implement fxbits
1 parent 0f836cf commit fbf7cf1

25 files changed

+465
-11
lines changed

libc/src/__support/fixed_point/fx_bits.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "include/llvm-libc-macros/stdfix-macros.h"
1313
#include "src/__support/CPP/bit.h"
1414
#include "src/__support/CPP/type_traits.h"
15-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
15+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1616
#include "src/__support/macros/config.h"
1717
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
1818
#include "src/__support/math_extras.h"
@@ -163,12 +163,23 @@ template <typename T> LIBC_INLINE constexpr T round(T x, int n) {
163163
return bit_and((x + round_bit), rounding_mask);
164164
}
165165

166-
// u?int_fx_t --> Fixed point
166+
// (u)?int_fx_t --> Fixed point
167167
template <typename T, typename XType> LIBC_INLINE constexpr T fxbits(XType x) {
168-
// Example: rbits(0x2000) where fract has 15 fractional bits,
169-
// 0x2000 --> 0010 0000 0000 0000 --> 0.010 0000 0000 0000 = 0.25
168+
using FXRep = FXRep<T>;
169+
170+
// Shift number by FX_IBITS so the bits are in the right spot.
171+
// If the number is negative we need to make it positive, shift it and then
172+
// renegate it to get the correct value.
173+
if (cpp::is_signed_v<XType> &&
174+
((1 << (FXRep::TOTAL_LEN - 1)) & x)) {
175+
x = -x;
176+
x >>= FXRep::INTEGRAL_LEN;
177+
x = -x;
178+
} else {
179+
x >>= FXRep::INTEGRAL_LEN;
180+
}
170181

171-
return cpp::bit_cast<T, XType>(x);
182+
return cpp::bit_cast<T, XType>(x);
172183
}
173184
} // namespace fixed_point
174185
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdfix/hkbits.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation of hkbits function ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
10+
#include "hkbits.h"
11+
12+
#include "src/__support/common.h"
13+
#include "src/__support/fixed_point/fx_bits.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(short accum, hkbits, (int_hk_t x)) {
19+
return fixed_point::fxbits<short accum, int_hk_t>(x);
20+
}
21+
22+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdfix/hkbits.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for hkbits ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDFIX_HKBITS_H
10+
#define LLVM_LIBC_SRC_STDFIX_HKBITS_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
#include "src/__support/macros/config.h"
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
short accum hkbits(int_hk_t x);
17+
18+
}
19+
20+
#endif

libc/src/stdfix/hrbits.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
//===-- Implementation of hrbits function ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
19
#include "hrbits.h"
10+
211
#include "src/__support/common.h"
312
#include "src/__support/fixed_point/fx_bits.h"
413
#include "src/__support/macros/config.h"
514

615
namespace LIBC_NAMESPACE_DECL {
716

817
LLVM_LIBC_FUNCTION(short fract, hrbits, (int_hr_t x)) {
9-
return fixed_point::bits(x);
18+
return fixed_point::fxbits(x);
1019
}
1120

1221
}

libc/src/stdfix/hrbits.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
//===-- Implementation header for hrbits ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
19
#ifndef LLVM_LIBC_SRC_STDFIX_HRBITS_H
210
#define LLVM_LIBC_SRC_STDFIX_HRBITS_H
3-
11+
412
#include "include/llvm-libc-macros/stdfix-macros.h"
513
#include "src/__support/macros/config.h"
614
namespace LIBC_NAMESPACE_DECL {

libc/src/stdfix/kbits.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of kbits function ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "kbits.h"
10+
11+
#include "src/__support/common.h"
12+
#include "src/__support/fixed_point/fx_bits.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(accum, kbits, (int_k_t x)) {
18+
return fixed_point::fxbits<accum, int_k_t>(x);
19+
}
20+
21+
}

libc/src/stdfix/kbits.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for kbits --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
10+
#ifndef LLVM_LIBC_SRC_STDFIX_KBITS_H
11+
#define LLVM_LIBC_SRC_STDFIX_KBITS_H
12+
13+
#include "include/llvm-libc-macros/stdfix-macros.h"
14+
#include "src/__support/macros/config.h"
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
accum kbits(int_k_t x);
18+
19+
}
20+
21+
#endif

libc/src/stdfix/lkbits.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of lkbits function ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lkbits.h"
10+
11+
#include "src/__support/common.h"
12+
#include "src/__support/fixed_point/fx_bits.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(long accum, lkbits, (int_lk_t x)) {
18+
return fixed_point::fxbits<long accum, int_lk_t>(x);
19+
}
20+
21+
}

libc/src/stdfix/lkbits.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation header for lkbits ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDFIX_LKBITS_H
10+
#define LLVM_LIBC_SRC_STDFIX_LKBITS_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
#include "src/__support/macros/config.h"
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
long accum lkbits(int_lk_t x);
17+
18+
}
19+
#endif

libc/src/stdfix/lrbits.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of lrbits function ---------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lrbits.h"
10+
11+
#include "src/__support/common.h"
12+
#include "src/__support/fixed_point/fx_bits.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(long fract, lrbits, (int_lr_t x)) {
18+
return fixed_point::fxbits<long fract, int_lr_t>(x);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)