github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/vlrt.go (about) 1 // Inferno's libkern/vlrt-arm.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/master/libkern/vlrt-arm.c 3 // 4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 5 // Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com). All rights reserved. 6 // Portions Copyright 2009 The Go Authors. All rights reserved. 7 // 8 // Permission is hereby granted, free of charge, to any person obtaining a copy 9 // of this software and associated documentation files (the "Software"), to deal 10 // in the Software without restriction, including without limitation the rights 11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 // copies of the Software, and to permit persons to whom the Software is 13 // furnished to do so, subject to the following conditions: 14 // 15 // The above copyright notice and this permission notice shall be included in 16 // all copies or substantial portions of the Software. 17 // 18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 // THE SOFTWARE. 25 26 // +build arm 386 mips mipsle 27 28 package runtime 29 30 import "unsafe" 31 32 const ( 33 sign32 = 1 << (32 - 1) 34 sign64 = 1 << (64 - 1) 35 ) 36 37 func float64toint64(d float64) (y uint64) { 38 _d2v(&y, d) 39 return 40 } 41 42 func float64touint64(d float64) (y uint64) { 43 _d2v(&y, d) 44 return 45 } 46 47 func int64tofloat64(y int64) float64 { 48 if y < 0 { 49 return -uint64tofloat64(-uint64(y)) 50 } 51 return uint64tofloat64(uint64(y)) 52 } 53 54 func uint64tofloat64(y uint64) float64 { 55 hi := float64(uint32(y >> 32)) 56 lo := float64(uint32(y)) 57 d := hi*(1<<32) + lo 58 return d 59 } 60 61 func _d2v(y *uint64, d float64) { 62 x := *(*uint64)(unsafe.Pointer(&d)) 63 64 xhi := uint32(x>>32)&0xfffff | 0x100000 65 xlo := uint32(x) 66 sh := 1075 - int32(uint32(x>>52)&0x7ff) 67 68 var ylo, yhi uint32 69 if sh >= 0 { 70 sh := uint32(sh) 71 /* v = (hi||lo) >> sh */ 72 if sh < 32 { 73 if sh == 0 { 74 ylo = xlo 75 yhi = xhi 76 } else { 77 ylo = xlo>>sh | xhi<<(32-sh) 78 yhi = xhi >> sh 79 } 80 } else { 81 if sh == 32 { 82 ylo = xhi 83 } else if sh < 64 { 84 ylo = xhi >> (sh - 32) 85 } 86 } 87 } else { 88 /* v = (hi||lo) << -sh */ 89 sh := uint32(-sh) 90 if sh <= 11 { 91 ylo = xlo << sh 92 yhi = xhi<<sh | xlo>>(32-sh) 93 } else { 94 /* overflow */ 95 yhi = uint32(d) /* causes something awful */ 96 } 97 } 98 if x&sign64 != 0 { 99 if ylo != 0 { 100 ylo = -ylo 101 yhi = ^yhi 102 } else { 103 yhi = -yhi 104 } 105 } 106 107 *y = uint64(yhi)<<32 | uint64(ylo) 108 } 109 func uint64div(n, d uint64) uint64 { 110 // Check for 32 bit operands 111 if uint32(n>>32) == 0 && uint32(d>>32) == 0 { 112 if uint32(d) == 0 { 113 panicdivide() 114 } 115 return uint64(uint32(n) / uint32(d)) 116 } 117 q, _ := dodiv(n, d) 118 return q 119 } 120 121 func uint64mod(n, d uint64) uint64 { 122 // Check for 32 bit operands 123 if uint32(n>>32) == 0 && uint32(d>>32) == 0 { 124 if uint32(d) == 0 { 125 panicdivide() 126 } 127 return uint64(uint32(n) % uint32(d)) 128 } 129 _, r := dodiv(n, d) 130 return r 131 } 132 133 func int64div(n, d int64) int64 { 134 // Check for 32 bit operands 135 if int64(int32(n)) == n && int64(int32(d)) == d { 136 if int32(n) == -0x80000000 && int32(d) == -1 { 137 // special case: 32-bit -0x80000000 / -1 = -0x80000000, 138 // but 64-bit -0x80000000 / -1 = 0x80000000. 139 return 0x80000000 140 } 141 if int32(d) == 0 { 142 panicdivide() 143 } 144 return int64(int32(n) / int32(d)) 145 } 146 147 nneg := n < 0 148 dneg := d < 0 149 if nneg { 150 n = -n 151 } 152 if dneg { 153 d = -d 154 } 155 uq, _ := dodiv(uint64(n), uint64(d)) 156 q := int64(uq) 157 if nneg != dneg { 158 q = -q 159 } 160 return q 161 } 162 163 //go:nosplit 164 func int64mod(n, d int64) int64 { 165 // Check for 32 bit operands 166 if int64(int32(n)) == n && int64(int32(d)) == d { 167 if int32(d) == 0 { 168 panicdivide() 169 } 170 return int64(int32(n) % int32(d)) 171 } 172 173 nneg := n < 0 174 if nneg { 175 n = -n 176 } 177 if d < 0 { 178 d = -d 179 } 180 _, ur := dodiv(uint64(n), uint64(d)) 181 r := int64(ur) 182 if nneg { 183 r = -r 184 } 185 return r 186 } 187 188 //go:noescape 189 func _mul64by32(lo64 *uint64, a uint64, b uint32) (hi32 uint32) 190 191 //go:noescape 192 func _div64by32(a uint64, b uint32, r *uint32) (q uint32) 193 194 //go:nosplit 195 func dodiv(n, d uint64) (q, r uint64) { 196 if GOARCH == "arm" { 197 // arm doesn't have a division instruction, so 198 // slowdodiv is the best that we can do. 199 return slowdodiv(n, d) 200 } 201 202 if GOARCH == "mips" || GOARCH == "mipsle" { 203 // No _div64by32 on mips and using only _mul64by32 doesn't bring much benefit 204 return slowdodiv(n, d) 205 } 206 207 if d > n { 208 return 0, n 209 } 210 211 if uint32(d>>32) != 0 { 212 t := uint32(n>>32) / uint32(d>>32) 213 var lo64 uint64 214 hi32 := _mul64by32(&lo64, d, t) 215 if hi32 != 0 || lo64 > n { 216 return slowdodiv(n, d) 217 } 218 return uint64(t), n - lo64 219 } 220 221 // d is 32 bit 222 var qhi uint32 223 if uint32(n>>32) >= uint32(d) { 224 if uint32(d) == 0 { 225 panicdivide() 226 } 227 qhi = uint32(n>>32) / uint32(d) 228 n -= uint64(uint32(d)*qhi) << 32 229 } else { 230 qhi = 0 231 } 232 233 var rlo uint32 234 qlo := _div64by32(n, uint32(d), &rlo) 235 return uint64(qhi)<<32 + uint64(qlo), uint64(rlo) 236 } 237 238 //go:nosplit 239 func slowdodiv(n, d uint64) (q, r uint64) { 240 if d == 0 { 241 panicdivide() 242 } 243 244 // Set up the divisor and find the number of iterations needed. 245 capn := n 246 if n >= sign64 { 247 capn = sign64 248 } 249 i := 0 250 for d < capn { 251 d <<= 1 252 i++ 253 } 254 255 for ; i >= 0; i-- { 256 q <<= 1 257 if n >= d { 258 n -= d 259 q |= 1 260 } 261 d >>= 1 262 } 263 return q, n 264 } 265 266 // Floating point control word values. 267 // Bits 0-5 are bits to disable floating-point exceptions. 268 // Bits 8-9 are the precision control: 269 // 0 = single precision a.k.a. float32 270 // 2 = double precision a.k.a. float64 271 // Bits 10-11 are the rounding mode: 272 // 0 = round to nearest (even on a tie) 273 // 3 = round toward zero 274 var ( 275 controlWord64 uint16 = 0x3f + 2<<8 + 0<<10 276 controlWord64trunc uint16 = 0x3f + 2<<8 + 3<<10 277 )