comparison

Test of 8 subsequent bytes isn't translated into a single compare instruction

喜你入骨 提交于 2020-08-27 22:12:26
问题 Motivated by this question, I compared three different functions for checking if 8 bytes pointed to by the argument are zeros (note that in the original question, characters are compared with '0' , not 0 ): bool f1(const char *ptr) { for (int i = 0; i < 8; i++) if (ptr[i]) return false; return true; } bool f2(const char *ptr) { bool res = true; for (int i = 0; i < 8; i++) res &= (ptr[i] == 0); return res; } bool f3(const char *ptr) { static const char tmp[8]{}; return !std::memcmp(ptr, tmp, 8

Test of 8 subsequent bytes isn't translated into a single compare instruction

久未见 提交于 2020-08-27 22:12:07
问题 Motivated by this question, I compared three different functions for checking if 8 bytes pointed to by the argument are zeros (note that in the original question, characters are compared with '0' , not 0 ): bool f1(const char *ptr) { for (int i = 0; i < 8; i++) if (ptr[i]) return false; return true; } bool f2(const char *ptr) { bool res = true; for (int i = 0; i < 8; i++) res &= (ptr[i] == 0); return res; } bool f3(const char *ptr) { static const char tmp[8]{}; return !std::memcmp(ptr, tmp, 8

Compare dates in Lua

笑着哭i 提交于 2020-08-24 08:34:32
问题 I have a variable with a date table that looks like this * table: [day] * number: 15 [year] * number: 2015 [month] * number: 2 How do I get the days between the current date and the date above? Many thanks! 回答1: You can use os.time() to convert your table to seconds and get the current time and then use os.difftime() to compute the difference. see Lua Wiki for more details. reference = os.time{day=15, year=2015, month=2} daysfrom = os.difftime(os.time(), reference) / (24 * 60 * 60) -- seconds

Compare dates in Lua

只愿长相守 提交于 2020-08-24 08:31:29
问题 I have a variable with a date table that looks like this * table: [day] * number: 15 [year] * number: 2015 [month] * number: 2 How do I get the days between the current date and the date above? Many thanks! 回答1: You can use os.time() to convert your table to seconds and get the current time and then use os.difftime() to compute the difference. see Lua Wiki for more details. reference = os.time{day=15, year=2015, month=2} daysfrom = os.difftime(os.time(), reference) / (24 * 60 * 60) -- seconds

Why can I invoke == with a defaulted <=> but not a user-provided one?

风格不统一 提交于 2020-08-19 04:27:10
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Why can I invoke == with a defaulted <=> but not a user-provided one?

☆樱花仙子☆ 提交于 2020-08-19 04:25:08
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Comparing lists containing NaNs

帅比萌擦擦* 提交于 2020-07-29 14:19:31
问题 I am trying to compare two different lists to see if they are equal, and was going to remove NaNs, only to discover that my list comparisons still work, despite NaN == NaN -> False . Could someone explain why the following evaluate True or False , as I am finding this behavior unexpected. Thanks, I have read the following which don't seem to resolve the issue: Why in numpy nan == nan is False while nan in [nan] is True? Why is NaN not equal to NaN? [duplicate] (Python 2.7.3, numpy-1.9.2) I

Comparing Julia variable to `nothing` using !== or !=

别来无恙 提交于 2020-07-23 10:51:29
问题 In some Julia code when can see conditional expression such as if val !== nothing dosomething() end where val is a variable of type Union{Int,Nothing} What is the difference between conditons val !== nothing and val != nothing ? 回答1: First of all, it is generally advisable to use isnothing to compare if something is nothing . This particular function is efficient, as it is soley based on types ( @edit isnothing(nothing) ): isnothing(::Any) = false isnothing(::Nothing) = true (Note that

Comparing Julia variable to `nothing` using !== or !=

隐身守侯 提交于 2020-07-23 10:49:30
问题 In some Julia code when can see conditional expression such as if val !== nothing dosomething() end where val is a variable of type Union{Int,Nothing} What is the difference between conditons val !== nothing and val != nothing ? 回答1: First of all, it is generally advisable to use isnothing to compare if something is nothing . This particular function is efficient, as it is soley based on types ( @edit isnothing(nothing) ): isnothing(::Any) = false isnothing(::Nothing) = true (Note that

SSE Comparison Intrinsics - How to get 1 or 0 from a comparison?

你说的曾经没有我的故事 提交于 2020-07-21 04:52:44
问题 I am trying to write the equivalent of an if statement with SSE intrinsics. I am using __m128 _mm_cmplt_ps(__m128 a, __m128 b) to do the comparison a < b, and this returns 0xffffffff or 0x0 if the comparison was respectively true or false. I would like to convert these values into 1 and 0. In order to do this, is it correct to implement the logical "and" __m128 _mm_and_ps(__m128 c , __m128 d) , where c is the result of the conversion and d is, e.g., 0xffffffff ? Thank you for your attention.