Bug 194263 - clampTo(): do not convert the input to double when dealing with integers
Summary: clampTo(): do not convert the input to double when dealing with integers
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: Benjamin Poulain
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-02-04 17:51 PST by Benjamin Poulain
Modified: 2019-02-08 15:12 PST (History)
4 users (show)

See Also:


Attachments
Patch (20.75 KB, patch)
2019-02-04 17:58 PST, Benjamin Poulain
no flags Details | Formatted Diff | Diff
Patch (20.93 KB, patch)
2019-02-05 16:06 PST, Benjamin Poulain
no flags Details | Formatted Diff | Diff
Patch (21.04 KB, patch)
2019-02-05 20:48 PST, Benjamin Poulain
no flags Details | Formatted Diff | Diff
Patch (28.46 KB, patch)
2019-02-07 14:27 PST, Benjamin Poulain
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews121 for ios-simulator-wk2 (2.51 MB, application/zip)
2019-02-07 17:11 PST, EWS Watchlist
no flags Details
Patch (28.46 KB, patch)
2019-02-07 21:18 PST, Benjamin Poulain
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Poulain 2019-02-04 17:51:49 PST
clampTo(): do not convert the input to double when dealing with integers
Comment 1 Benjamin Poulain 2019-02-04 17:58:14 PST
Created attachment 361143 [details]
Patch
Comment 2 Benjamin Poulain 2019-02-04 17:58:16 PST
<rdar://problem/47692312>
Comment 3 Benjamin Poulain 2019-02-04 21:16:18 PST
I'll have to specialize the test for windows.

Visual Studio does not like comparison with Infinity: https://stackoverflow.com/questions/25667254/why-do-i-get-warning-c4756-overflow-in-constant-arithmetic-when-returning-float
Comment 4 Benjamin Poulain 2019-02-05 16:06:41 PST
Created attachment 361236 [details]
Patch
Comment 5 Benjamin Poulain 2019-02-05 20:48:49 PST
Created attachment 361273 [details]
Patch
Comment 6 Darin Adler 2019-02-05 21:39:01 PST
Comment on attachment 361273 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=361273&action=review

Don’t we also want to test clamping smaller sizes to larger ones to be sure it does nothing? I also think we want to cover peculiar types, such as "char".

> Source/WTF/ChangeLog:12
> +        In many case, that was very wasteful. WebKit has many uses of clampTo() with

Tiny grammar mistake or typo, should be "many cases".

> Source/WTF/ChangeLog:13
> +        the same type as input/output, or with integer types of different size/size.

Did you mean size/sign?

> Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp:29
> +#include <wtf/Compiler.h>

This is not needed. MathExtras.h includes Compiler.h.

> Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp:220
> +TEST(WTF, clampFloatingPointToFloatingPoint)

There seem to be edge cases missing. To cite one example, super-large doubles with exponents that can’t be represented as float need to clamp to the largest non-infinite float.

> Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp:234
> +    EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max())), std::numeric_limits<int32_t>::max());

I think we’d want to test that <int32_t>::max() - <FloatingPointType>::epsilon() clamps to <int32_t>::max() - 1 and other cases like that. Covering both of the edge cases; the last floating point number that does *not* clamp to max as well as the first one that does.
Comment 7 Benjamin Poulain 2019-02-05 22:22:50 PST
Thanks for the review! I'll update the tests and land this week.
Comment 8 Benjamin Poulain 2019-02-07 14:27:26 PST
Created attachment 361445 [details]
Patch
Comment 9 EWS Watchlist 2019-02-07 17:11:30 PST
Comment on attachment 361445 [details]
Patch

Attachment 361445 [details] did not pass ios-sim-ews (ios-simulator-wk2):
Output: https://webkit-queues.webkit.org/results/11072632

New failing tests:
fast/viewport/ios/device-width-viewport-after-changing-view-scale.html
Comment 10 EWS Watchlist 2019-02-07 17:11:32 PST
Created attachment 361473 [details]
Archive of layout-test-results from ews121 for ios-simulator-wk2

The attached test failures were seen while running run-webkit-tests on the ios-sim-ews.
Bot: ews121  Port: ios-simulator-wk2  Platform: Mac OS X 10.13.6
Comment 11 Benjamin Poulain 2019-02-07 21:18:29 PST
Created attachment 361492 [details]
Patch
Comment 12 WebKit Commit Bot 2019-02-08 01:46:42 PST
Comment on attachment 361492 [details]
Patch

Clearing flags on attachment: 361492

Committed r241192: <https://trac.webkit.org/changeset/241192>
Comment 13 WebKit Commit Bot 2019-02-08 01:46:43 PST
All reviewed patches have been landed.  Closing bug.
Comment 14 Darin Adler 2019-02-08 12:45:08 PST
Quite happy with the change, but still might want to go back later and add one or two more test cases. I think the interesting one is intmax-1-epsilon, not intmax-1 and not intmax-epsilon, although it’s good to cover those too.
Comment 15 Benjamin Poulain 2019-02-08 14:16:11 PST
(In reply to Darin Adler from comment #14)
> Quite happy with the change, but still might want to go back later and add
> one or two more test cases. I think the interesting one is intmax-1-epsilon,
> not intmax-1 and not intmax-epsilon, although it’s good to cover those too.

I'll add that.

Can you explain why (intmax-1-epsilon) is more interesting? I don't understand why it is different.
Comment 16 Darin Adler 2019-02-08 14:48:32 PST
(In reply to Benjamin Poulain from comment #15)
> (In reply to Darin Adler from comment #14)
> > Quite happy with the change, but still might want to go back later and add
> > one or two more test cases. I think the interesting one is intmax-1-epsilon,
> > not intmax-1 and not intmax-epsilon, although it’s good to cover those too.
> 
> I'll add that.
> 
> Can you explain why (intmax-1-epsilon) is more interesting? I don't
> understand why it is different.

I was interested in the edge cases. The last floating point number that converts to intmax-1 and the first one that converts to intmax. So I think I got it wrong, I suppose it's intmax-1 and intmax-1+epsilon that are those two.
Comment 17 Darin Adler 2019-02-08 14:49:53 PST
Or maybe it should be std::nextafter rather than epsilon that we should use to test. So instead of intmax-1+epsilon it could be written more like std::nextafter(intmax-1, intmax).
Comment 18 Benjamin Poulain 2019-02-08 15:12:45 PST
(In reply to Darin Adler from comment #17)
> Or maybe it should be std::nextafter rather than epsilon that we should use
> to test. So instead of intmax-1+epsilon it could be written more like
> std::nextafter(intmax-1, intmax).

Ok, I get it now! I'll have a look.