Skip to content

Commit faf903c

Browse files
committed
[libc][stdfix] Add abs functions for signed fixed point types.
1 parent 0df0e4f commit faf903c

29 files changed

+473
-1
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,18 @@ if(LIBC_COMPILER_HAS_FLOAT128)
442442
)
443443
endif()
444444

445+
if(LIBC_COMPILER_HAS_FIXED_POINT)
446+
list(APPEND TARGET_LIBM_ENTRYPOINTS
447+
# stdfix.h _Fract and _Accum entrypoints
448+
libc.src.stdfix.abshk
449+
libc.src.stdfix.abshr
450+
libc.src.stdfix.absk
451+
libc.src.stdfix.absr
452+
libc.src.stdfix.abslk
453+
libc.src.stdfix.abslr
454+
)
455+
endif()
456+
445457
if(LLVM_LIBC_FULL_BUILD)
446458
list(APPEND TARGET_LIBC_ENTRYPOINTS
447459
# assert.h entrypoints

libc/docs/math/stdfix.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Fixed-point Arithmetics
6060
| +----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
6161
| | unsigned (uhr) | signed (hr) | unsigned (ur) | signed (r) | unsigned (ulr) | signed (lr) | unsigned (uhk) | signed (hk) | unsigned (uk) | signed (k) | unsigned (ulk) | signed (lk) |
6262
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
63-
| abs | | | | | | | | | | | | |
63+
| abs | | |check| | | |check| | | |check| | | |check| | | |check| | | |check| |
6464
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
6565
| bits\* | | | | | | | | | | | | |
6666
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+

libc/spec/spec.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ def UnsignedShortType : NamedType<"unsigned short">;
5555
// TODO: Add compatibility layer to use C23 type _Float128 if possible.
5656
def Float128Type : NamedType<"float128">;
5757

58+
// Fixed point types.
59+
// From ISO/IEC TR 18037:2008 standard:
60+
// https://standards.iso.org/ittf/PubliclyAvailableStandards/c051126_ISO_IEC_TR_18037_2008.zip
61+
def ShortFractType : NamedType<"short fract">;
62+
def FractType : NamedType<"fract">;
63+
def LongFractType : NamedType<"long fract">;
64+
def UnsignedShortFractType : NamedType<"unsigned short fract">;
65+
def UnsignedFractType : NamedType<"unsigned fract">;
66+
def UnsignedLongFractType : NamedType<"unsigned long fract">;
67+
68+
def ShortAccumType : NamedType<"short accum">;
69+
def AccumType : NamedType<"accum">;
70+
def LongAccumType : NamedType<"long accum">;
71+
def UnsignedShortAccumType : NamedType<"unsigned short accum">;
72+
def UnsignedAccumType : NamedType<"unsigned accum">;
73+
def UnsignedLongAccumType : NamedType<"unsigned long accum">;
74+
5875
// Common types
5976
def VoidPtr : PtrType<VoidType>;
6077
def VoidPtrPtr : PtrType<VoidPtr>;

libc/spec/stdc_ext.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ def StdcExt : StandardSpec<"stdc_ext"> {
77
[], // types
88
[], // enums
99
[ // functions
10+
GuardedFunctionSpec<"abshr", RetValSpec<ShortFractType>, [ArgSpec<ShortFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
11+
GuardedFunctionSpec<"absr", RetValSpec<FractType>, [ArgSpec<FractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
12+
GuardedFunctionSpec<"abslr", RetValSpec<LongFractType>, [ArgSpec<LongFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
13+
14+
GuardedFunctionSpec<"abshk", RetValSpec<ShortAccumType>, [ArgSpec<ShortAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
15+
GuardedFunctionSpec<"absk", RetValSpec<AccumType>, [ArgSpec<AccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
16+
GuardedFunctionSpec<"abslk", RetValSpec<LongAccumType>, [ArgSpec<LongAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
1017
]
1118
>;
1219

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_subdirectory(fenv)
66
add_subdirectory(inttypes)
77
add_subdirectory(math)
88
add_subdirectory(stdbit)
9+
add_subdirectory(stdfix)
910
add_subdirectory(stdio)
1011
add_subdirectory(stdlib)
1112
add_subdirectory(string)

libc/src/__support/fixed_point/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,14 @@ add_header_library(
77
libc.src.__support.macros.attributes
88
libc.src.__support.CPP.type_traits
99
)
10+
11+
add_header_library(
12+
fx_bits
13+
HDRS
14+
fx_bits.h
15+
DEPENDS
16+
.fx_rep
17+
libc.include.llvm-libc-macros.stdfix_macros
18+
libc.src.__support.macros.attributes
19+
libc.src.__support.macros.optimization
20+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Utility class to manipulate fixed point numbers. --*- 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___SUPPORT_FIXEDPOINT_FXBITS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXBITS_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
#include "src/__support/macros/attributes.h" // LIBC_INLINE
14+
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
15+
16+
#include "fx_rep.h"
17+
18+
#ifdef LIBC_COMPILER_HAS_FIXED_POINT
19+
20+
namespace LIBC_NAMESPACE::fixed_point {
21+
22+
template <typename T> LIBC_INLINE constexpr T abs(T x) {
23+
using FXRep = FXRep<T>;
24+
if constexpr (FXRep::SIGN_LEN == 0)
25+
return x;
26+
else {
27+
if (LIBC_UNLIKELY(x == FXRep::MIN()))
28+
return FXRep::MAX();
29+
return (x < FXRep::ZERO() ? -x : x);
30+
}
31+
}
32+
33+
} // namespace LIBC_NAMESPACE::fixed_point
34+
35+
#endif // LIBC_COMPILER_HAS_FIXED_POINT
36+
37+
#endif // LLVM_LIBC_SRC___SUPPORT_FIXEDPOINT_FXBITS_H

libc/src/stdfix/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if(NOT LIBC_COMPILER_HAS_FIXED_POINT)
2+
return()
3+
endif()
4+
5+
foreach(suffix IN ITEMS hr r lr hk k lk)
6+
add_entrypoint_object(
7+
abs${suffix}
8+
HDRS
9+
abs${suffix}.h
10+
SRCS
11+
abs${suffix}.cpp
12+
COMPILE_OPTIONS
13+
-O3
14+
-ffixed-point
15+
DEPENDS
16+
libc.src.__support.fixed_point.fx_bits
17+
)
18+
endforeach()

libc/src/stdfix/abshk.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of abshk 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 "abshk.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(short accum, abshk, (short accum x)) {
16+
return fixed_point::abs(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/abshk.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for abshk -------------------------*- 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_ABSHK_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSHK_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
short accum abshk(short accum x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSHK_H

libc/src/stdfix/abshr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of abshr 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 "abshr.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(short fract, abshr, (short fract x)) {
16+
return fixed_point::abs(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/abshr.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for abshr -------------------------*- 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_ABSHR_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSHR_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
short fract abshr(short fract x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSHR_H

libc/src/stdfix/absk.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of absk 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 "absk.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(accum, absk, (accum x)) { return fixed_point::abs(x); }
16+
17+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/absk.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for absk --------------------------*- 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_ABSK_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSK_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
accum absk(accum x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSK_H

libc/src/stdfix/abslk.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of abslk 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 "abslk.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(long accum, abslk, (long accum x)) {
16+
return fixed_point::abs(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/abslk.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for abslk -------------------------*- 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_ABSLK_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSLK_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
long accum abslk(long accum x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSLK_H

libc/src/stdfix/abslr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of abslr 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 "abslr.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(long fract, abslr, (long fract x)) {
16+
return fixed_point::abs(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/abslr.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for abslr -------------------------*- 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_ABSLR_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSLR_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
long fract abslr(long fract x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSLR_H

libc/src/stdfix/absr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of absr 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 "absr.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/fixed_point/fx_bits.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(fract, absr, (fract x)) { return fixed_point::abs(x); }
16+
17+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/absr.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for absr --------------------------*- 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_ABSR_H
10+
#define LLVM_LIBC_SRC_STDFIX_ABSR_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
fract absr(fract x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_ABSR_H

libc/test/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_subdirectory(inttypes)
4444
add_subdirectory(math)
4545
add_subdirectory(search)
4646
add_subdirectory(stdbit)
47+
add_subdirectory(stdfix)
4748
add_subdirectory(stdio)
4849
add_subdirectory(stdlib)
4950
add_subdirectory(string)

0 commit comments

Comments
 (0)