}
func trunc(x float64) float64 {
- if x == 0 || IsNaN(x) || IsInf(x, 0) {
- return x
+ if Abs(x) < 1 {
+ return Copysign(0, x)
}
- d, _ := Modf(x)
- return d
+
+ b := Float64bits(x)
+ e := uint(b>>shift)&mask - bias
+
+ // Keep the top 12+e bits, the integer part; clear the rest.
+ if e < 64-12 {
+ b &^= 1<<(64-12-e) - 1
+ }
+ return Float64frombits(b)
}
// Round returns the nearest integer, rounding half away from zero.
// Modf(±Inf) = ±Inf, NaN
// Modf(NaN) = NaN, NaN
func Modf(f float64) (int float64, frac float64) {
- if haveArchModf {
- return archModf(f)
- }
- return modf(f)
-}
-
-func modf(f float64) (int float64, frac float64) {
- if f < 1 {
- switch {
- case f < 0:
- int, frac = Modf(-f)
- return -int, -frac
- case f == 0:
- return f, f // Return -0, -0 when f == -0
- }
- return 0, f
- }
-
- x := Float64bits(f)
- e := uint(x>>shift)&mask - bias
-
- // Keep the top 12+e bits, the integer part; clear the rest.
- if e < 64-12 {
- x &^= 1<<(64-12-e) - 1
- }
- int = Float64frombits(x)
- frac = f - int
+ int = Trunc(f)
+ frac = Copysign(f-int, f)
return
}
+++ /dev/null
-// Copyright 2016 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.
-
-#include "textflag.h"
-
-// func archModf(f float64) (int float64, frac float64)
-TEXT ·archModf(SB),NOSPLIT,$0
- MOVD f+0(FP), R0
- FMOVD R0, F0
- FRINTZD F0, F1
- FMOVD F1, int+8(FP)
- FSUBD F1, F0
- FMOVD F0, R1
- AND $(1<<63), R0
- ORR R0, R1 // must have same sign
- MOVD R1, frac+16(FP)
- RET
+++ /dev/null
-// Copyright 2021 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.
-
-//go:build arm64 || ppc64 || ppc64le
-
-package math
-
-const haveArchModf = true
-
-func archModf(f float64) (int float64, frac float64)
+++ /dev/null
-// Copyright 2021 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.
-
-//go:build !arm64 && !ppc64 && !ppc64le
-
-package math
-
-const haveArchModf = false
-
-func archModf(f float64) (int float64, frac float64) {
- panic("not implemented")
-}
+++ /dev/null
-// Copyright 2017 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.
-
-//go:build ppc64 || ppc64le
-
-#include "textflag.h"
-
-// func archModf(f float64) (int float64, frac float64)
-TEXT ·archModf(SB),NOSPLIT,$0
- FMOVD f+0(FP), F0
- FRIZ F0, F1
- FMOVD F1, int+8(FP)
- FSUB F1, F0, F2
- FCPSGN F2, F0, F2
- FMOVD F2, frac+16(FP)
- RET