Skip to content

Conversation

@braw-lee
Copy link
Contributor

Fixes #113359

@github-actions
Copy link

github-actions bot commented Nov 18, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@braw-lee braw-lee force-pushed the bitsfx_support branch 5 times, most recently from 48441ec to 3e2b7ad Compare November 20, 2024 12:55
@braw-lee braw-lee marked this pull request as ready for review November 20, 2024 13:42
@llvmbot llvmbot added the libc label Nov 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-libc

Author: None (braw-lee)

Changes

Fixes #113359


Patch is 41.19 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/116599.diff

45 Files Affected:

  • (modified) libc/config/baremetal/arm/entrypoints.txt (+12)
  • (modified) libc/config/baremetal/riscv/entrypoints.txt (+12)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+13)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+12)
  • (modified) libc/newhdrgen/yaml/stdfix.yaml (+84)
  • (modified) libc/spec/stdc_ext.td (+13)
  • (modified) libc/src/stdfix/CMakeLists.txt (+12)
  • (added) libc/src/stdfix/bitshk.cpp (+19)
  • (added) libc/src/stdfix/bitshk.h (+21)
  • (added) libc/src/stdfix/bitshr.cpp (+19)
  • (added) libc/src/stdfix/bitshr.h (+22)
  • (added) libc/src/stdfix/bitsk.cpp (+19)
  • (added) libc/src/stdfix/bitsk.h (+22)
  • (added) libc/src/stdfix/bitslk.cpp (+19)
  • (added) libc/src/stdfix/bitslk.h (+22)
  • (added) libc/src/stdfix/bitslr.cpp (+19)
  • (added) libc/src/stdfix/bitslr.h (+22)
  • (added) libc/src/stdfix/bitsr.cpp (+19)
  • (added) libc/src/stdfix/bitsr.h (+22)
  • (added) libc/src/stdfix/bitsuhk.cpp (+19)
  • (added) libc/src/stdfix/bitsuhk.h (+21)
  • (added) libc/src/stdfix/bitsuhr.cpp (+19)
  • (added) libc/src/stdfix/bitsuhr.h (+22)
  • (added) libc/src/stdfix/bitsuk.cpp (+19)
  • (added) libc/src/stdfix/bitsuk.h (+22)
  • (added) libc/src/stdfix/bitsulk.cpp (+19)
  • (added) libc/src/stdfix/bitsulk.h (+22)
  • (added) libc/src/stdfix/bitsulr.cpp (+19)
  • (added) libc/src/stdfix/bitsulr.h (+22)
  • (added) libc/src/stdfix/bitsur.cpp (+19)
  • (added) libc/src/stdfix/bitsur.h (+22)
  • (added) libc/test/src/stdfix/BitsFxTest.h (+54)
  • (modified) libc/test/src/stdfix/CMakeLists.txt (+16)
  • (added) libc/test/src/stdfix/bitshk_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitshr_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsk_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitslk_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitslr_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsr_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsuhk_test.cpp (+13)
  • (added) libc/test/src/stdfix/bitsuhr_test.cpp (+13)
  • (added) libc/test/src/stdfix/bitsuk_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsulk_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsulr_test.cpp (+12)
  • (added) libc/test/src/stdfix/bitsur_test.cpp (+12)
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index b85ae1119345dd..cc98a53a1a38c0 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -467,6 +467,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsulk
   )
 endif()
 
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 199a030ee6371e..79e0498d7cd52a 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -462,6 +462,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits   
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsulk
   )
 endif()
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5419462d4f5b3b..162ce7e31299a5 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -730,6 +730,19 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     # TODO: https://github.com/llvm/llvm-project/issues/115778
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsulk
+
   )
 endif()
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 957e28bd66cc4c..c887bfd9a677b1 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -841,6 +841,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsulk
   )
 endif()
 
