Skip to content

Commit 02d3eac

Browse files
committed
numeric: 飽和演算(#1237)
1 parent 2673b91 commit 02d3eac

File tree

7 files changed

+382
-6
lines changed

7 files changed

+382
-6
lines changed

lang/cpp26.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
155155

156156
### 数値
157157
- [`<numeric>`](/reference/numeric.md)に、飽和演算 (Saturation Arithmetic) として、型の表現可能な範囲で演算を行う以下の関数を追加
158-
- [`std::add_sat()`](/reference/numeric/add_sat.md.nolink)
159-
- [`std::sub_sat()`](/reference/numeric/sub_sat.md.nolink)
160-
- [`std::mul_sat()`](/reference/numeric/mul_sat.md.nolink)
161-
- [`std::div_sat()`](/reference/numeric/div_sat.md.nolink)
162-
- [`std::saturation_cast()`](/reference/numeric/saturation_cast.md.nolink)
158+
- [`std::add_sat()`](/reference/numeric/add_sat.md)
159+
- [`std::sub_sat()`](/reference/numeric/sub_sat.md)
160+
- [`std::mul_sat()`](/reference/numeric/mul_sat.md)
161+
- [`std::div_sat()`](/reference/numeric/div_sat.md)
162+
- [`std::saturate_cast()`](/reference/numeric/saturate_cast.md)
163163
- [`<cmath>`](/reference/cmath.md)の以下の関数を、`constexpr`に対応 (特殊関数と、グローバルの丸めモードに依存する丸め関数以外の全て)
164164
- [`std::cos()`](/reference/cmath/cos.md)
165165
- [`std::sin()`](/reference/cmath/sin.md)

reference/numeric.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# numeric
22
* numeric[meta header]
33

4-
`<numeric>` ヘッダは数値のシーケンスの処理に特化したアルゴリズムを定義する
4+
`<numeric>` ヘッダでは、数値のシーケンスや数値の処理に特化したアルゴリズムを定義する
55

66

77
## 集計
@@ -55,6 +55,17 @@
5555
| [`midpoint`](numeric/midpoint.md) | 数値とポインタの中点を求める | C++20 |
5656

5757

58+
## 飽和演算
59+
60+
| 名前 | 説明 | 対応バージョン |
61+
|------|------|-------|
62+
| [`add_sat`](numeric/add_sat.md) | 飽和加算`x + y` | C++26 |
63+
| [`sub_sat`](numeric/sub_sat.md) | 飽和減算`x - y` | C++26 |
64+
| [`mul_sat`](numeric/mul_sat.md) | 飽和乗算`x * y` | C++26 |
65+
| [`div_sat`](numeric/div_sat.md) | 飽和除算`x / y` | C++26 |
66+
| [`saturate_cast`](numeric/saturate_cast.md) | 飽和演算あり型キャスト | C++26 |
67+
68+
5869
## 関連項目
5970
- [`<algorithm>`](/reference/algorithm.md)
6071
- より汎用的なアルゴリズム

reference/numeric/add_sat.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# add_sat
2+
* numeric[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template<class T>
10+
constexpr T add_sat(T x, T y) noexcept;
11+
}
12+
```
13+
14+
## 概要
15+
飽和加算 `x + y` を計算する。
16+
17+
18+
## テンプレートパラメータ制約
19+
`T`は符号付き整数型または符号無し整数型であること。
20+
21+
22+
## 戻り値
23+
- 無限の範囲で計算した値`x + y`が型`T`で表現可能ならば、`x + y`を返す
24+
- そうでないとき、型`T`で表現可能な最大値または最小値のうち`x + y`に近い方の値を返す
25+
26+
27+
## 例外
28+
投げない
29+
30+
31+
## 例
32+
```cpp example
33+
#include <cstdint>
34+
#include <numeric>
35+
#include <print>
36+
37+
int main()
38+
{
39+
// 1 + 2 = 3
40+
std::println("{}", std::add_sat(1, 2));
41+
42+
// 200 + 200 = 400 -> 255(2**8-1)
43+
std::uint8_t x = 200;
44+
std::println("{}", std::add_sat(x, x));
45+
46+
// -100 + -100 = -200 -> -128(-2**7)
47+
std::int8_t y = -100;
48+
std::println("{}", std::add_sat(y, y));
49+
}
50+
```
51+
* std::add_sat[color ff0000]
52+
* std::println[link /reference/print/println.md]
53+
54+
### 出力
55+
```
56+
3
57+
255
58+
-128
59+
```
60+
61+
62+
## バージョン
63+
### 言語
64+
- C++26
65+
66+
### 処理系
67+
- [Clang](/implementation.md#clang): ??
68+
- [GCC](/implementation.md#gcc): ??
69+
- [ICC](/implementation.md#icc): ??
70+
- [Visual C++](/implementation.md#visual_cpp): ??
71+
72+
73+
## 参照
74+
- [P0543R3 Saturation arithmetic](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0543r3.html)

reference/numeric/div_sat.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# div_sat
2+
* numeric[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template<class T>
10+
constexpr T div_sat(T x, T y) noexcept;
11+
}
12+
```
13+
14+
## 概要
15+
飽和除算 `x / y` を計算する。
16+
17+
18+
## テンプレートパラメータ制約
19+
`T`は符号付き整数型または符号無し整数型であること。
20+
21+
22+
## 事前条件
23+
`y != 0`
24+
25+
26+
## 戻り値
27+
- `T`が符号付き整数型かつ`x ==` [`numeric_limits<T>::min()`](/reference/limits/numeric_limits/min.md) `&& y == -1`のとき、[`numeric_limits<T>::max()`](/reference/limits/numeric_limits/max.md)を返す
28+
- そうでなければ、`x / y`を返す
29+
30+
31+
## 例外
32+
投げない
33+
34+
35+
## 定数式に評価される条件
36+
事前条件を満たすこと
37+
38+
39+
## 例
40+
```cpp example
41+
#include <cstdint>
42+
#include <numeric>
43+
#include <print>
44+
45+
int main()
46+
{
47+
// 10 / 3 = 3
48+
std::println("{}", std::div_sat(10, 3));
49+
50+
// -128 * -1 = 128 -> 127(2**7-1)
51+
std::int8_t x = -128, y = -1;
52+
std::println("{}", std::div_sat(x, y));
53+
}
54+
```
55+
* std::div_sat[color ff0000]
56+
* std::println[link /reference/print/println.md]
57+
58+
### 出力
59+
```
60+
3
61+
127
62+
```
63+
64+
65+
## バージョン
66+
### 言語
67+
- C++26
68+
69+
### 処理系
70+
- [Clang](/implementation.md#clang): ??
71+
- [GCC](/implementation.md#gcc): ??
72+
- [ICC](/implementation.md#icc): ??
73+
- [Visual C++](/implementation.md#visual_cpp): ??
74+
75+
76+
## 参照
77+
- [P0543R3 Saturation arithmetic](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0543r3.html)

reference/numeric/mul_sat.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# mul_sat
2+
* numeric[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template<class T>
10+
constexpr T mul_sat(T x, T y) noexcept;
11+
}
12+
```
13+
14+
## 概要
15+
飽和乗算 `x * y` を計算する。
16+
17+
18+
## テンプレートパラメータ制約
19+
`T`は符号付き整数型または符号無し整数型であること。
20+
21+
22+
## 戻り値
23+
- 無限の範囲で計算した値`x * y`が型`T`で表現可能ならば、`x * y`を返す
24+
- そうでないとき、型`T`で表現可能な最大値または最小値のうち`x * y`に近い方の値を返す
25+
26+
27+
## 例外
28+
投げない
29+
30+
31+
## 例
32+
```cpp example
33+
#include <cstdint>
34+
#include <numeric>
35+
#include <print>
36+
37+
int main()
38+
{
39+
// 2 * 3 = 6
40+
std::println("{}", std::mul_sat(2, 3));
41+
42+
// 20 * 20 = 400 -> 255(2**8-1)
43+
std::uint8_t n = 20;
44+
std::println("{}", std::mul_sat(n, n));
45+
46+
// -128 * -1 = 128 -> 127(2**7-1)
47+
std::int8_t x = -128, y = -1;
48+
std::println("{}", std::mul_sat(x, y));
49+
}
50+
```
51+
* std::mul_sat[color ff0000]
52+
* std::println[link /reference/print/println.md]
53+
54+
### 出力
55+
```
56+
6
57+
255
58+
127
59+
```
60+
61+
62+
## バージョン
63+
### 言語
64+
- C++26
65+
66+
### 処理系
67+
- [Clang](/implementation.md#clang): ??
68+
- [GCC](/implementation.md#gcc): ??
69+
- [ICC](/implementation.md#icc): ??
70+
- [Visual C++](/implementation.md#visual_cpp): ??
71+
72+
73+
## 参照
74+
- [P0543R3 Saturation arithmetic](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0543r3.html)

reference/numeric/saturate_cast.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# saturate_cast
2+
* numeric[meta header]
3+
* function template[meta id-type]
4+
* std[meta namespace]
5+
* cpp26[meta cpp]
6+
7+
```cpp
8+
namespace std {
9+
template<class R, class T>
10+
constexpr R saturate_cast(T x) noexcept;
11+
}
12+
```
13+
14+
## 概要
15+
値`x`を変換先の型`R`で表現可能な値へ丸める。
16+
17+
18+
## テンプレートパラメータ制約
19+
`T`は符号付き整数型または符号無し整数型であること。
20+
21+
22+
## 戻り値
23+
- 値`x`が型`T`で表現可能ならば、`x`を返す
24+
- そうでないとき、型`T`で表現可能な最大値または最小値のうち`x`に近い方の値を返す
25+
26+
27+
## 例外
28+
投げない
29+
30+
31+
## 例
32+
```cpp example
33+
#include <cstdint>
34+
#include <numeric>
35+
#include <print>
36+
37+
int main()
38+
{
39+
std::println("{}", std::saturate_cast<std::int8_t>( 100));
40+
std::println("{}", std::saturate_cast<std::int8_t>( 200));
41+
std::println("{}", std::saturate_cast<std::int8_t>(-200));
42+
}
43+
```
44+
* std::saturate_cast[color ff0000]
45+
* std::println[link /reference/print/println.md]
46+
47+
### 出力
48+
```
49+
100
50+
127
51+
-128
52+
```
53+
54+
55+
## バージョン
56+
### 言語
57+
- C++26
58+
59+
### 処理系
60+
- [Clang](/implementation.md#clang): ??
61+
- [GCC](/implementation.md#gcc): ??
62+
- [ICC](/implementation.md#icc): ??
63+
- [Visual C++](/implementation.md#visual_cpp): ??
64+
65+
66+
## 参照
67+
- [P0543R3 Saturation arithmetic](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p0543r3.html)

0 commit comments

Comments
 (0)