Skip to content

Fix up more SSE implementations for nontrapping-fp #22931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
8 changes: 5 additions & 3 deletions system/include/compat/emmintrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef __emscripten_emmintrin_h__
#define __emscripten_emmintrin_h__

#include <climits>
#ifndef __SSE2__
#error "SSE2 instruction set not enabled"
#endif
Expand Down Expand Up @@ -1008,9 +1009,10 @@ static __inline__ long long __attribute__((__always_inline__, __nodebug__))
_mm_cvtsd_si64(__m128d __a)
{
// TODO: optimize
if (isnan(__a[0]) || isinf(__a[0])) return 0x8000000000000000LL;
long long x = llrint(__a[0]);
if (x != 0xFFFFFFFF00000000ULL && (x != 0 || fabs(__a[0]) < 2.f))
double e = __a[0];
if (isnan(e) || isinf(e)) return 0x8000000000000000LL;
long long x = llrint(e);
if (x != 0xFFFFFFFF00000000ULL && (x != 0 || fabs(e) < 2.f) && e <= LLONG_MAX && e >= LLONG_MIN)
return x;
else
return 0x8000000000000000LL;
Expand Down
12 changes: 7 additions & 5 deletions system/include/compat/xmmintrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,9 @@ _mm_cvtsi32_ss(__m128 __a, int __b)

static __inline__ int __attribute__((__always_inline__, __nodebug__, DIAGNOSE_SLOW)) _mm_cvtss_si32(__m128 __a)
{
int x = lrint(((__f32x4)__a)[0]);
if (x != 0 || fabsf(((__f32x4)__a)[0]) < 2.f)
float e = ((__f32x4)__a)[0];
int x = lrint(e);
if ((x != 0 || fabsf(e)) < 2.f && !isnan(e) && e <= (float)INT_MAX && e >= INT_MIN)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these comparisons be made against x to avoid the cast here?

Is the cast needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without the explicit cast, there's a warning about the implicit cast changing the value of INT_MAX.
Although looking at the other implementations in this file, they also have similar implicit casts which also produce that warning, so maybe we should be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we can't compare against x (at least like it's written here) because an int is always <= INT_MAX

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the comparison style (e.g. whether the nan check was first or further down) was inconsistent so I made all the checks the same. I think if we want to optimize this further we should look more into using the nontrapping instructions directly but it's not clear whether that's important.

return x;
else
return (int)0x80000000;
Expand Down Expand Up @@ -627,9 +628,10 @@ _mm_cvtsi64_ss(__m128 __a, long long __b)
static __inline__ long long __attribute__((__always_inline__, __nodebug__, DIAGNOSE_SLOW))
_mm_cvtss_si64(__m128 __a)
{
if (isnan(((__f32x4)__a)[0]) || isinf(((__f32x4)__a)[0])) return 0x8000000000000000LL;
long long x = llrintf(((__f32x4)__a)[0]);
if (x != 0xFFFFFFFF00000000ULL && (x != 0 || fabsf(((__f32x4)__a)[0]) < 2.f))
float e = ((__f32x4)__a)[0];
if (isnan(e) || isinf(e)) return 0x8000000000000000LL;
long long x = llrintf(e);
if ((x != 0xFFFFFFFF00000000ULL && (x != 0 || fabsf(e) < 2.f)) && e <= (float)LLONG_MAX && e >= LLONG_MIN)
return x;
else
return 0x8000000000000000LL;
Expand Down
Loading