Skip to content

Commit 713ea0a

Browse files
Learn Build Service GitHub AppLearn Build Service GitHub App
authored andcommitted
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents 83fc35b + 791f0b4 commit 713ea0a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

docs/standard-library/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,8 @@ items:
16501650
href: iterators.md
16511651
- name: Algorithms
16521652
href: algorithms.md
1653+
- name: Vectorized STL Algorithms
1654+
href: vectorized-stl-algorithms.md
16531655
- name: Allocators
16541656
href: allocators.md
16551657
- name: Function objects in the C++ Standard Library
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: "Vectorized MSVC STL Algorithms"
3+
description: "Learn more about: Vectorized STL Algorithms"
4+
ms.date: 10/03/2025
5+
f1_keywords: ["_USE_STD_VECTOR_ALGORITHMS", "_USE_STD_VECTOR_FLOATING_ALGORITHMS"]
6+
helpviewer_keywords: ["_USE_STD_VECTOR_ALGORITHMS", "_USE_STD_VECTOR_FLOATING_ALGORITHMS", "Vector Algorithms", "Vectorization", "SIMD"]
7+
---
8+
# Vectorized MSVC STL Algorithms
9+
10+
Under specific conditions, algorithms in the MSVC Standard Template Library (STL) can process multiple elements simultaneously on a single CPU core, rather than handling each element individually. This optimization uses single instruction, multiple data (SIMD) instructions provided by the CPU, a technique called vectorization. When this optimization isn't applied, the implementation is referred to as scalar.
11+
12+
The conditions required for vectorization are:
13+
- The container or range must be contiguous. Examples include `array`, `vector`, and `basic_string`. Types like `span` and `basic_string_view` provide contiguous ranges. Built-in arrays also form contiguous ranges. Containers like `list` and `map` aren't contiguous.
14+
- The target platform must support the necessary SIMD instructions to implement the algorithm for the element types. This is typically true for arithmetic types and simple operations.
15+
- One of these conditions must be met:
16+
- The compiler can emit vectorized machine code for an implementation written as scalar code (auto-vectorization).
17+
- The algorithm's implementation explicitly uses vectorized code (manual vectorization).
18+
19+
## Auto-vectorization in the MSVC STL
20+
21+
For more information about automatic vectorization, see [Auto-Vectorizer](../parallel/auto-parallelization-and-auto-vectorization.md#auto-vectorizer) and the discussion in that article about the [`/arch`](../build/reference/arch-minimum-cpu-architecture.md) switch. This applies to the STL implementation code the same way it applies to user code.
22+
23+
Algorithms like `transform`, `reduce`, and `accumulate` benefit heavily from auto-vectorization.
24+
25+
## Manual vectorization in the MSVC STL
26+
27+
Certain algorithms for x64 and x86 include manual vectorization. This implementation is separately compiled and relies on runtime CPU dispatch, so it applies only to suitable CPUs.
28+
29+
Manually vectorized algorithms use template metaprogramming to detect if the element type is suitable for vectorization. As a result, they're only vectorized for simple types such as standard integer types.
30+
31+
Programs either benefit in performance from manual vectorization or remain unaffected by it. Disable manual vectorization by defining `_USE_STD_VECTOR_ALGORITHMS=0` in your project. Manually vectorized algorithms are enabled by default on x64 and x86 because `_USE_STD_VECTOR_ALGORITHMS` defaults to 1 on those platforms.
32+
33+
Assign the same value to `_USE_STD_VECTOR_ALGORITHMS` for all linked translation units that use algorithms. Configure it in the project properties instead of in the source code for consistency. For more information about how to configure it, see [/D (Preprocessor Definitions)](../build/reference/d-preprocessor-definitions.md).
34+
35+
36+
The `_USE_STD_VECTOR_ALGORITHMS` macro controls the behavior of these manually vectorized algorithms:
37+
- `contains`, `contains_subrange`
38+
- `find`, `find_last`, `find_end`, `find_first_of`, `adjacent_find`
39+
- `count`
40+
- `mismatch`
41+
- `search`, `search_n`
42+
- `swap_ranges`
43+
- `replace`
44+
- `remove`, `remove_copy`
45+
- `unique`, `unique_copy`
46+
- `reverse`, `reverse_copy`
47+
- `rotate`
48+
- `is_sorted`, `is_sorted_until`
49+
- `lexicographical_compare`, `lexicographical_compare_three_way`
50+
- `max`, `min`, `minmax`
51+
- `max_element`, `min_element`, `minmax_element`
52+
53+
The `_USE_STD_VECTOR_ALGORITHMS` macro also controls the manual vectorization of:
54+
55+
- `basic_string` and `basic_string_view` members:
56+
- `find`
57+
- `rfind`
58+
- `find_first_of`, `find_first_not_of`
59+
- `find_last_of`, `find_last_not_of`
60+
- `bitset` constructors from string and `bitset::to_string`
61+
62+
## Manually vectorized algorithms for floating point types
63+
64+
Vectorization of floating-point types involves specific considerations:
65+
- Vectorization might reorder operations, which can affect the precision of floating-point results.
66+
- Floating-point types might contain `NaN` values, which don't behave transitively in comparisons.
67+
- Floating-point operations might raise exceptions.
68+
69+
The STL addresses the first two considerations safely. Only `max_element`, `min_element`, `minmax_element`, `max`, `min`, `minmax`, `is_sorted`, and `is_sorted_until` are manually vectorized. These algorithms:
70+
- Don’t compute new floating-point values. Instead, they compare existing values to ensure that differences in operation order don't impact precision.
71+
- Because these are sorting algorithms, `NaN` values aren't allowed as inputs.
72+
73+
Use `_USE_STD_VECTOR_FLOATING_ALGORITHMS` to control the use of these vectorized algorithms for floating-point types. Set it to 0 to disable vectorization. `_USE_STD_VECTOR_FLOATING_ALGORITHMS` doesn't affect anything if `_USE_STD_VECTOR_ALGORITHMS` is set to 0.
74+
75+
The `_USE_STD_VECTOR_FLOATING_ALGORITHMS` macro defaults to 0 when [`/fp:except`](../build/reference/fp-specify-floating-point-behavior.md#except) is set.
76+
77+
Assign the same value to `_USE_STD_VECTOR_FLOATING_ALGORITHMS` for all linked translation units that use algorithms. Configure it in the project properties instead of in the source code for consistency. For more information about how to configure it, see [/D (Preprocessor Definitions)](../build/reference/d-preprocessor-definitions.md).
78+
79+
## See also
80+
81+
[Auto-Vectorizer](../parallel/auto-parallelization-and-auto-vectorization.md#auto-vectorizer)

0 commit comments

Comments
 (0)