c++ - Visual Studio 无法识别 __AVX2__ 或 __AVX__
问题描述
我正在用 C++ 实现一个简单的 SIMD 包装器。为了使其跨平台,我使用 CMake 通过 Visual Studio 设置项目
我添加了 /Arch:AVX2,但 Visual Studio 无法识别__AVX2__
宏。
首先,我的 CMake。
cmake_minimum_required(VERSION 3.12.2)
set(INCLUDEDIR "include/Maths")
set(SOURCEDIR "src")
set(HEADER_FILES
${INCLUDEDIR}/export.h
${INCLUDEDIR}/SIMDWrapper/Config/SIMDConfig.h)
set(SOURCE_FILES
${SOURCEDIR}/Application.cpp)
add_library(Maths SHARED ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(Maths PUBLIC Core)
target_include_directories(Maths PUBLIC "include")
target_compile_options(Maths PRIVATE $<$<BOOL:${MSVC}>:/arch:AVX2>)
target_compile_definitions(Maths PRIVATE MATHS_EXPORT)
还有我的头文件(来自 Agner Fog 的 VectorClass instrset.h
):
#pragma once
#if (defined(_M_AMD64) || defined(_M_X64) || defined(__amd64)) && !
defined(__x86_64__)
#define __x86_64__ 1
#endif
#ifndef SIMD_INSTR_SET
#if defined (__AVX2__)
#define SIMD_INSTR_SET 8
#elif defined ( __AVX__ )
#define SIMD_INSTR_SET 7
#elif defined ( __SSE4_2__ )
#define SIMD_INSTR_SET 6
#elif defined ( __SSE4_1__ )
#define SIMD_INSTR_SET 5
#elif defined ( __SSSE3__ )
#define SIMD_INSTR_SET 4
#elif defined ( __SSE3__ )
#define SIMD_INSTR_SET 3
#elif defined ( __SSE2__ ) || defined ( __x86_64__ )
#define SIMD_INSTR_SET 2 //this is where the color has changed
#elif defined ( __SSE__ )
#define SIMD_INSTR_SET 1
#elif defined ( _M_IX86_FP )
#define SIMD_INSTR_SET _M_IX86_FP
#else
#define SIMD_INSTR_SET 0
#endif // instruction set defines
#endif // SIMD_INSTR_SET
这就是我所做的。
__x86_64__
已定义,我的 CPU 是 i5 Skylake,所以它应该支持 AVX2。
我检查了是否从项目配置属性中启用了 Advanced Vector Extensions 2 选项,并且它已启用。
是否需要从 CMake 或 Visual Studio 更改/添加某些内容以使 AVX2 宏可识别?
解决方案
有三种方法可以/arch:AVX2
为您的目标启用编译选项(如果您的编译器支持它)。
通过使用生成器表达式
target_compile_options(Maths PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/arch:AVX2>)
if
或通过在子句中设置编译选项
if(MSVC)
target_compile_options(Maths PRIVATE /arch:AVX2)
endif()
或通过add_definition
调用将定义添加到在此命令之后在脚本中创建的所有目标
if(MSVC)
add_definition(/arch:AVX2)
endif()
如果不是绝对必要的话,我会尽量避免使用生成器表达式,因为它们不会提高 CMake 脚本文件的可读性,而且有时很难做到正确。
推荐阅读
- python - Mpi4py scatter、gather 和 send-recv
- routes - 获取 404 错误以查看使用sails g 页面新生成的页面
- video - 如何使用 ffmpeg 渲染 7 分钟的视频持续时间但只播放 4 分钟然后停止但视频的总长度仍然保持 7 分钟?
- node.js - 如何以编程方式在 Node.js Mocha 测试套件中显示当前测试编号?
- c++ - 运行时数据类型多态性
- tensorflow - 向 tf predictor.from_saved_model() 提供示例,用于使用 tf hub 模块训练的估计器
- c# - 使用 X509Certificate 通过 .cer 文件对 pdf 进行数字签名时出现错误 No Private Key
- powerbi - 基于 Power BI 桌面中人员/行的所有特征/列进行筛选
- ajax - MVC:使用 Kendo().Window 加载 PartialView 时,出现错误“找不到资源”
- javascript - jQuery在单击和替换时附加值