diff --git a/libc/newhdrgen/yaml/stdfix.yaml b/libc/newhdrgen/yaml/stdfix.yaml
index 9787eaba45e4ed..0da7ea6ad06c91 100644
--- a/libc/newhdrgen/yaml/stdfix.yaml
+++ b/libc/newhdrgen/yaml/stdfix.yaml
@@ -147,6 +147,90 @@ functions:
     arguments:
       - type: uint_ulk_t
     guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitshr
+    standards:
+      - stdc_ext
+    return_type: int_hr_t
+    arguments:
+      - type: short fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuhr
+    standards:
+      - stdc_ext
+    return_type: uint_uhr_t
+    arguments:
+      - type: unsigned short fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsr
+    standards:
+      - stdc_ext
+    return_type: int_r_t
+    arguments:
+      - type: fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsur
+    standards:
+      - stdc_ext
+    return_type: uint_ur_t
+    arguments:
+      - type: unsigned fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitslr
+    standards:
+      - stdc_ext
+    return_type: int_lr_t
+    arguments:
+      - type: long fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsulr
+    standards:
+      - stdc_ext
+    return_type: uint_ulr_t
+    arguments:
+      - type: unsigned long fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitshk
+    standards:
+      - stdc_ext
+    return_type: int_hk_t
+    arguments:
+      - type:  short accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuhk
+    standards:
+      - stdc_ext
+    return_type: uint_uhk_t
+    arguments:
+      - type: unsigned short accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsk
+    standards:
+      - stdc_ext
+    return_type: int_k_t
+    arguments:
+      - type: accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuk
+    standards:
+      - stdc_ext
+    return_type: uint_uk_t
+    arguments:
+      - type: unsigned accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitslk
+    standards:
+      - stdc_ext
+    return_type: uint_ulr_t
+    arguments:
+      - type: long accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsulk
+    standards:
+      - stdc_ext
+    return_type: uint_ulk_t
+    arguments:
+      - type: unsigned long accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
   - name: roundhk
     standards:
       - stdc_ext
