Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
ef46a9dd
Commit
ef46a9dd
authored
Nov 15, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gc: fix up floating point NaN comparisons
Fixes #167. R=ken2
https://golang.org/cl/155062
parent
391e082c
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
181 additions
and
38 deletions
+181
-38
src/cmd/5g/cgen.c
src/cmd/5g/cgen.c
+12
-1
src/cmd/6g/cgen.c
src/cmd/6g/cgen.c
+41
-14
src/cmd/6g/gsubr.c
src/cmd/6g/gsubr.c
+0
-4
src/cmd/8g/cgen.c
src/cmd/8g/cgen.c
+36
-11
src/cmd/8g/gsubr.c
src/cmd/8g/gsubr.c
+0
-4
test/float_lit.go
test/float_lit.go
+4
-4
test/floatcmp.go
test/floatcmp.go
+88
-0
No files found.
src/cmd/5g/cgen.c
View file @
ef46a9dd
...
...
@@ -895,8 +895,19 @@ bgen(Node *n, int true, Prog *to)
case
OLE
:
case
OGE
:
a
=
n
->
op
;
if
(
!
true
)
if
(
!
true
)
{
if
(
isfloat
[
nl
->
type
->
etype
])
{
// brcom is not valid on floats when NaN is involved.
p1
=
gbranch
(
AJMP
,
T
);
p2
=
gbranch
(
AJMP
,
T
);
patch
(
p1
,
pc
);
bgen
(
n
,
1
,
p2
);
patch
(
gbranch
(
AJMP
,
T
),
to
);
patch
(
p2
,
pc
);
goto
ret
;
}
a
=
brcom
(
a
);
}
// make simplest on right
if
(
nl
->
op
==
OLITERAL
||
nl
->
ullman
<
nr
->
ullman
)
{
...
...
src/cmd/6g/cgen.c
View file @
ef46a9dd
...
...
@@ -668,7 +668,7 @@ void
bgen
(
Node
*
n
,
int
true
,
Prog
*
to
)
{
int
et
,
a
;
Node
*
nl
,
*
nr
,
*
r
;
Node
*
nl
,
*
nr
,
*
l
,
*
r
;
Node
n1
,
n2
,
tmp
;
Prog
*
p1
,
*
p2
;
...
...
@@ -782,8 +782,19 @@ bgen(Node *n, int true, Prog *to)
case
OLE
:
case
OGE
:
a
=
n
->
op
;
if
(
!
true
)
if
(
!
true
)
{
if
(
isfloat
[
nr
->
type
->
etype
])
{
// brcom is not valid on floats when NaN is involved.
p1
=
gbranch
(
AJMP
,
T
);
p2
=
gbranch
(
AJMP
,
T
);
patch
(
p1
,
pc
);
bgen
(
n
,
1
,
p2
);
patch
(
gbranch
(
AJMP
,
T
),
to
);
patch
(
p2
,
pc
);
goto
ret
;
}
a
=
brcom
(
a
);
}
// make simplest on right
if
(
nl
->
op
==
OLITERAL
||
nl
->
ullman
<
nr
->
ullman
)
{
...
...
@@ -792,7 +803,7 @@ bgen(Node *n, int true, Prog *to)
nl
=
nr
;
nr
=
r
;
}
if
(
isslice
(
nl
->
type
))
{
// only valid to cmp darray to literal nil
if
((
a
!=
OEQ
&&
a
!=
ONE
)
||
nr
->
op
!=
OLITERAL
)
{
...
...
@@ -831,8 +842,6 @@ bgen(Node *n, int true, Prog *to)
break
;
}
a
=
optoas
(
a
,
nr
->
type
);
if
(
nr
->
ullman
>=
UINF
)
{
regalloc
(
&
n1
,
nr
->
type
,
N
);
cgen
(
nr
,
&
n1
);
...
...
@@ -847,12 +856,7 @@ bgen(Node *n, int true, Prog *to)
regalloc
(
&
n2
,
nr
->
type
,
&
n2
);
cgen
(
&
tmp
,
&
n2
);
gins
(
optoas
(
OCMP
,
nr
->
type
),
&
n1
,
&
n2
);
patch
(
gbranch
(
a
,
nr
->
type
),
to
);
regfree
(
&
n1
);
regfree
(
&
n2
);
break
;
goto
cmp
;
}
regalloc
(
&
n1
,
nl
->
type
,
N
);
...
...
@@ -860,17 +864,40 @@ bgen(Node *n, int true, Prog *to)
if
(
smallintconst
(
nr
))
{
gins
(
optoas
(
OCMP
,
nr
->
type
),
&
n1
,
nr
);
patch
(
gbranch
(
a
,
nr
->
type
),
to
);
patch
(
gbranch
(
optoas
(
a
,
nr
->
type
)
,
nr
->
type
),
to
);
regfree
(
&
n1
);
break
;
}
regalloc
(
&
n2
,
nr
->
type
,
N
);
cgen
(
nr
,
&
n2
);
cmp:
// only < and <= work right with NaN; reverse if needed
l
=
&
n1
;
r
=
&
n2
;
if
(
isfloat
[
nl
->
type
->
etype
]
&&
(
a
==
OGT
||
a
==
OGE
))
{
l
=
&
n2
;
r
=
&
n1
;
a
=
brrev
(
a
);
}
gins
(
optoas
(
OCMP
,
nr
->
type
),
&
n1
,
&
n2
);
patch
(
gbranch
(
a
,
nr
->
type
),
to
);
gins
(
optoas
(
OCMP
,
nr
->
type
),
l
,
r
);
if
(
isfloat
[
nr
->
type
->
etype
]
&&
(
n
->
op
==
OEQ
||
n
->
op
==
ONE
))
{
if
(
n
->
op
==
OEQ
)
{
// neither NE nor P
p1
=
gbranch
(
AJNE
,
T
);
p2
=
gbranch
(
AJPS
,
T
);
patch
(
gbranch
(
AJMP
,
T
),
to
);
patch
(
p1
,
pc
);
patch
(
p2
,
pc
);
}
else
{
// either NE or P
patch
(
gbranch
(
AJNE
,
T
),
to
);
patch
(
gbranch
(
AJPS
,
T
),
to
);
}
}
else
patch
(
gbranch
(
optoas
(
a
,
nr
->
type
),
nr
->
type
),
to
);
regfree
(
&
n1
);
regfree
(
&
n2
);
break
;
...
...
src/cmd/6g/gsubr.c
View file @
ef46a9dd
...
...
@@ -1146,8 +1146,6 @@ optoas(int op, Type *t)
case
CASE
(
OLT
,
TUINT16
):
case
CASE
(
OLT
,
TUINT32
):
case
CASE
(
OLT
,
TUINT64
):
case
CASE
(
OGT
,
TFLOAT32
):
case
CASE
(
OGT
,
TFLOAT64
):
a
=
AJCS
;
break
;
...
...
@@ -1162,8 +1160,6 @@ optoas(int op, Type *t)
case
CASE
(
OLE
,
TUINT16
):
case
CASE
(
OLE
,
TUINT32
):
case
CASE
(
OLE
,
TUINT64
):
case
CASE
(
OGE
,
TFLOAT32
):
case
CASE
(
OGE
,
TFLOAT64
):
a
=
AJLS
;
break
;
...
...
src/cmd/8g/cgen.c
View file @
ef46a9dd
...
...
@@ -838,8 +838,19 @@ bgen(Node *n, int true, Prog *to)
case
OLE
:
case
OGE
:
a
=
n
->
op
;
if
(
!
true
)
if
(
!
true
)
{
if
(
isfloat
[
nl
->
type
->
etype
])
{
// brcom is not valid on floats when NaN is involved.
p1
=
gbranch
(
AJMP
,
T
);
p2
=
gbranch
(
AJMP
,
T
);
patch
(
p1
,
pc
);
bgen
(
n
,
1
,
p2
);
patch
(
gbranch
(
AJMP
,
T
),
to
);
patch
(
p2
,
pc
);
break
;
}
a
=
brcom
(
a
);
}
// make simplest on right
if
(
nl
->
op
==
OLITERAL
||
nl
->
ullman
<
nr
->
ullman
)
{
...
...
@@ -888,6 +899,14 @@ bgen(Node *n, int true, Prog *to)
}
if
(
isfloat
[
nr
->
type
->
etype
])
{
a
=
brrev
(
a
);
// because the args are stacked
if
(
a
==
OGE
||
a
==
OGT
)
{
// only < and <= work right with NaN; reverse if needed
r
=
nr
;
nr
=
nl
;
nl
=
r
;
a
=
brrev
(
a
);
}
nodreg
(
&
tmp
,
nr
->
type
,
D_F0
);
nodreg
(
&
n2
,
nr
->
type
,
D_F0
+
1
);
nodreg
(
&
ax
,
types
[
TUINT16
],
D_AX
);
...
...
@@ -915,7 +934,19 @@ bgen(Node *n, int true, Prog *to)
}
gins
(
AFSTSW
,
N
,
&
ax
);
gins
(
ASAHF
,
N
,
N
);
patch
(
gbranch
(
optoas
(
brrev
(
a
),
nr
->
type
),
T
),
to
);
if
(
a
==
OEQ
)
{
// neither NE nor P
p1
=
gbranch
(
AJNE
,
T
);
p2
=
gbranch
(
AJPS
,
T
);
patch
(
gbranch
(
AJMP
,
T
),
to
);
patch
(
p1
,
pc
);
patch
(
p2
,
pc
);
}
else
if
(
a
==
ONE
)
{
// either NE or P
patch
(
gbranch
(
AJNE
,
T
),
to
);
patch
(
gbranch
(
AJPS
,
T
),
to
);
}
else
patch
(
gbranch
(
optoas
(
a
,
nr
->
type
),
T
),
to
);
break
;
}
...
...
@@ -941,21 +972,14 @@ bgen(Node *n, int true, Prog *to)
a
=
optoas
(
a
,
nr
->
type
);
if
(
nr
->
ullman
>=
UINF
)
{
tempalloc
(
&
n1
,
nl
->
type
);
tempalloc
(
&
tmp
,
nr
->
type
);
cgen
(
nr
,
&
tmp
);
tempalloc
(
&
n1
,
nl
->
type
);
cgen
(
nl
,
&
n1
);
regalloc
(
&
n2
,
nr
->
type
,
N
);
cgen
(
&
tmp
,
&
n2
);
gins
(
optoas
(
OCMP
,
nr
->
type
),
&
n1
,
&
n2
);
patch
(
gbranch
(
a
,
nr
->
type
),
to
);
tempfree
(
&
n1
);
tempfree
(
&
tmp
);
regfree
(
&
n2
);
break
;
goto
cmp
;
}
tempalloc
(
&
n1
,
nl
->
type
);
...
...
@@ -974,6 +998,7 @@ bgen(Node *n, int true, Prog *to)
gmove
(
&
tmp
,
&
n2
);
tempfree
(
&
tmp
);
cmp:
gins
(
optoas
(
OCMP
,
nr
->
type
),
&
n1
,
&
n2
);
patch
(
gbranch
(
a
,
nr
->
type
),
to
);
regfree
(
&
n2
);
...
...
src/cmd/8g/gsubr.c
View file @
ef46a9dd
...
...
@@ -260,8 +260,6 @@ optoas(int op, Type *t)
case
CASE
(
OLT
,
TUINT16
):
case
CASE
(
OLT
,
TUINT32
):
case
CASE
(
OLT
,
TUINT64
):
case
CASE
(
OGT
,
TFLOAT32
):
case
CASE
(
OGT
,
TFLOAT64
):
a
=
AJCS
;
break
;
...
...
@@ -276,8 +274,6 @@ optoas(int op, Type *t)
case
CASE
(
OLE
,
TUINT16
):
case
CASE
(
OLE
,
TUINT32
):
case
CASE
(
OLE
,
TUINT64
):
case
CASE
(
OGE
,
TFLOAT32
):
case
CASE
(
OGE
,
TFLOAT64
):
a
=
AJLS
;
break
;
...
...
test/float_lit.go
View file @
ef46a9dd
...
...
@@ -20,8 +20,8 @@ close(da float64, ia, ib int64, pow int) bool
db
:=
float64
(
ia
)
/
float64
(
ib
);
db
*=
pow10
(
pow
);
if
da
==
0
{
if
db
==
0
{
if
da
==
0
||
db
==
0
{
if
d
a
==
0
&&
d
b
==
0
{
return
true
;
}
return
false
;
...
...
@@ -59,8 +59,8 @@ main()
if
!
close
(
-
210e3
,
-
210
,
1
,
3
)
{
print
(
"-210e3 is "
,
-
210e3
,
"
\n
"
);
}
if
!
close
(
0E-1
,
0
,
1
,
0
)
{
print
(
"0E-1 is "
,
0E-1
,
"
\n
"
);
}
if
!
close
(
+
0e23
,
0
,
1
,
23
)
{
print
(
"+0e23 is "
,
+
0e23
,
"
\n
"
);
}
if
!
close
(
-
0e345
,
0
,
1
,
345
)
{
print
(
"-0e345 is "
,
-
0e345
,
"
\n
"
);
}
if
!
close
(
+
0e23
,
0
,
1
,
1
)
{
print
(
"+0e23 is "
,
+
0e23
,
"
\n
"
);
}
if
!
close
(
-
0e345
,
0
,
1
,
1
)
{
print
(
"-0e345 is "
,
-
0e345
,
"
\n
"
);
}
if
!
close
(
0E1
,
0
,
1
,
1
)
{
print
(
"0E1 is "
,
0E1
,
"
\n
"
);
}
if
!
close
(
+
10e23
,
10
,
1
,
23
)
{
print
(
"+10e23 is "
,
+
10e23
,
"
\n
"
);
}
...
...
test/floatcmp.go
0 → 100644
View file @
ef46a9dd
// $G $F.go && $L $F.$A && ./$A.out
// Copyright 2009 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
main
import
"math"
type
floatTest
struct
{
name
string
;
expr
bool
;
want
bool
;
}
var
nan
float64
=
math
.
NaN
();
var
f
float64
=
1
;
var
tests
=
[]
floatTest
{
floatTest
{
"nan == nan"
,
nan
==
nan
,
false
},
floatTest
{
"nan != nan"
,
nan
!=
nan
,
true
},
floatTest
{
"nan < nan"
,
nan
<
nan
,
false
},
floatTest
{
"nan > nan"
,
nan
>
nan
,
false
},
floatTest
{
"nan <= nan"
,
nan
<=
nan
,
false
},
floatTest
{
"nan >= nan"
,
nan
>=
nan
,
false
},
floatTest
{
"f == nan"
,
f
==
nan
,
false
},
floatTest
{
"f != nan"
,
f
!=
nan
,
true
},
floatTest
{
"f < nan"
,
f
<
nan
,
false
},
floatTest
{
"f > nan"
,
f
>
nan
,
false
},
floatTest
{
"f <= nan"
,
f
<=
nan
,
false
},
floatTest
{
"f >= nan"
,
f
>=
nan
,
false
},
floatTest
{
"nan == f"
,
nan
==
f
,
false
},
floatTest
{
"nan != f"
,
nan
!=
f
,
true
},
floatTest
{
"nan < f"
,
nan
<
f
,
false
},
floatTest
{
"nan > f"
,
nan
>
f
,
false
},
floatTest
{
"nan <= f"
,
nan
<=
f
,
false
},
floatTest
{
"nan >= f"
,
nan
>=
f
,
false
},
floatTest
{
"!(nan == nan)"
,
!
(
nan
==
nan
),
true
},
floatTest
{
"!(nan != nan)"
,
!
(
nan
!=
nan
),
false
},
floatTest
{
"!(nan < nan)"
,
!
(
nan
<
nan
),
true
},
floatTest
{
"!(nan > nan)"
,
!
(
nan
>
nan
),
true
},
floatTest
{
"!(nan <= nan)"
,
!
(
nan
<=
nan
),
true
},
floatTest
{
"!(nan >= nan)"
,
!
(
nan
>=
nan
),
true
},
floatTest
{
"!(f == nan)"
,
!
(
f
==
nan
),
true
},
floatTest
{
"!(f != nan)"
,
!
(
f
!=
nan
),
false
},
floatTest
{
"!(f < nan)"
,
!
(
f
<
nan
),
true
},
floatTest
{
"!(f > nan)"
,
!
(
f
>
nan
),
true
},
floatTest
{
"!(f <= nan)"
,
!
(
f
<=
nan
),
true
},
floatTest
{
"!(f >= nan)"
,
!
(
f
>=
nan
),
true
},
floatTest
{
"!(nan == f)"
,
!
(
nan
==
f
),
true
},
floatTest
{
"!(nan != f)"
,
!
(
nan
!=
f
),
false
},
floatTest
{
"!(nan < f)"
,
!
(
nan
<
f
),
true
},
floatTest
{
"!(nan > f)"
,
!
(
nan
>
f
),
true
},
floatTest
{
"!(nan <= f)"
,
!
(
nan
<=
f
),
true
},
floatTest
{
"!(nan >= f)"
,
!
(
nan
>=
f
),
true
},
floatTest
{
"!!(nan == nan)"
,
!!
(
nan
==
nan
),
false
},
floatTest
{
"!!(nan != nan)"
,
!!
(
nan
!=
nan
),
true
},
floatTest
{
"!!(nan < nan)"
,
!!
(
nan
<
nan
),
false
},
floatTest
{
"!!(nan > nan)"
,
!!
(
nan
>
nan
),
false
},
floatTest
{
"!!(nan <= nan)"
,
!!
(
nan
<=
nan
),
false
},
floatTest
{
"!!(nan >= nan)"
,
!!
(
nan
>=
nan
),
false
},
floatTest
{
"!!(f == nan)"
,
!!
(
f
==
nan
),
false
},
floatTest
{
"!!(f != nan)"
,
!!
(
f
!=
nan
),
true
},
floatTest
{
"!!(f < nan)"
,
!!
(
f
<
nan
),
false
},
floatTest
{
"!!(f > nan)"
,
!!
(
f
>
nan
),
false
},
floatTest
{
"!!(f <= nan)"
,
!!
(
f
<=
nan
),
false
},
floatTest
{
"!!(f >= nan)"
,
!!
(
f
>=
nan
),
false
},
floatTest
{
"!!(nan == f)"
,
!!
(
nan
==
f
),
false
},
floatTest
{
"!!(nan != f)"
,
!!
(
nan
!=
f
),
true
},
floatTest
{
"!!(nan < f)"
,
!!
(
nan
<
f
),
false
},
floatTest
{
"!!(nan > f)"
,
!!
(
nan
>
f
),
false
},
floatTest
{
"!!(nan <= f)"
,
!!
(
nan
<=
f
),
false
},
floatTest
{
"!!(nan >= f)"
,
!!
(
nan
>=
f
),
false
},
}
func
main
()
{
bad
:=
false
;
for
_
,
t
:=
range
tests
{
if
t
.
expr
!=
t
.
want
{
if
!
bad
{
bad
=
true
;
println
(
"BUG: floatcmp"
);
}
println
(
t
.
name
,
"="
,
t
.
expr
,
"want"
,
t
.
want
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment