如果SSE/AVX寄存器的值是所有字节都是0或1,有没有办法有效地获得所有非零元素的索引?
例如,如果xmm值为| r0 = 0 | r1 = 1 | r2 = 0 | r3 = 1 | r4 = 0 | r5 = 1 | r6 = 0 | ... | r14 = 0 | r15 = 1 | 结果应该是(1,3,5,...,15).结果应放在另一个_m128i变量或char [16]数组中.
如果它有帮助,我们可以假设寄存器的值是所有字节都是0或某个常量非零值(不是必需的1).
我非常想知道是否有关于那个或最好是C/C++内在的指令.在任何SSE或AVX指令集中.
编辑1:
@ zx485正确地观察到原始问题不够明确.我一直在寻找任何"连续"的解决方案.
0 1 0 1 0 1 0 1...上面的示例应该导致以下任一情况:
0将是终止字节,结果可能是002 004 006 008 010 012 014 016 000 000 000 000 000 000 000
001 003 …
我有Pandas DataFrame,它看起来像follow(df_olymic).我希望列的值Type在独立列中转换(df_olympic_table)
原始数据帧
In [3]: df_olympic
Out[3]:
Country Type Num
0 USA Gold 46
1 USA Silver 37
2 USA Bronze 38
3 GB Gold 27
4 GB Silver 23
5 GB Bronze 17
6 China Gold 26
7 China Silver 18
8 China Bronze 26
9 Russia Gold 19
10 Russia Silver 18
11 Russia Bronze 19
Run Code Online (Sandbox Code Playgroud)
转换后的数据帧
In [5]: df_olympic_table
Out[5]:
Country N_Gold N_Silver N_Bronze
0 USA 46 37 38
1 GB …Run Code Online (Sandbox Code Playgroud) 我现在正在学习C++ 11内存阶模型,并想明白之间的差别memory_order_relaxed和memory_order_consume.
具体而言,我正在寻找一个简单的例子,其中一个不能代替memory_order_consume用memory_order_relaxed.
有一篇很好的文章详细阐述了一个memory_order_consume可以应用的简单但非常具有说明性的例子.下面是文字复制粘贴.
例:
atomic<int*> Guard(nullptr);
int Payload = 0;
Run Code Online (Sandbox Code Playgroud)
制片人:
Payload = 42;
Guard.store(&Payload, memory_order_release);
Run Code Online (Sandbox Code Playgroud)
消费者:
g = Guard.load(memory_order_consume);
if (g != nullptr)
p = *g;
Run Code Online (Sandbox Code Playgroud)
我的问题包括两部分:
memory_order_consume用memory_order_relaxed在上面的例子?memory_order_consume不能被替换memory_order_relaxed?我想用constexpr char[]另一个constexpr char []成员初始化成员。可以在上面C++11或者上面做吗?
#include <iostream>
struct Base {
static constexpr char ValueOne[] = "One";
static constexpr char ValueTwo[] = "Two";
};
template <typename T>
struct ValueOneHolder {
static constexpr char Value[] = T::ValueOne; // << How can one initialize this?
};
int main() {
std::cout << ValueOneHolder<Base>::Value << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 你能否解释一下我为什么要error: call of overloaded ‘func(const Test&)’ is ambiguous使用显式模板实例化?
#include <iostream>
struct Test {
};
void func(const Test &) {
std::cout << "By Reference" << std::endl;
}
void func(const Test) {
std::cout << "By Value" << std::endl;
}
template <typename... TArgs>
void wrap(TArgs... args) {
func(args...);
}
int main() {
Test t;
wrap<const Test &>(t);
return 0;
};
Run Code Online (Sandbox Code Playgroud)
编辑
模棱两可的原因是两个因素的结合.第一个是在调用中应用的简单重载规则func(args...).第二个是简单函数不能通过value和const引用重载.为了确保,一个可以调用替换wrap<const Test &>(t)用func(static_cast<const Test &>(t)).错误仍然存在.
要解决这个问题,可以使用函数模板func和值与const参考模板特化,如@lubgr提供的示例所示
谢谢大家帮我揭开这个概念的神秘面纱.
假设有一个C++类分别支持文件描述符上的只读和只写操作.
class ReadFd {
public:
ssize_t read( /* */ ) {
// read from file_descriptor_
}
protected:
int file_descriptor_;
};
class WriteFd {
public:
ssize_t write(/* */) {
// write to file_descriptor_
}
protected:
int file_descriptor_;
};
Run Code Online (Sandbox Code Playgroud)
现在假设有人想要定义一个ReadWriteFd支持读写操作的类.
我的问题是如何设计这样的读写类以避免代码重复?
我不能从两者继承ReadFd,WriteFd因为显然我只需要一个file_descriptor_.我遇到的真实情况有点复杂,但这里的概念反映了它相当接近.
1)我有两个类class A,class B它们都有一个被调用foo但有不同参数列表的方法.
class A {
public:
void foo(int a);
};
class B {
public:
void foo(int a, int b);
};
Run Code Online (Sandbox Code Playgroud)
2)此外,我有一个class Cwith template参数T,它也有一个foo方法如下
template <typename T>
class C {
public:
void foo();
private:
T t_;
int a_;
int b_;
};
Run Code Online (Sandbox Code Playgroud)
3)我想使用class A和class B作为模板参数class C.说我希望有一个方法C::foo可以像下面这样实现:
template <typename T>
void C<T>::foo()
{
if (compile time check: T has foo(int a, int …Run Code Online (Sandbox Code Playgroud) 下面的例子是退化的,因为我想了解这个概念。
假设我们想要view一个数组的 1 个元素。
我的问题是如何使它与const和non-const对象一起工作。
我知道为什么下面代码中的第二个块无法编译,但我不知道如何组织代码以服务于这两种情况。
#include <cassert>
#include <array>
template <typename T>
class View {
private:
const std::size_t index_;
T &t_;
using value_type = typename T::value_type;
public:
View(T &t, std::size_t index) : t_{t}, index_{index} {}
const value_type &value() const { return t_[index_]; }
value_type &value() { return t_[index_]; }
};
int main() {
using Array = std::array<int, 2>;
// The block below works
{
Array array{0, 0};
View<Array> view(array, 0);
view.value() = 5; …Run Code Online (Sandbox Code Playgroud) 我有一个带有整数模板参数的函数模板。我想仅提供特定整数的实现。尝试将函数模板与另一个参数一起使用应该会导致编译错误。
我static_assert以下面介绍的方式使用。
#include <type_traits>
#include <iostream>
template <typename T>
struct false_type : public std::false_type {};
template <int T>
void function() {
static_assert(false_type<decltype(T)>::value, "Error");
};
template <>
void function<1>() {
std::cout << 1 << std::endl;
}
int main() {
function<1>();
}
Run Code Online (Sandbox Code Playgroud)
该代码运行良好,直到gcc 9.1它给出一个error: static assertion failed.
我想知道是否有一种技术可以实现我的目标并且与 兼容gcc 9.1?
动机:有一个函数可以接受 lambda 或值(为简单起见,可以是 或const char *)std::string,如下所示
template <typename LambdaOrValue>
void Function(LambdaOrValue &&lambda_or_value) {
// The idea here is to have sort of a magic that
// evaluates a lambda if an argument is actually
// a lambda or do nothing otherwise
if (Evaluate(std::forward<LabmdaOrValue>(lambda_or_value)) ==
std::string("pattern"))
// Do something
}
Run Code Online (Sandbox Code Playgroud)
我正在努力正确实现一个功能Evaluate(),即使下面的代码编译。特别是,在基于“值”的实现中使用哪个返回值来保留类型(例如const char *或std::string)
#include <type_traits>
#include <iostream>
template <typename T>
decltype(std::declval<T>()()) Evaluate(T &&t) {
return t();
}
template <typename T>
T …Run Code Online (Sandbox Code Playgroud) struct MyType {
const char *pointer{""};
};
struct MyTypeHolder {
MyType my_type;
MyType &GetMyType() { return my_type; }
};
int main () {
MyTypeHolder my_type_holder;
auto &my_type = my_type_holder.GetMyType();
my_type = MyType{};
// May one assert that my_type.pointer points to the empty string?
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,my_type = MyType{}类型的临时对象MyType被值初始化。因为MyType非静态成员有一个默认的成员初始值设定项,pointer所以一旦对象被值初始化,就可以断言 指向空字符串吗?