Skip to content

[libc][stdfix] Implement fixed point fxbits functions in llvm-libc #114912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.sqrtulr
libc.src.stdfix.uhksqrtus
libc.src.stdfix.uksqrtui
libc.src.stdfix.hrbits
libc.src.stdfix.uhrbits
libc.src.stdfix.rbits
libc.src.stdfix.urbits
libc.src.stdfix.lrbits
libc.src.stdfix.ulrbits
libc.src.stdfix.hkbits
libc.src.stdfix.uhkbits
libc.src.stdfix.kbits
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
)
endif()

Expand Down
12 changes: 12 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.sqrtulr
libc.src.stdfix.uhksqrtus
libc.src.stdfix.uksqrtui
libc.src.stdfix.hrbits
libc.src.stdfix.uhrbits
libc.src.stdfix.rbits
libc.src.stdfix.urbits
libc.src.stdfix.lrbits
libc.src.stdfix.ulrbits
libc.src.stdfix.hkbits
libc.src.stdfix.uhkbits
libc.src.stdfix.kbits
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
)
endif()

Expand Down
12 changes: 12 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.sqrtulr
libc.src.stdfix.uhksqrtus
libc.src.stdfix.uksqrtui
libc.src.stdfix.hrbits
libc.src.stdfix.uhrbits
libc.src.stdfix.rbits
libc.src.stdfix.urbits
libc.src.stdfix.lrbits
libc.src.stdfix.ulrbits
libc.src.stdfix.hkbits
libc.src.stdfix.uhkbits
libc.src.stdfix.kbits
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
)
endif()

Expand Down
12 changes: 12 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
libc.src.stdfix.sqrtulr
libc.src.stdfix.uhksqrtus
libc.src.stdfix.uksqrtui
libc.src.stdfix.hrbits
libc.src.stdfix.uhrbits
libc.src.stdfix.rbits
libc.src.stdfix.urbits
libc.src.stdfix.lrbits
libc.src.stdfix.ulrbits
libc.src.stdfix.hkbits
libc.src.stdfix.uhkbits
libc.src.stdfix.kbits
libc.src.stdfix.ukbits
libc.src.stdfix.lkbits
libc.src.stdfix.ulkbits
)
endif()

Expand Down
1 change: 1 addition & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ add_header_macro(
stdfix.h
DEPENDS
.llvm-libc-macros.stdfix_macros
.llvm-libc-types.stdfix-types
)

# TODO: This should be conditional on POSIX networking being included.
Expand Down
1 change: 1 addition & 0 deletions libc/include/llvm-libc-types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ DEPENDS
)
add_header(locale_t HDR locale_t.h)
add_header(struct_lconv HDR struct_lconv.h)
add_header(stdfix-types HDR stdfix-types.h)
25 changes: 25 additions & 0 deletions libc/include/llvm-libc-types/stdfix-types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Definition of stdfix integer types --------------------------------===//
//
// 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_TYPES_STDFIX_TYPES_H
#define LLVM_LIBC_TYPES_STDFIX_TYPES_H

typedef signed char int_hr_t;
typedef signed short int int_r_t;
typedef signed int int_lr_t;
typedef signed short int_hk_t;
typedef signed int int_k_t;
typedef signed long int_lk_t;
typedef unsigned char uint_uhr_t;
typedef unsigned short int uint_ur_t;
typedef unsigned int uint_ulr_t;
typedef unsigned short int uint_uhk_t;
typedef unsigned int uint_uk_t;
typedef unsigned long uint_ulk_t;

#endif // LLVM_LIBC_TYPES_STDFIX_TYPES_H
87 changes: 86 additions & 1 deletion libc/newhdrgen/yaml/stdfix.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
header: stdfix.h
macros: []
types: []
types:
- type_name: stdfix-types
enums: []
objects: []
functions:
Expand Down Expand Up @@ -62,6 +63,90 @@ functions:
arguments:
- type: accum
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: hrbits
standards:
- stdc_ext
return_type: short fract
arguments:
- type: int_hr_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: uhrbits
standards:
- stdc_ext
return_type: unsigned short fract
arguments:
- type: uint_uhr_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: rbits
standards:
- stdc_ext
return_type: fract
arguments:
- type: int_r_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: urbits
standards:
- stdc_ext
return_type: unsigned fract
arguments:
- type: uint_ur_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: lrbits
standards:
- stdc_ext
return_type: long fract
arguments:
- type: int_lr_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: ulrbits
standards:
- stdc_ext
return_type: unsigned long fract
arguments:
- type: uint_ulr_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: hkbits
standards:
- stdc_ext
return_type: short accum
arguments:
- type: int_hk_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: uhkbits
standards:
- stdc_ext
return_type: unsigned short accum
arguments:
- type: uint_uhk_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: kbits
standards:
- stdc_ext
return_type: accum
arguments:
- type: int_k_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: ukbits
standards:
- stdc_ext
return_type: unsigned accum
arguments:
- type: uint_uk_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: lkbits
standards:
- stdc_ext
return_type: long accum
arguments:
- type: uint_ulr_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: ulkbits
standards:
- stdc_ext
return_type: unsigned long accum
arguments:
- type: uint_ulk_t
guard: LIBC_COMPILER_HAS_FIXED_POINT
- name: roundhk
standards:
- stdc_ext
Expand Down
28 changes: 27 additions & 1 deletion libc/spec/stdc_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ def UnsignedShortAccumType : NamedType<"unsigned short accum">;
def UnsignedAccumType : NamedType<"unsigned accum">;
def UnsignedLongAccumType : NamedType<"unsigned long accum">;

