float64 is now an alias to 'float'; fixes #545

This commit is contained in:
Araq 2013-08-30 12:24:54 +02:00
commit 7056ceda67
8 changed files with 50 additions and 19 deletions

View file

@ -465,10 +465,11 @@ proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) = proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
const const
binArithTab: array[mAddF64..mXor, string] = [ binArithTab: array[mAddF64..mXor, string] = [
"($1 + $2)", # AddF64 "(($4)($1) + ($4)($2))", # AddF64
"($1 - $2)", # SubF64 "(($4)($1) - ($4)($2))", # SubF64
"($1 * $2)", # MulF64 "(($4)($1) * ($4)($2))", # MulF64
"($1 / $2)", # DivF64 "(($4)($1) / ($4)($2))", # DivF64
"($4)((NU$3)($1) >> (NU$3)($2))", # ShrI "($4)((NU$3)($1) >> (NU$3)($2))", # ShrI
"($4)((NU$3)($1) << (NU$3)($2))", # ShlI "($4)((NU$3)($1) << (NU$3)($2))", # ShlI
"($4)($1 & $2)", # BitandI "($4)($1 & $2)", # BitandI
@ -1488,8 +1489,9 @@ proc binaryFloatArith(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
assert(e.sons[2].typ != nil) assert(e.sons[2].typ != nil)
InitLocExpr(p, e.sons[1], a) InitLocExpr(p, e.sons[1], a)
InitLocExpr(p, e.sons[2], b) InitLocExpr(p, e.sons[2], b)
putIntoDest(p, d, e.typ, rfmt(nil, "($2 $1 $3)", putIntoDest(p, d, e.typ, rfmt(nil, "(($4)($2) $1 ($4)($3))",
toRope(opr[m]), rdLoc(a), rdLoc(b))) toRope(opr[m]), rdLoc(a), rdLoc(b),
getSimpleTypeDesc(p.module, e[1].typ)))
if optNanCheck in p.options: if optNanCheck in p.options:
linefmt(p, cpsStmts, "#nanCheck($1);$n", rdLoc(d)) linefmt(p, cpsStmts, "#nanCheck($1);$n", rdLoc(d))
if optInfCheck in p.options: if optInfCheck in p.options:

View file

@ -115,11 +115,16 @@ proc getIntLitType*(literal: PNode): PType =
result = copyType(ti, ti.owner, false) result = copyType(ti, ti.owner, false)
result.n = literal result.n = literal
proc getFloatLitType*(literal: PNode): PType =
# for now we do not cache these:
result = newSysType(tyFloat, size=8)
result.n = literal
proc skipIntLit*(t: PType): PType {.inline.} = proc skipIntLit*(t: PType): PType {.inline.} =
if t.kind == tyInt and t.n != nil: if t.n != nil:
result = getSysType(tyInt) if t.kind in {tyInt, tyFloat}:
else: return getSysType(t.kind)
result = t result = t
proc AddSonSkipIntLit*(father, son: PType) = proc AddSonSkipIntLit*(father, son: PType) =
if isNil(father.sons): father.sons = @[] if isNil(father.sons): father.sons = @[]

View file

@ -1808,7 +1808,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
of nkUInt64Lit: of nkUInt64Lit:
if result.typ == nil: result.typ = getSysType(tyUInt64) if result.typ == nil: result.typ = getSysType(tyUInt64)
of nkFloatLit: of nkFloatLit:
if result.typ == nil: result.typ = getSysType(tyFloat) if result.typ == nil: result.typ = getFloatLitType(result)
of nkFloat32Lit: of nkFloat32Lit:
if result.typ == nil: result.typ = getSysType(tyFloat32) if result.typ == nil: result.typ = getSysType(tyFloat32)
of nkFloat64Lit: of nkFloat64Lit:

View file

@ -21,7 +21,7 @@ proc getConstExpr*(m: PSym, n: PNode): PNode
proc evalOp*(m: TMagic, n, a, b, c: PNode): PNode proc evalOp*(m: TMagic, n, a, b, c: PNode): PNode
proc leValueConv*(a, b: PNode): bool proc leValueConv*(a, b: PNode): bool
proc newIntNodeT*(intVal: BiggestInt, n: PNode): PNode proc newIntNodeT*(intVal: BiggestInt, n: PNode): PNode
proc newFloatNodeT*(floatVal: BiggestFloat, n: PNode): PNode proc newFloatNodeT(floatVal: BiggestFloat, n: PNode): PNode
proc newStrNodeT*(strVal: string, n: PNode): PNode proc newStrNodeT*(strVal: string, n: PNode): PNode
# implementation # implementation
@ -43,7 +43,10 @@ proc newIntNodeT(intVal: BiggestInt, n: PNode): PNode =
proc newFloatNodeT(floatVal: BiggestFloat, n: PNode): PNode = proc newFloatNodeT(floatVal: BiggestFloat, n: PNode): PNode =
result = newFloatNode(nkFloatLit, floatVal) result = newFloatNode(nkFloatLit, floatVal)
result.typ = n.typ if skipTypes(n.typ, abstractVarRange).kind == tyFloat:
result.typ = getFloatLitType(result)
else:
result.typ = n.typ
result.info = n.info result.info = n.info
proc newStrNodeT(strVal: string, n: PNode): PNode = proc newStrNodeT(strVal: string, n: PNode): PNode =

View file

@ -290,7 +290,11 @@ proc handleFloatRange(f, a: PType): TTypeRelation =
let ab = skipTypes(a, {tyRange}) let ab = skipTypes(a, {tyRange})
var k = ab.kind var k = ab.kind
if k == f.kind: result = isSubrange if k == f.kind: result = isSubrange
elif isFloatLit(ab): result = isFromIntLit
elif isIntLit(ab): result = isConvertible elif isIntLit(ab): result = isConvertible
elif f.kind == tyFloat32:
# no conversion to "float32" as that might lose precision
result = isNone
elif k >= tyFloat and k <= tyFloat128: result = isConvertible elif k >= tyFloat and k <= tyFloat128: result = isConvertible
else: result = isNone else: result = isNone

View file

@ -109,6 +109,9 @@ proc getOrdValue(n: PNode): biggestInt =
proc isIntLit*(t: PType): bool {.inline.} = proc isIntLit*(t: PType): bool {.inline.} =
result = t.kind == tyInt and t.n != nil and t.n.kind == nkIntLit result = t.kind == tyInt and t.n != nil and t.n.kind == nkIntLit
proc isFloatLit*(t: PType): bool {.inline.} =
result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
proc isCompatibleToCString(a: PType): bool = proc isCompatibleToCString(a: PType): bool =
if a.kind == tyArray: if a.kind == tyArray:
if (firstOrd(a.sons[0]) == 0) and if (firstOrd(a.sons[0]) == 0) and

View file

@ -28,7 +28,9 @@ type
uint64* {.magic: UInt64.} ## unsigned 64 bit integer type uint64* {.magic: UInt64.} ## unsigned 64 bit integer type
float* {.magic: Float.} ## default floating point type float* {.magic: Float.} ## default floating point type
float32* {.magic: Float32.} ## 32 bit floating point type float32* {.magic: Float32.} ## 32 bit floating point type
float64* {.magic: Float64.} ## 64 bit floating point type float64* {.magic: Float.} ## 64 bit floating point type
# 'float64' is now an alias to 'float'; this solves many problems
type # we need to start a new type section here, so that ``0`` can have a type type # we need to start a new type section here, so that ``0`` can have a type
bool* {.magic: Bool.} = enum ## built-in boolean type bool* {.magic: Bool.} = enum ## built-in boolean type
@ -627,6 +629,14 @@ proc `<%` *(x, y: Int64): bool {.magic: "LtU64", noSideEffect.}
# floating point operations: # floating point operations:
proc `+` *(x: float32): float32 {.magic: "UnaryPlusF64", noSideEffect.}
proc `-` *(x: float32): float32 {.magic: "UnaryMinusF64", noSideEffect.}
proc `+` *(x, y: float32): float32 {.magic: "AddF64", noSideEffect.}
proc `-` *(x, y: float32): float32 {.magic: "SubF64", noSideEffect.}
proc `*` *(x, y: float32): float32 {.magic: "MulF64", noSideEffect.}
proc `/` *(x, y: float32): float32 {.magic: "DivF64", noSideEffect.}
proc `+` *(x: float): float {.magic: "UnaryPlusF64", noSideEffect.} proc `+` *(x: float): float {.magic: "UnaryPlusF64", noSideEffect.}
proc `-` *(x: float): float {.magic: "UnaryMinusF64", noSideEffect.} proc `-` *(x: float): float {.magic: "UnaryMinusF64", noSideEffect.}
proc `+` *(x, y: float): float {.magic: "AddF64", noSideEffect.} proc `+` *(x, y: float): float {.magic: "AddF64", noSideEffect.}
@ -635,6 +645,10 @@ proc `*` *(x, y: float): float {.magic: "MulF64", noSideEffect.}
proc `/` *(x, y: float): float {.magic: "DivF64", noSideEffect.} proc `/` *(x, y: float): float {.magic: "DivF64", noSideEffect.}
## computes the floating point division ## computes the floating point division
proc `==` *(x, y: float32): bool {.magic: "EqF64", noSideEffect.}
proc `<=` *(x, y: float32): bool {.magic: "LeF64", noSideEffect.}
proc `<` *(x, y: float32): bool {.magic: "LtF64", noSideEffect.}
proc `==` *(x, y: float): bool {.magic: "EqF64", noSideEffect.} proc `==` *(x, y: float): bool {.magic: "EqF64", noSideEffect.}
proc `<=` *(x, y: float): bool {.magic: "LeF64", noSideEffect.} proc `<=` *(x, y: float): bool {.magic: "LeF64", noSideEffect.}
proc `<` *(x, y: float): bool {.magic: "LtF64", noSideEffect.} proc `<` *(x, y: float): bool {.magic: "LtF64", noSideEffect.}
@ -1985,7 +1999,7 @@ when not defined(JS): #and not defined(NimrodVM):
## `content` completely to the file and closes the file afterwards. ## `content` completely to the file and closes the file afterwards.
## Raises an IO exception in case of an error. ## Raises an IO exception in case of an error.
proc write*(f: TFile, r: float) {.tags: [FWriteIO].} proc write*(f: TFile, r: float32) {.tags: [FWriteIO].}
proc write*(f: TFile, i: int) {.tags: [FWriteIO].} proc write*(f: TFile, i: int) {.tags: [FWriteIO].}
proc write*(f: TFile, i: biggestInt) {.tags: [FWriteIO].} proc write*(f: TFile, i: biggestInt) {.tags: [FWriteIO].}
proc write*(f: TFile, r: biggestFloat) {.tags: [FWriteIO].} proc write*(f: TFile, r: biggestFloat) {.tags: [FWriteIO].}

View file

@ -91,7 +91,7 @@ proc write(f: TFile, i: biggestInt) =
proc write(f: TFile, b: bool) = proc write(f: TFile, b: bool) =
if b: write(f, "true") if b: write(f, "true")
else: write(f, "false") else: write(f, "false")
proc write(f: TFile, r: float) = fprintf(f, "%g", r) proc write(f: TFile, r: float32) = fprintf(f, "%g", r)
proc write(f: TFile, r: biggestFloat) = fprintf(f, "%g", r) proc write(f: TFile, r: biggestFloat) = fprintf(f, "%g", r)
proc write(f: TFile, c: Char) = putc(c, f) proc write(f: TFile, c: Char) = putc(c, f)