Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
814cd8f
Document vectorized STL algorithms
AlexGuteniev Sep 19, 2025
4272d7e
validation errors fix
AlexGuteniev Sep 19, 2025
cc385c5
Un-nest to make that work
AlexGuteniev Sep 19, 2025
86a3e29
Typo in file name
AlexGuteniev Sep 19, 2025
53ae06f
Typoes
AlexGuteniev Sep 19, 2025
a36c5a8
Spelling
AlexGuteniev Sep 20, 2025
6c10dc3
Complete the lists
AlexGuteniev Sep 20, 2025
b1600d8
Update docs/standard-library/vectorized-stl-algorithms.md
AlexGuteniev Sep 20, 2025
becc300
Review comments
AlexGuteniev Sep 20, 2025
053dac2
Update docs/standard-library/vectorized-stl-algorithms.md
AlexGuteniev Sep 22, 2025
8f4b39f
Review feedback
AlexGuteniev Sep 22, 2025
6d98a0e
STL review feedback
AlexGuteniev Sep 24, 2025
59f9977
Spelling
AlexGuteniev Sep 24, 2025
ce5dca4
Global macro
AlexGuteniev Sep 25, 2025
eff4d93
Link to documentation on how to set macro globally
AlexGuteniev Oct 1, 2025
effc396
Merge branch 'vector-algorithms' of https://github.com/AlexGuteniev/c…
TylerMSFT Oct 1, 2025
d6f6bd9
touch
TylerMSFT Oct 1, 2025
f890420
edit pass
TylerMSFT Oct 2, 2025
bc3d9cf
edits
TylerMSFT Oct 2, 2025
279a1dc
tech review
TylerMSFT Oct 3, 2025
27b7e12
edit
TylerMSFT Oct 3, 2025
70e0fe5
edit pass
TylerMSFT Oct 3, 2025
3b81dc0
last small edits
TylerMSFT Oct 3, 2025
fd1aa69
add branding
TylerMSFT Oct 3, 2025
030d999
Merge pull request #6105 from TylerMSFT/AlexGuteniev
v-dirichards Oct 3, 2025
791f0b4
Merge pull request #6109 from MicrosoftDocs/main
learn-build-service-prod[bot] Oct 4, 2025
713ea0a
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs…
Oct 4, 2025
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
2 changes: 2 additions & 0 deletions docs/standard-library/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,8 @@ items:
href: iterators.md
- name: Algorithms
href: algorithms.md
- name: Vectorized STL Algorithms
href: vectorized-stl-algorithms.md
- name: Allocators
href: allocators.md
- name: Function objects in the C++ Standard Library
Expand Down
81 changes: 81 additions & 0 deletions docs/standard-library/vectorized-stl-algorithms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: "Vectorized MSVC STL Algorithms"
description: "Learn more about: Vectorized STL Algorithms"
ms.date: 10/03/2025
f1_keywords: ["_USE_STD_VECTOR_ALGORITHMS", "_USE_STD_VECTOR_FLOATING_ALGORITHMS"]
helpviewer_keywords: ["_USE_STD_VECTOR_ALGORITHMS", "_USE_STD_VECTOR_FLOATING_ALGORITHMS", "Vector Algorithms", "Vectorization", "SIMD"]
---
# Vectorized MSVC STL Algorithms

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.

The conditions required for vectorization are:
- 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.
- 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.
- One of these conditions must be met:
- The compiler can emit vectorized machine code for an implementation written as scalar code (auto-vectorization).
- The algorithm's implementation explicitly uses vectorized code (manual vectorization).

## Auto-vectorization in the MSVC STL

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.

Algorithms like `transform`, `reduce`, and `accumulate` benefit heavily from auto-vectorization.

## Manual vectorization in the MSVC STL

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.

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.

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.

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).


The `_USE_STD_VECTOR_ALGORITHMS` macro controls the behavior of these manually vectorized algorithms:
- `contains`, `contains_subrange`
- `find`, `find_last`, `find_end`, `find_first_of`, `adjacent_find`
- `count`
- `mismatch`
- `search`, `search_n`
- `swap_ranges`
- `replace`
- `remove`, `remove_copy`
- `unique`, `unique_copy`
- `reverse`, `reverse_copy`
- `rotate`
- `is_sorted`, `is_sorted_until`
- `lexicographical_compare`, `lexicographical_compare_three_way`
- `max`, `min`, `minmax`
- `max_element`, `min_element`, `minmax_element`

The `_USE_STD_VECTOR_ALGORITHMS` macro also controls the manual vectorization of:

- `basic_string` and `basic_string_view` members:
- `find`
- `rfind`
- `find_first_of`, `find_first_not_of`
- `find_last_of`, `find_last_not_of`
- `bitset` constructors from string and `bitset::to_string`

## Manually vectorized algorithms for floating point types

Vectorization of floating-point types involves specific considerations:
- Vectorization might reorder operations, which can affect the precision of floating-point results.
- Floating-point types might contain `NaN` values, which don't behave transitively in comparisons.
- Floating-point operations might raise exceptions.

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:
- Don’t compute new floating-point values. Instead, they compare existing values to ensure that differences in operation order don't impact precision.
- Because these are sorting algorithms, `NaN` values aren't allowed as inputs.

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.

The `_USE_STD_VECTOR_FLOATING_ALGORITHMS` macro defaults to 0 when [`/fp:except`](../build/reference/fp-specify-floating-point-behavior.md#except) is set.

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).

## See also

[Auto-Vectorizer](../parallel/auto-parallelization-and-auto-vectorization.md#auto-vectorizer)