def IntHrT : NamedType <"int_hr_t">;
def IntRT : NamedTypes<"int_r_t">;
Copy link
Member

Choose a reason for hiding this comment

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

Buildbots are failing with:

llvm-project/libc/spec/stdc_ext.td:19:13: error: Couldn't find class 'NamedTypes'
def IntRT : NamedTypes<"int_r_t">;
            ^

Copy link
Member

Choose a reason for hiding this comment

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

s/NamedTypes/NamedType/

Copy link
Member

Choose a reason for hiding this comment

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

pushed 8366be7

def IntLrT : NamedType<"int_lr_t">;
def IntHkT : NamedType<"int_hk_t">;
def IntKT : NamedType<"int_k_t">;
def IntLkT : NamedType<"int_lk_t">;
def UIntUhrT : NamedType<"uint_uhr_t">;
def UIntUrT : NamedType<"uint_ur_t">;
def UIntUlrT : NamedType<"uint_ulr_t">;
def UIntUhkT : NamedType<"uint_uhk_t">;
def UIntUkT : NamedType<"uint_uk_t">;
def UIntUlkT : NamedType<"uint_ulk_t">;

def StdcExt : StandardSpec<"stdc_ext"> {
// From ISO/IEC TR 18037:2008 standard:
// https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip
HeaderSpec StdFix = HeaderSpec<
"stdfix.h",
[], // macros
[], // types
[IntHrT,IntRT, IntLrT, IntHkT, IntKT, IntLkT, UIntUhrT, UIntUrT, UIntUlrT, UIntUhkT, UIntUkT, UIntUlkT], // types
[], // enums
[ // functions
GuardedFunctionSpec<"abshr", RetValSpec<ShortFractType>, [ArgSpec<ShortFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
Expand All @@ -47,6 +60,19 @@ def StdcExt : StandardSpec<"stdc_ext"> {
GuardedFunctionSpec<"rounduhk", RetValSpec<UnsignedShortAccumType>, [ArgSpec<UnsignedShortAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"rounduk", RetValSpec<UnsignedAccumType>, [ArgSpec<UnsignedAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"roundulk", RetValSpec<UnsignedLongAccumType>, [ArgSpec<UnsignedLongAccumType>, ArgSpec<IntType>], "LIBC_COMPILER_HAS_FIXED_POINT">,

GuardedFunctionSpec<"hrbits", RetValSpec<ShortFractType>, [ArgSpec<IntHrT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"rbits", RetValSpec<FractType>, [ArgSpec<IntRT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"lrbits", RetValSpec<LongFractType>, [ArgSpec<IntLrT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"hkbits", RetValSpec<ShortAccumType>, [ArgSpec<IntHkT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"kbits", RetValSpec<AccumType>, [ArgSpec<IntKT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"lkbits", RetValSpec<LongAccumType>, [ArgSpec<IntLkT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"uhrbits", RetValSpec<UnsignedShortFractType>, [ArgSpec<UIntUhrT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"urbits", RetValSpec<UnsignedFractType>, [ArgSpec<UIntUrT>], "LIBC_COMPILER_HAS_FIXED_POINT">,
GuardedFunctionSpec<"ukbits", RetValSpec<UnsignedAccumType>, [ArgSpec<UIntUkT>], "LIBC_COMPILER_HAS_FIXED_POINT">
Copy link
Member

Choose a reason for hiding this comment

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

Missing a comma on the end.

Copy link
Member

Choose a reason for hiding this comment

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

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">
]
>;

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/fixed_point/fx_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "include/llvm-libc-macros/stdfix-macros.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/__support/math_extras.h"
Expand Down
11 changes: 11 additions & 0 deletions libc/src/stdfix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
DEPENDS
libc.src.__support.fixed_point.fx_bits
)

add_entrypoint_object(
${suffix}bits
HDRS
${suffix}bits.h
SRCS
${suffix}bits.cpp
DEPENDS
libc.src.__support.CPP.bit
libc.src.__support.fixed_point.fx_bits
)
endforeach()

add_entrypoint_object(
Expand Down
19 changes: 19 additions & 0 deletions libc/src/stdfix/hkbits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of hkbits 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 "hkbits.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(short accum, hkbits, (int_hk_t x)) {
return cpp::bit_cast<short accum, int_hk_t>(x);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/hkbits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for hkbits ------------------------*- 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_HKBITS_H
#define LLVM_LIBC_SRC_STDFIX_HKBITS_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 {

short accum hkbits(int_hk_t x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_HKBITS_H
19 changes: 19 additions & 0 deletions libc/src/stdfix/hrbits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of hrbits 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 "hrbits.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(short fract, hrbits, (int_hr_t x)) {
return cpp::bit_cast<short fract, int_hr_t>(x);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/stdfix/hrbits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for hrbits ------------------------*- 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_HRBITS_H
#define LLVM_LIBC_SRC_STDFIX_HRBITS_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 {

short fract hrbits(int_hr_t x);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_STDFIX_HRBITS_H
19 changes: 19 additions & 0 deletions libc/src/stdfix/kbits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of kbits 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 "kbits.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(accum, kbits, (int_k_t x)) {
return cpp::bit_cast<accum, int_k_t>(x);
}

} // namespace LIBC_NAMESPACE_DECL
Loading
Loading