diff --git a/libc/spec/stdc_ext.td b/libc/spec/stdc_ext.td
index dee3b8bdf6feeb..144d1f7dfd75e7 100644
--- a/libc/spec/stdc_ext.td
+++ b/libc/spec/stdc_ext.td
@@ -73,6 +73,19 @@ def StdcExt : StandardSpec<"stdc_ext"> {
           GuardedFunctionSpec<"ulrbits", RetValSpec<UnsignedLongFractType>, [ArgSpec<UIntUlrT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
           GuardedFunctionSpec<"uhkbits", RetValSpec<UnsignedShortAccumType>, [ArgSpec<UIntUhkT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
           GuardedFunctionSpec<"ulkbits", RetValSpec<UnsignedLongAccumType>, [ArgSpec<UIntUlkT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+
+          GuardedFunctionSpec<"bitshr", RetValSpec<IntHrT>, [ArgSpec<ShortFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsr", RetValSpec<IntRT>, [ArgSpec<FractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitslr", RetValSpec<IntLrT>, [ArgSpec<LongFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitshk", RetValSpec<IntHkT>, [ArgSpec<ShortAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsk", RetValSpec<IntKT>, [ArgSpec<AccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitslk", RetValSpec<IntLkT>, [ArgSpec<LongAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsuhr", RetValSpec<UIntUhrT>, [ArgSpec<UnsignedShortFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsur", RetValSpec<UIntUrI>, [ArgSpec<UnsignedFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsuk", RetValSpec<UIntUkT>, [ArgSpec<UnsignedAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsulr", RetValSpec<UIntUlrT>, [ArgSpec<UnsignedLongFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsuhk", RetValSpec<UIntUhkT>, [ArgSpec<UnsignedShortAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
+          GuardedFunctionSpec<"bitsulk", RetValSpec<UIntUlkT>, [ArgSpec<UnsignedLongAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
       ]
   >;
 
diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index 238b86728188ad..2e6ecdc4937a3e 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -53,6 +53,18 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
       libc.src.__support.CPP.bit
       libc.src.__support.fixed_point.fx_bits
   )
+
+  add_entrypoint_object(
+    bits${suffix}
+    HDRS
+      bits${suffix}.h
+    SRCS
+      bits${suffix}.cpp
+    DEPENDS
+      libc.src.__support.CPP.bit
+      libc.src.__support.fixed_point.fx_bits
+  )
+
 endforeach()
 
 add_entrypoint_object(
diff --git a/libc/src/stdfix/bitshk.cpp b/libc/src/stdfix/bitshk.cpp
new file mode 100644
index 00000000000000..d152f964a476e0
--- /dev/null
+++ b/libc/src/stdfix/bitshk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitshk function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_hk_t, bitshk, (short accum x)) {
+  return cpp::bit_cast<int_hk_t, short accum>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitshk.h b/libc/src/stdfix/bitshk.h
new file mode 100644
index 00000000000000..3fa9261bb5a695
--- /dev/null
+++ b/libc/src/stdfix/bitshk.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for bitshk ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSHK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSHK_H
+
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_hk_t bitshk(short accum x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSHK_H
diff --git a/libc/src/stdfix/bitshr.cpp b/libc/src/stdfix/bitshr.cpp
new file mode 100644
index 00000000000000..36c9a1a2940c49
--- /dev/null
+++ b/libc/src/stdfix/bitshr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitshr function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitshr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_hr_t, bitshr, (short fract x)) {
+  return cpp::bit_cast<int_hr_t, short fract>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitshr.h b/libc/src/stdfix/bitshr.h
new file mode 100644
index 00000000000000..911e0d9524dda4
--- /dev/null
+++ b/libc/src/stdfix/bitshr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitshr ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSHR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSHR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_hr_t bitshr(short fract x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSHR_H
diff --git a/libc/src/stdfix/bitsk.cpp b/libc/src/stdfix/bitsk.cpp
new file mode 100644
index 00000000000000..dedfa8e66bb53d
--- /dev/null
+++ b/libc/src/stdfix/bitsk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitsk function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_k_t, bitsk, (accum x)) {
+  return cpp::bit_cast<int_k_t, accum>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsk.h b/libc/src/stdfix/bitsk.h
new file mode 100644
index 00000000000000..cd89a2cdc303f1
--- /dev/null
+++ b/libc/src/stdfix/bitsk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsk -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_k_t bitsk(accum x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSK_H
diff --git a/libc/src/stdfix/bitslk.cpp b/libc/src/stdfix/bitslk.cpp
new file mode 100644
index 00000000000000..db235f417db060
--- /dev/null
+++ b/libc/src/stdfix/bitslk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitslk function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitslk.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_lk_t, bitslk, (long accum x)) {
+  return cpp::bit_cast<int_lk_t, long accum>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitslk.h b/libc/src/stdfix/bitslk.h
new file mode 100644
index 00000000000000..3ddfdbc0d77d16
--- /dev/null
+++ b/libc/src/stdfix/bitslk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitslk ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSLK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSLK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_lk_t bitslk(long accum x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSLK_H
diff --git a/libc/src/stdfix/bitslr.cpp b/libc/src/stdfix/bitslr.cpp
new file mode 100644
index 00000000000000..6af9e36d823048
--- /dev/null
+++ b/libc/src/stdfix/bitslr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitslr function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitslr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_lr_t, bitslr, (long fract x)) {
+  return cpp::bit_cast<int_lr_t, long fract>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitslr.h b/libc/src/stdfix/bitslr.h
new file mode 100644
index 00000000000000..ec80cc87b4d8bc
--- /dev/null
+++ b/libc/src/stdfix/bitslr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitslr ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSLR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSLR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_lr_t bitslr(long fract x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSLR_H
diff --git a/libc/src/stdfix/bitsr.cpp b/libc/src/stdfix/bitsr.cpp
new file mode 100644
index 00000000000000..22a4d9653cefd3
--- /dev/null
+++ b/libc/src/stdfix/bitsr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitsr function ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsr.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int_r_t, bitsr, (fract x)) {
+  return cpp::bit_cast<int_r_t, fract>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsr.h b/libc/src/stdfix/bitsr.h
new file mode 100644
index 00000000000000..5b6587b17ea3df
--- /dev/null
+++ b/libc/src/stdfix/bitsr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsr -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h"
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_r_t bitsr(fract x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSR_H
diff --git a/libc/src/stdfix/bitsuhk.cpp b/libc/src/stdfix/bitsuhk.cpp
new file mode 100644
index 00000000000000..f6e3da16d1608c
--- /dev/null
+++ b/libc/src/stdfix/bitsuhk.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of bitsuhk function -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "include/llvm-libc-types/stdfix-types.h"
+#include "src/__support/common.h"
+#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(uint_uhk_t, bitsuhk, (short accum x)) {
+  return cpp::bit_cast<uint_uhk_t, short accum>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsuhk.h b/libc/src/stdfix/bitsuhk.h
new file mode 100644
index 00000000000000..47a7918c1736bb
--- /dev/null
+++ b/libc/src/stdfix/bitsuhk.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for bitsuhk -----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-----------------------...
[truncated]

@braw-lee braw-lee force-pushed the bitsfx_support branch 4 times, most recently from 89e07ac to 9fc9f65 Compare November 21, 2024 12:50
@braw-lee
Copy link
Contributor Author

I am getting a linker error for just one test file bitshr_test.cpp

[1250/1256] Linking CXX executable libc/test/src/stdfix/libc.test.src.stdfix.bitshr_test.__unit__.__build__                                                   
FAILED: libc/test/src/stdfix/libc.test.src.stdfix.bitshr_test.__unit__.__build__                                                                              
: && /home/ikasama/oss/llvm_related/llvm-project/build/./bin/clang++ --target=x86_64-unknown-linux-gnu -fPIC -fno-semantic-interposition -fvisibility-inlines-
hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimp
licit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading
-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fpie -ffixed-point -Wno-c99-extensions -Wno-gnu-im
aginary-constant -Wno-pedantic -O3 libc/test/src/stdfix/CMakeFiles/libc.test.src.stdfix.bitshr_test.__unit__.__build__.dir/bitshr_test.cpp.o -o libc/test/src/
stdfix/libc.test.src.stdfix.bitshr_test.__unit__.__build__  libc/src/stdfix/CMakeFiles/libc.src.stdfix.bitshr.__internal__.dir/./bitshr.cpp.o  libc/src/errno/
CMakeFiles/libc.src.errno.errno.__internal__.dir/./libc_errno.cpp.o  libc/src/__support/StringUtil/CMakeFiles/libc.src.__support.StringUtil.error_to_string.dir/./error_to_string.cpp.o  libc/src/__support/OSUtil/linux/CMakeFiles/libc.src.__support.OSUtil.linux.linux_util.dir/./exit.cpp.o  libc/src/__support/OSUtil/linux/CMakeFiles/libc.src.__support.OSUtil.linux.linux_util.dir/./fcntl.cpp.o  libc/test/UnitTest/libLibcDeathTestExecutors.unit.a  libc/test/UnitTest/libLibcTest.unit.a && :
/usr/bin/ld: libc/test/src/stdfix/CMakeFiles/libc.test.src.stdfix.bitshr_test.__unit__.__build__.dir/bitshr_test.cpp.o: in function `LlvmLibcBitshrTest_SpecialNumbers::Run()':
bitshr_test.cpp:(.text._ZN33LlvmLibcBitshrTest_SpecialNumbers3RunEv+0x48): undefined reference to `bool __llvm_libc_20_0_0_git::testing::internal::test<signed char>(__llvm_libc_20_0_0_git::testing::internal::RunContext*, __llvm_libc_20_0_0_git::testing::TestCond, signed char, signed char, char const*, char const*, __llvm_libc_20_0_0_git::testing::internal::Location)'
/usr/bin/ld: bitshr_test.cpp:(.text._ZN33LlvmLibcBitshrTest_SpecialNumbers3RunEv+0x89): undefined reference to `bool __llvm_libc_20_0_0_git::testing::internal::test<signed char>(__llvm_libc_20_0_0_git::testing::internal::RunContext*, __llvm_libc_20_0_0_git::testing::TestCond, signed char, signed char, char const*, char const*, __llvm_libc_20_0_0_git::testing::internal::Location)'
/usr/bin/ld: bitshr_test.cpp:(.text._ZN33LlvmLibcBitshrTest_SpecialNumbers3RunEv+0xca): undefined reference to `bool __llvm_libc_20_0_0_git::testing::internal::test<signed char>(__llvm_libc_20_0_0_git::testing::internal::RunContext*, __llvm_libc_20_0_0_git::testing::TestCond, signed char, signed char, char const*, char const*, __llvm_libc_20_0_0_git::testing::internal::Location)'
/usr/bin/ld: bitshr_test.cpp:(.text._ZN33LlvmLibcBitshrTest_SpecialNumbers3RunEv+0x10b): undefined reference to `bool __llvm_libc_20_0_0_git::testing::internal::test<signed char>(__llvm_libc_20_0_0_git::testing::internal::RunContext*, __llvm_libc_20_0_0_git::testing::TestCond, signed char, signed char, char const*, char const*, __llvm_libc_20_0_0_git::testing::internal::Location)'
/usr/bin/ld: libc/test/src/stdfix/libc.test.src.stdfix.bitshr_test.__unit__.__build__: hidden symbol `_ZN22__llvm_libc_20_0_0_git7testing8internal4testIaEEbPNS1_10RunContextENS0_8TestCondET_S6_PKcS8_NS1_8LocationE' isn't defined
/usr/bin/ld: final link failed: bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[1251/1256] Running unit test libc.test.src.math.smoke.nanf_test.__unit__

the problematic test file is similar to other test files (which are working fine)
can anyone help me out here?

@overmighty
Copy link
Member

overmighty commented Nov 22, 2024

We seemingly don't have an explicit template instantiation of testing::internal::test for signed char:

TEST_SPECIALIZATION(char);
.

int_hr_t is defined as an alias for signed char:

typedef signed char int_hr_t;
.

@braw-lee
Copy link
Contributor Author

@overmighty nice find, thank you
seems like char, signed char and unsigned char are 3 distinct types,
unlike for int, where we have just int and unsigned int because signed int is equivalent to int

@braw-lee
Copy link
Contributor Author

@michaelrj-google @PiJoules @nickdesaulniers @lntue
this PR is completed, it would be nice if someone has time to review this, thank you!

@lntue lntue self-requested a review November 26, 2024 13:37
Copy link
Contributor

@PiJoules PiJoules left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far! Just a few comments:

  • Would you be able to also add some tests explicitly testing with -fpadding-on-unsigned-fixed-point? I added a comment somewhere in this PR regarding padding for unsigned types with some context. You can probably copy the cmake logic in #114318 for adding this.


namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int_hk_t, bitshk, (short accum x)) {
return cpp::bit_cast<int_hk_t, short accum>(x);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so I didn't notice this with the fxbits patch, but I think a simple bitcast won't suffice if the fixed point representation has padding bits since the padding could be any value. By default the only upstream representation for fx types doesn't use padding, but anyone use -fpadding-on-unsigned-fixed-point might be affected by this. I believe this could be addressed by just masking out the padding bits for the unsigned types after the bit_cast.

https://github.com/llvm/llvm-project/pull/114318/files#diff-aa94f9761108563a2750a9c9be57772371129073ff15c1b4e0a9ab6790a2c260R55 added the mask in FXBits which could be borrowed for this patch.

func(half));
// Occupy the bit to the left of the fixed point for Accum types
// Saturate fraction portion for Fract types
EXPECT_EQ(get_one_or_saturated_fraction(), func(one));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is me being pedantic, but could you also add some test cases for more boring, non-corner case numbers? Values like 5, 16, -10.25, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement fixed point bitsfx functions in llvm-libc

4 participants