Skip to content
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

internal: add package internal/float #42848

Open
smasher164 opened this issue Nov 26, 2020 · 6 comments
Open

internal: add package internal/float #42848

smasher164 opened this issue Nov 26, 2020 · 6 comments

Comments

@smasher164
Copy link
Member

@smasher164 smasher164 commented Nov 26, 2020

Currently, functions that rely on properties of a floating-point number like its sign-bit, nan-checking, and integer representation get duplicated across packages. For instance,

For instance, isFinite, isInf, abs, copysign, float64bits, and float64frombits are defined in both math and runtime.

isNaN itself is defined in math, math/bits, runtime, sort.

I propose that we move the declarations in runtime/float.go to internal/float and reuse them in both math, runtime, and anyplace else.

I assume this hasn't been done already because internal packages didn't exist at the time most of this code was written. In the case of sort, we wanted to remove its dependency on math, but if we end up adopting #33440, sort will need more access to a float64's underlying representation.

@gopherbot gopherbot added this to the Proposal milestone Nov 26, 2020
@martisch
Copy link
Contributor

@martisch martisch commented Nov 27, 2020

How would the IsNaN package then reference IsNaN in internal/float?

If using:

func IsNan(x float64) bool {
	return internalmath.IsNan()
}

This might cause additional inlining budget to be used affecting performance.

IsNaN some others are also so simple that having it defined multiple times to e.g. avoid adding multiple new dependency seems fine.

Loading

@smasher164
Copy link
Member Author

@smasher164 smasher164 commented Nov 27, 2020

This might cause additional inlining budget to be used affecting performance.

This was my concern as well. Unless there's a way to apply a peephole or a rewrite for this that bypasses the inlining budget, this would affect it.

IsNaN some others are also so simple that having it defined multiple times to e.g. avoid adding multiple new dependency seems fine.

I agree that IsNaN on its own is fine being defined multiple times. I'd be happy if just Float64bits and Float64frombits were exposed, since most of these functions can be implemented in terms of it. w.r.t #33440, we can't introduce have sort import unsafe, but all it really needs is the sign bit.

Loading

@bcmills
Copy link
Member

@bcmills bcmills commented Dec 2, 2020

This might cause additional inlining budget to be used affecting performance.

If so, we should fix the inlining heuristics. Such a trivial forwarding function should be a zero-cost abstraction.

Loading

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 6, 2021

If this doesn't affect public API, I don't think this needs to go through the proposal process.

Loading

@smasher164 smasher164 changed the title proposal: add package internal/float add package internal/float Jan 6, 2021
@smasher164 smasher164 removed this from the Proposal milestone Jan 7, 2021
@smasher164 smasher164 added this to the Backlog milestone Jan 7, 2021
@cagedmantis cagedmantis changed the title add package internal/float internal: add package internal/float Jan 11, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 19, 2021

Change https://golang.org/cl/293794 mentions this issue: internal/float: consolidate float property-checking functions

Loading

@smasher164
Copy link
Member Author

@smasher164 smasher164 commented Feb 19, 2021

I sent CL 293794 for this. I am not convinced the deduplication is worth the effort unless we know if future that #33440 will be resolved. I was slightly conservative -- there are packages that import math for these functions, where we could strip that import by having them import internal/float.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants