forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change introduces the Compare and Compare32 functions based on the total-ordering predicate in IEEE-754, section 5.10. In particular, * -NaN is ordered before any other value * +NaN is ordered after any other value * -0 is ordered before +0 * All other values are ordered the usual way Compare-8 0.4537n ± 1% Compare32-8 0.3752n ± 1% geomean 0.4126n Fixes golang#56491. Change-Id: I5c9c77430a2872f380688c1b0a66f2105b77d5ac Reviewed-on: https://go-review.googlesource.com/c/go/+/467515 Reviewed-by: WANG Xuerui <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Lynn Boger <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
- Loading branch information
1 parent
40ed359
commit 3348fd0
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pkg math, func Compare(float64, float64) int #56491 | ||
pkg math, func Compare32(float32, float32) int #56491 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package math | ||
|
||
func sign[I int32 | int64](a, b I) int { | ||
if a < b { | ||
return -1 | ||
} | ||
if a > b { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
// Compare compares a and b such that | ||
// -NaN is ordered before any other value, | ||
// +NaN is ordered after any other value, | ||
// and -0 is ordered before +0. | ||
// In other words, it defines a total order over floats | ||
// (according to the total-ordering predicate in IEEE-754, section 5.10). | ||
// It returns 0 if a == b, -1 if a < b, and +1 if a > b. | ||
func Compare(a, b float64) int { | ||
// Perform a bitwise comparison (a < b) by casting the float64s into an int64s. | ||
x := int64(Float64bits(a)) | ||
y := int64(Float64bits(b)) | ||
|
||
// If a and b are both negative, flip the comparison so that we check a > b. | ||
if x < 0 && y < 0 { | ||
return sign(y, x) | ||
} | ||
return sign(x, y) | ||
} | ||
|
||
// Compare32 compares a and b such that | ||
// -NaN is ordered before any other value, | ||
// +NaN is ordered after any other value, | ||
// and -0 is ordered before +0. | ||
// In other words, it defines a total order over floats | ||
// (according to the total-ordering predicate in IEEE-754, section 5.10). | ||
// It returns 0 if a == b, -1 if a < b, and +1 if a > b. | ||
func Compare32(a, b float32) int { | ||
// Perform a bitwise comparison (a < b) by casting the float32s into an int32s. | ||
x := int32(Float32bits(a)) | ||
y := int32(Float32bits(b)) | ||
|
||
// If a and b are both negative, flip the comparison so that we check a > b. | ||
if x < 0 && y < 0 { | ||
return sign(y, x) | ||
} | ||
return sign(x, y) | ||
} |