llvm-c166/libc/test/UnitTest/RoundingModeUtils.cpp
Hoàng Minh Thiên a454a3cdaf
[libc][math] Integer-only, statically rounded implementation of expf (#209406)
Integer-only, statically rounded implementation of `expf`, using 1
single `Frac64`-based pipeline

# Accuracy

All unit, smoke, and exhaustive tests against
`LIBC_NAMESPACE::shared::expf` pass for all rounding modes.

# Code size:

> [!NOTE]
> The report below are from the`armv8m.main-none-eabi` triple.

## Before the patch

```sh
$ ls -lB libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/
total 16
-rw-r-----@ 1 davidhoang  staff  5520 Jul 24 15:59 expf.cpp.obj

$ llvm-nm --radix=d --print-size --size-sort --reverse-sort libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/expf.cpp.obj
00000000 00001560 V _ZN22__llvm_libc_24_0_0_git4math6EXP_M1E
00000000 00001024 V _ZN22__llvm_libc_24_0_0_git4math6EXP_M2E
00000000 00000628 W _ZN22__llvm_libc_24_0_0_git4math4expfEf
00000000 00000012 T expf
00000000 00000012 T _ZN22__llvm_libc_24_0_0_git4expfEf
00000000 00000004 V _ZZN22__llvm_libc_24_0_0_git6fputil7generic16fenv_is_round_upEvE1x
00000000 00000004 V _ZZN22__llvm_libc_24_0_0_git6fputil7generic15quick_get_roundEvE1x
                  U __aeabi_f2d
                  U __aeabi_dmul
                  U __aeabi_dadd
                  U __aeabi_d2f
```

## After the patch and with `LIBC_MATH_FAST`

```sh
$ ls -lB libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/
total 8
-rw-r-----@ 1 davidhoang  staff  3244 Aug 18 14:29 expf.cpp.obj

$ llvm-nm --radix=d --print-size --size-sort --reverse-sort libc/src/math/generic/CMakeFiles/libc.src.math.generic.expf.dir/expf.cpp.obj
00000000 00001916 W _ZN22__llvm_libc_24_0_0_git6shared4math15static_rounding4expfEfi
00000000 00000014 T expf
00000000 00000014 T _ZN22__llvm_libc_24_0_0_git4expfEf
```

# Latency


[bench.zip](https://github.com/user-attachments/files/31171370/bench.zip),
containing `bench.cc` with compilation instruction.

```
====================LIBC_MATH_FAST is ON====================
==========================baseline==========================
overflow [88,100]              51.67 ns/call  (19M ops/sec)
underflow [-110,-100]          55.50 ns/call  (18M ops/sec)
normal [-10,10]                32.09 ns/call  (31M ops/sec)
subnormal-result [-103,-87]    40.14 ns/call  (25M ops/sec)
near-underflow [-100,-80]      40.84 ns/call  (24M ops/sec)
======================static_rounding=======================
overflow [88,100]              142.13 ns/call  (7M ops/sec)
underflow [-110,-100]          431.57 ns/call  (2M ops/sec)
normal [-10,10]                925.12 ns/call  (1M ops/sec)
subnormal-result [-103,-87]    965.58 ns/call  (1M ops/sec)
near-underflow [-100,-80]      955.55 ns/call  (1M ops/sec)
```

```
===================LIBC_MATH_FAST is OFF====================
==========================baseline==========================
overflow [88,100]              55.99 ns/call  (18M ops/sec)
underflow [-110,-100]          58.80 ns/call  (17M ops/sec)
normal [-10,10]                32.70 ns/call  (31M ops/sec)
subnormal-result [-103,-87]    40.90 ns/call  (24M ops/sec)
near-underflow [-100,-80]      40.46 ns/call  (25M ops/sec)
======================static_rounding=======================
overflow [88,100]              141.72 ns/call  (7M ops/sec)
underflow [-110,-100]          432.78 ns/call  (2M ops/sec)
normal [-10,10]                935.92 ns/call  (1M ops/sec)
subnormal-result [-103,-87]    971.07 ns/call  (1M ops/sec)
near-underflow [-100,-80]      966.82 ns/call  (1M ops/sec)
```

Benchmarking system: MacBook Air M3, macOS 26.5.1
2026-08-23 11:34:41 -04:00

66 lines
1.7 KiB
C++

//===-- RoundingModeUtils.cpp ---------------------------------------------===//
//
// 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 "RoundingModeUtils.h"
#include "src/__support/FPUtil/FEnvImpl.h"
#include "src/__support/FPUtil/rounding_mode.h"
#include "hdr/fenv_macros.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
namespace testing {
int get_fe_rounding(RoundingMode mode) {
switch (mode) {
case RoundingMode::Upward:
return FE_UPWARD;
case RoundingMode::Downward:
return FE_DOWNWARD;
case RoundingMode::TowardZero:
return FE_TOWARDZERO;
case RoundingMode::Nearest:
return FE_TONEAREST;
}
return -1;
}
ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
old_rounding_mode = FE_TONEAREST;
rounding_mode = FE_TONEAREST;
success = (mode == RoundingMode::Nearest);
#else
old_rounding_mode = quick_get_round();
rounding_mode = get_fe_rounding(mode);
if (old_rounding_mode != rounding_mode) {
if (rounding_mode == -1) {
success = false;
} else {
int status = set_round(rounding_mode);
success = (status == 0);
}
} else {
success = true;
}
#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
}
ForceRoundingMode::~ForceRoundingMode() {
#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
if (old_rounding_mode != rounding_mode)
set_round(old_rounding_mode);
#endif
}
} // namespace testing
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL