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
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@smasher164
Copy link
Member

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 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.

@smasher164
Copy link
Member Author

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.

@bcmills
Copy link
Contributor

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.

@ianlancetaylor
Copy link
Contributor

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

@smasher164 smasher164 changed the title proposal: add package internal/float add package internal/float Jan 6, 2021
@smasher164 smasher164 modified the milestones: Proposal, Backlog Jan 7, 2021
@cagedmantis cagedmantis changed the title add package internal/float internal: add package internal/float Jan 11, 2021
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 11, 2021
@gopherbot
Copy link

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

@smasher164
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants