Bug 179861 - [WTF] Use system endianess information instead of relying CPU information
Summary: [WTF] Use system endianess information instead of relying CPU information
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2017-11-18 08:21 PST by Yusuke Suzuki
Modified: 2017-11-19 00:08 PST (History)
15 users (show)

See Also:


Attachments
Patch (4.87 KB, patch)
2017-11-18 08:23 PST, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (4.75 KB, patch)
2017-11-18 08:32 PST, Yusuke Suzuki
jfbastien: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yusuke Suzuki 2017-11-18 08:21:55 PST
[WTF] Use system endianess information instead of relying CPU information
Comment 1 Yusuke Suzuki 2017-11-18 08:23:34 PST
Created attachment 327313 [details]
Patch
Comment 2 Yusuke Suzuki 2017-11-18 08:24:57 PST
My goal is removing bunch of minor CPUs from Platform.h.
Comment 3 Yusuke Suzuki 2017-11-18 08:32:24 PST
Created attachment 327314 [details]
Patch
Comment 4 JF Bastien 2017-11-18 10:29:06 PST
Comment on attachment 327314 [details]
Patch

Do we want something that’ll work with C++17?  http://en.cppreference.com/w/cpp/types/endian
Comment 5 Yusuke Suzuki 2017-11-18 14:27:04 PST
(In reply to JF Bastien from comment #4)
> Comment on attachment 327314 [details]
> Patch
> 
> Do we want something that’ll work with C++17? 
> http://en.cppreference.com/w/cpp/types/endian

In our current use cases, we need preprocessor defines to switch implementation by ifdefs. But C++ enum would work well if we implement things and switch by it.

template<std::endian>
void implementation();

template<>
void implementation<std::endian::little>()
{
    ...;
}

template<>
void implementation<std::endian::big>()
{
    ...;
}

And layout change is like,

template<std::endian>
struct Layout;

template<>
struct Layout<std::endian::big> {
    uint8_t f0;
    uint8_t f1;
};

template<>
struct Layout<std::endian::little> {
    uint8_t f1;
    uint8_t f0;
};

I'm not sure how to handle PDP endian (a.k.a. middle endian) in C++17.
Comment 6 JF Bastien 2017-11-18 21:01:41 PST
(In reply to Yusuke Suzuki from comment #5)
> (In reply to JF Bastien from comment #4)
> > Comment on attachment 327314 [details]
> > Patch
> > 
> > Do we want something that’ll work with C++17? 
> > http://en.cppreference.com/w/cpp/types/endian
> 
> In our current use cases, we need preprocessor defines to switch
> implementation by ifdefs. But C++ enum would work well if we implement
> things and switch by it.

Sounds like you'd rather do that separately? Sounds fine, I just figured I would ask! :)

Shame that the proposal didn't have a feature test macro so we could conditionally use it when available :(

> I'm not sure how to handle PDP endian (a.k.a. middle endian) in C++17.

Can we just static_assert that we never get such endian? I don't think WebKit supports this type of platform?

Otherwise r=me
Comment 7 Yusuke Suzuki 2017-11-18 23:43:01 PST
(In reply to JF Bastien from comment #6)
> (In reply to Yusuke Suzuki from comment #5)
> > (In reply to JF Bastien from comment #4)
> > > Comment on attachment 327314 [details]
> > > Patch
> > > 
> > > Do we want something that’ll work with C++17? 
> > > http://en.cppreference.com/w/cpp/types/endian
> > 
> > In our current use cases, we need preprocessor defines to switch
> > implementation by ifdefs. But C++ enum would work well if we implement
> > things and switch by it.
> 
> Sounds like you'd rather do that separately? Sounds fine, I just figured I
> would ask! :)
> 
> Shame that the proposal didn't have a feature test macro so we could
> conditionally use it when available :(

Yeah, I think currently using CPU(LITTLE_ENDIAN) / CPU(BIG_ENDIAN) is enough for WebKit. But once C++17 is everywhere, we can consider using std::endian instead in our code base.

> 
> > I'm not sure how to handle PDP endian (a.k.a. middle endian) in C++17.
> 
> Can we just static_assert that we never get such endian? I don't think
> WebKit supports this type of platform?
> 
> Otherwise r=me

Right, I think the current WebKit does not support middle endian well.
And I don't think there are any stake holders using middle endian CPUs.
I'll add `static_assert(!CPU(MIDDLE_ENDIAN), "");` to ensure this.
Comment 8 Yusuke Suzuki 2017-11-18 23:44:17 PST
(In reply to Yusuke Suzuki from comment #7)
> (In reply to JF Bastien from comment #6)
> > (In reply to Yusuke Suzuki from comment #5)
> > > (In reply to JF Bastien from comment #4)
> > > > Comment on attachment 327314 [details]
> > > > Patch
> > > > 
> > > > Do we want something that’ll work with C++17? 
> > > > http://en.cppreference.com/w/cpp/types/endian
> > > 
> > > In our current use cases, we need preprocessor defines to switch
> > > implementation by ifdefs. But C++ enum would work well if we implement
> > > things and switch by it.
> > 
> > Sounds like you'd rather do that separately? Sounds fine, I just figured I
> > would ask! :)
> > 
> > Shame that the proposal didn't have a feature test macro so we could
> > conditionally use it when available :(
> 
> Yeah, I think currently using CPU(LITTLE_ENDIAN) / CPU(BIG_ENDIAN) is enough
> for WebKit. But once C++17 is everywhere, we can consider using std::endian
> instead in our code base.
> 
> > 
> > > I'm not sure how to handle PDP endian (a.k.a. middle endian) in C++17.
> > 
> > Can we just static_assert that we never get such endian? I don't think
> > WebKit supports this type of platform?
> > 
> > Otherwise r=me
> 
> Right, I think the current WebKit does not support middle endian well.
> And I don't think there are any stake holders using middle endian CPUs.
> I'll add `static_assert(!CPU(MIDDLE_ENDIAN), "");` to ensure this.

Since it's macro, and we do not want to add any C++ things to Platform.h, I'll insert

#if !CPU(LITTLE_ENDIAN) && !CPU(BIG_ENDIAN)
#error Unsupported endian
#endif
Comment 9 Yusuke Suzuki 2017-11-19 00:07:13 PST
Committed r225032: <https://trac.webkit.org/changeset/225032>
Comment 10 Radar WebKit Bug Importer 2017-11-19 00:08:28 PST
<rdar://problem/35635902>