| 1 | const std = @import("std"); |
| 2 | |
| 3 | const compiler_rt = @import("../compiler_rt.zig"); |
| 4 | const symbol = compiler_rt.symbol; |
| 5 | |
| 6 | comptime { |
| 7 | if (compiler_rt.want_aeabi) { |
| 8 | if (compiler_rt.gnu_f16_abi) { |
| 9 | symbol(&__aeabi_f2h, "__gnu_f2h_ieee"); |
| 10 | } else { |
| 11 | symbol(&__aeabi_f2h, "__aeabi_f2h"); |
| 12 | } |
| 13 | symbol(&__aeabi_d2h, "__aeabi_d2h"); |
| 14 | } else if (compiler_rt.gnu_f16_abi) { |
| 15 | symbol(&__truncsfhf2, "__gnu_f2h_ieee"); |
| 16 | } |
| 17 | symbol(&__truncsfhf2, "__truncsfhf2"); |
| 18 | symbol(&__truncdfhf2, "__truncdfhf2"); |
| 19 | symbol(&__truncxfhf2, "__truncxfhf2"); |
| 20 | if (compiler_rt.want_ppc_abi) { |
| 21 | symbol(&__trunctfhf2, "__trunckfhf2"); |
| 22 | } else { |
| 23 | symbol(&__trunctfhf2, "__trunctfhf2"); |
| 24 | } |
| 25 | |
| 26 | if (compiler_rt.want_aeabi) { |
| 27 | symbol(&__aeabi_d2f, "__aeabi_d2f"); |
| 28 | } else { |
| 29 | symbol(&__truncdfsf2, "__truncdfsf2"); |
| 30 | } |
| 31 | symbol(&__truncxfsf2, "__truncxfsf2"); |
| 32 | if (compiler_rt.want_ppc_abi) { |
| 33 | symbol(&__trunctfsf2, "__trunckfsf2"); |
| 34 | } else if (compiler_rt.want_sparc64_abi) { |
| 35 | symbol(&_Qp_qtos, "_Qp_qtos"); |
| 36 | } else if (compiler_rt.want_sparc32_abi) { |
| 37 | symbol(&__trunctfsf2, "_Q_qtos"); |
| 38 | } else { |
| 39 | symbol(&__trunctfsf2, "__trunctfsf2"); |
| 40 | } |
| 41 | |
| 42 | symbol(&__truncxfdf2, "__truncxfdf2"); |
| 43 | |
| 44 | if (compiler_rt.want_ppc_abi) { |
| 45 | symbol(&__trunctfdf2, "__trunckfdf2"); |
| 46 | } else if (compiler_rt.want_sparc64_abi) { |
| 47 | symbol(&_Qp_qtod, "_Qp_qtod"); |
| 48 | } else if (compiler_rt.want_sparc32_abi) { |
| 49 | symbol(&__trunctfdf2, "_Q_qtod"); |
| 50 | } else { |
| 51 | symbol(&__trunctfdf2, "__trunctfdf2"); |
| 52 | } |
| 53 | |
| 54 | if (compiler_rt.want_ppc_abi) { |
| 55 | symbol(&__trunctfxf2, "__trunckfxf2"); |
| 56 | } else { |
| 57 | symbol(&__trunctfxf2, "__trunctfxf2"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | fn __truncsfhf2(a: compiler_rt.f32.Abi) callconv(.c) compiler_rt.f16Conv(f32).Abi { |
| 62 | return compiler_rt.f16Conv(f32).toAbi(f16_floatCast_f32(compiler_rt.f32.fromAbi(a))); |
| 63 | } |
| 64 | fn __aeabi_f2h(a: u32) callconv(.{ .arm_aapcs = .{} }) u16 { |
| 65 | return @bitCast(f16_floatCast_f32(@bitCast(a))); |
| 66 | } |
| 67 | pub fn f16_floatCast_f32(a: f32) f16 { |
| 68 | return truncf(f16, f32, a); |
| 69 | } |
| 70 | |
| 71 | fn __truncdfhf2(a: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f16Conv(f64).Abi { |
| 72 | return compiler_rt.f16Conv(f64).toAbi(f16_floatCast_f64(compiler_rt.f64.fromAbi(a))); |
| 73 | } |
| 74 | fn __aeabi_d2h(a: u64) callconv(.{ .arm_aapcs = .{} }) u16 { |
| 75 | return @bitCast(f16_floatCast_f64(@bitCast(a))); |
| 76 | } |
| 77 | pub fn f16_floatCast_f64(a: f64) f16 { |
| 78 | return truncf(f16, f64, a); |
| 79 | } |
| 80 | |
| 81 | fn __truncxfhf2(a: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f16Conv(f80).Abi { |
| 82 | return compiler_rt.f16Conv(f80).toAbi(f16_floatCast_f80(compiler_rt.f80.fromAbi(a))); |
| 83 | } |
| 84 | pub fn f16_floatCast_f80(a: f80) f16 { |
| 85 | return trunc_f80(f16, a); |
| 86 | } |
| 87 | |
| 88 | fn __trunctfhf2(a: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f16Conv(f128).Abi { |
| 89 | return compiler_rt.f16Conv(f128).toAbi(f16_floatCast_f128(compiler_rt.f128.fromAbi(a))); |
| 90 | } |
| 91 | pub fn f16_floatCast_f128(a: f128) f16 { |
| 92 | return truncf(f16, f128, a); |
| 93 | } |
| 94 | |
| 95 | fn __truncdfsf2(a: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f32.Abi { |
| 96 | return compiler_rt.f32.toAbi(f32_floatCast_f64(compiler_rt.f64.fromAbi(a))); |
| 97 | } |
| 98 | fn __aeabi_d2f(a: f64) callconv(.{ .arm_aapcs = .{} }) f32 { |
| 99 | return f32_floatCast_f64(a); |
| 100 | } |
| 101 | pub fn f32_floatCast_f64(a: f64) f32 { |
| 102 | return truncf(f32, f64, a); |
| 103 | } |
| 104 | |
| 105 | fn __truncxfsf2(a: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f32.Abi { |
| 106 | return compiler_rt.f32.toAbi(f32_floatCast_f80(compiler_rt.f80.fromAbi(a))); |
| 107 | } |
| 108 | pub fn f32_floatCast_f80(a: f80) f32 { |
| 109 | return trunc_f80(f32, a); |
| 110 | } |
| 111 | |
| 112 | fn __trunctfsf2(a: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f32.Abi { |
| 113 | return compiler_rt.f32.toAbi(f32_floatCast_f128(compiler_rt.f128.fromAbi(a))); |
| 114 | } |
| 115 | fn _Qp_qtos(a: *const f128) callconv(.c) f32 { |
| 116 | return f32_floatCast_f128(a.*); |
| 117 | } |
| 118 | pub fn f32_floatCast_f128(a: f128) f32 { |
| 119 | return truncf(f32, f128, a); |
| 120 | } |
| 121 | |
| 122 | fn __truncxfdf2(a: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f64.Abi { |
| 123 | return compiler_rt.f64.toAbi(f64_floatCast_f80(compiler_rt.f80.fromAbi(a))); |
| 124 | } |
| 125 | pub fn f64_floatCast_f80(a: f80) f64 { |
| 126 | return trunc_f80(f64, a); |
| 127 | } |
| 128 | |
| 129 | fn __trunctfdf2(a: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f64.Abi { |
| 130 | return compiler_rt.f64.toAbi(f64_floatCast_f128(compiler_rt.f128.fromAbi(a))); |
| 131 | } |
| 132 | fn _Qp_qtod(a: *const f128) callconv(.c) f64 { |
| 133 | return f64_floatCast_f128(a.*); |
| 134 | } |
| 135 | pub fn f64_floatCast_f128(a: f128) f64 { |
| 136 | return truncf(f64, f128, a); |
| 137 | } |
| 138 | |
| 139 | fn __trunctfxf2(a: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f80.Abi { |
| 140 | return compiler_rt.f80.toAbi(f80_floatCast_f128(compiler_rt.f128.fromAbi(a))); |
| 141 | } |
| 142 | pub fn f80_floatCast_f128(a: f128) f80 { |
| 143 | const src_sig_bits = std.math.floatMantissaBits(f128); |
| 144 | const dst_sig_bits = std.math.floatMantissaBits(f80) - 1; // -1 for the integer bit |
| 145 | |
| 146 | // Various constants whose values follow from the type parameters. |
| 147 | // Any reasonable optimizer will fold and propagate all of these. |
| 148 | const src_bits = @typeInfo(f128).float.bits; |
| 149 | const src_exp_bits = src_bits - src_sig_bits - 1; |
| 150 | const src_inf_exp = 0x7FFF; |
| 151 | |
| 152 | const src_inf = src_inf_exp << src_sig_bits; |
| 153 | const src_sign_mask = 1 << (src_sig_bits + src_exp_bits); |
| 154 | const src_abs_mask = src_sign_mask - 1; |
| 155 | const round_mask = (1 << (src_sig_bits - dst_sig_bits)) - 1; |
| 156 | const halfway = 1 << (src_sig_bits - dst_sig_bits - 1); |
| 157 | |
| 158 | // Break a into a sign and representation of the absolute value |
| 159 | const a_rep: u128 = @bitCast(a); |
| 160 | const a_abs = a_rep & src_abs_mask; |
| 161 | const sign: u16 = if (a_rep & src_sign_mask != 0) 0x8000 else 0; |
| 162 | const integer_bit = 1 << 63; |
| 163 | |
| 164 | var res: std.math.F80 = undefined; |
| 165 | |
| 166 | if (a_abs > src_inf) { |
| 167 | // a is NaN. |
| 168 | // Conjure the result by beginning with infinity, setting the qNaN |
| 169 | // bit and inserting the (truncated) trailing NaN field. |
| 170 | res.exp = 0x7fff; |
| 171 | res.fraction = 0x8000000000000000; |
| 172 | res.fraction |= @as(u64, @truncate(a_abs >> (src_sig_bits - dst_sig_bits))); |
| 173 | } else { |
| 174 | // The exponent of a is within the range of normal numbers in the |
| 175 | // destination format. We can convert by simply right-shifting with |
| 176 | // rounding, adding the explicit integer bit, and adjusting the exponent |
| 177 | res.fraction = @as(u64, @truncate(a_abs >> (src_sig_bits - dst_sig_bits))) | integer_bit; |
| 178 | res.exp = @truncate(a_abs >> src_sig_bits); |
| 179 | |
| 180 | const round_bits = a_abs & round_mask; |
| 181 | if (round_bits > halfway) { |
| 182 | // Round to nearest |
| 183 | const ov = @addWithOverflow(res.fraction, 1); |
| 184 | res.fraction = ov[0]; |
| 185 | res.exp += ov[1]; |
| 186 | res.fraction |= @as(u64, ov[1]) << 63; // Restore integer bit after carry |
| 187 | } else if (round_bits == halfway) { |
| 188 | // Ties to even |
| 189 | const ov = @addWithOverflow(res.fraction, res.fraction & 1); |
| 190 | res.fraction = ov[0]; |
| 191 | res.exp += ov[1]; |
| 192 | res.fraction |= @as(u64, ov[1]) << 63; // Restore integer bit after carry |
| 193 | } |
| 194 | if (res.exp == 0) res.fraction &= ~@as(u64, integer_bit); // Remove integer bit for de-normals |
| 195 | } |
| 196 | |
| 197 | res.exp |= sign; |
| 198 | return res.toFloat(); |
| 199 | } |
| 200 | |
| 201 | inline fn truncf(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t { |
| 202 | const src_rep_t = @Int(.unsigned, @typeInfo(src_t).float.bits); |
| 203 | const dst_rep_t = @Int(.unsigned, @typeInfo(dst_t).float.bits); |
| 204 | const srcSigBits = std.math.floatMantissaBits(src_t); |
| 205 | const dstSigBits = std.math.floatMantissaBits(dst_t); |
| 206 | |
| 207 | // Various constants whose values follow from the type parameters. |
| 208 | // Any reasonable optimizer will fold and propagate all of these. |
| 209 | const srcBits = @typeInfo(src_t).float.bits; |
| 210 | const srcExpBits = srcBits - srcSigBits - 1; |
| 211 | const srcInfExp = (1 << srcExpBits) - 1; |
| 212 | const srcExpBias = srcInfExp >> 1; |
| 213 | |
| 214 | const srcMinNormal = 1 << srcSigBits; |
| 215 | const srcSignificandMask = srcMinNormal - 1; |
| 216 | const srcInfinity = srcInfExp << srcSigBits; |
| 217 | const srcSignMask = 1 << (srcSigBits + srcExpBits); |
| 218 | const srcAbsMask = srcSignMask - 1; |
| 219 | const roundMask = (1 << (srcSigBits - dstSigBits)) - 1; |
| 220 | const halfway = 1 << (srcSigBits - dstSigBits - 1); |
| 221 | const srcQNaN = 1 << (srcSigBits - 1); |
| 222 | const srcNaNCode = srcQNaN - 1; |
| 223 | |
| 224 | const dstBits = @typeInfo(dst_t).float.bits; |
| 225 | const dstExpBits = dstBits - dstSigBits - 1; |
| 226 | const dstInfExp = (1 << dstExpBits) - 1; |
| 227 | const dstExpBias = dstInfExp >> 1; |
| 228 | |
| 229 | const underflowExponent = srcExpBias + 1 - dstExpBias; |
| 230 | const overflowExponent = srcExpBias + dstInfExp - dstExpBias; |
| 231 | const underflow = underflowExponent << srcSigBits; |
| 232 | const overflow = overflowExponent << srcSigBits; |
| 233 | |
| 234 | const dstQNaN = 1 << (dstSigBits - 1); |
| 235 | const dstNaNCode = dstQNaN - 1; |
| 236 | |
| 237 | // Break a into a sign and representation of the absolute value |
| 238 | const aRep: src_rep_t = @bitCast(a); |
| 239 | const aAbs: src_rep_t = aRep & srcAbsMask; |
| 240 | const sign: src_rep_t = aRep & srcSignMask; |
| 241 | var absResult: dst_rep_t = undefined; |
| 242 | |
| 243 | if (aAbs -% underflow < aAbs -% overflow) { |
| 244 | // The exponent of a is within the range of normal numbers in the |
| 245 | // destination format. We can convert by simply right-shifting with |
| 246 | // rounding and adjusting the exponent. |
| 247 | absResult = @truncate(aAbs >> (srcSigBits - dstSigBits)); |
| 248 | absResult -%= @as(dst_rep_t, srcExpBias - dstExpBias) << dstSigBits; |
| 249 | |
| 250 | const roundBits: src_rep_t = aAbs & roundMask; |
| 251 | if (roundBits > halfway) { |
| 252 | // Round to nearest |
| 253 | absResult += 1; |
| 254 | } else if (roundBits == halfway) { |
| 255 | // Ties to even |
| 256 | absResult += absResult & 1; |
| 257 | } |
| 258 | } else if (aAbs > srcInfinity) { |
| 259 | // a is NaN. |
| 260 | // Conjure the result by beginning with infinity, setting the qNaN |
| 261 | // bit and inserting the (truncated) trailing NaN field. |
| 262 | absResult = @as(dst_rep_t, @intCast(dstInfExp)) << dstSigBits; |
| 263 | absResult |= dstQNaN; |
| 264 | absResult |= @intCast(((aAbs & srcNaNCode) >> (srcSigBits - dstSigBits)) & dstNaNCode); |
| 265 | } else if (aAbs >= overflow) { |
| 266 | // a overflows to infinity. |
| 267 | absResult = @as(dst_rep_t, @intCast(dstInfExp)) << dstSigBits; |
| 268 | } else { |
| 269 | // a underflows on conversion to the destination type or is an exact |
| 270 | // zero. The result may be a denormal or zero. Extract the exponent |
| 271 | // to get the shift amount for the denormalization. |
| 272 | const aExp: u32 = @intCast(aAbs >> srcSigBits); |
| 273 | const shift: u32 = @intCast(srcExpBias - dstExpBias - aExp + 1); |
| 274 | |
| 275 | const significand: src_rep_t = (aRep & srcSignificandMask) | srcMinNormal; |
| 276 | |
| 277 | // Right shift by the denormalization amount with sticky. |
| 278 | if (shift > srcSigBits) { |
| 279 | absResult = 0; |
| 280 | } else { |
| 281 | const sticky: src_rep_t = @intFromBool(significand << @intCast(srcBits - shift) != 0); |
| 282 | const denormalizedSignificand: src_rep_t = significand >> @intCast(shift) | sticky; |
| 283 | absResult = @intCast(denormalizedSignificand >> (srcSigBits - dstSigBits)); |
| 284 | const roundBits: src_rep_t = denormalizedSignificand & roundMask; |
| 285 | if (roundBits > halfway) { |
| 286 | // Round to nearest |
| 287 | absResult += 1; |
| 288 | } else if (roundBits == halfway) { |
| 289 | // Ties to even |
| 290 | absResult += absResult & 1; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | const result: dst_rep_t align(@alignOf(dst_t)) = absResult | |
| 296 | @as(dst_rep_t, @truncate(sign >> @intCast(srcBits - dstBits))); |
| 297 | return @bitCast(result); |
| 298 | } |
| 299 | |
| 300 | inline fn trunc_f80(comptime dst_t: type, a: f80) dst_t { |
| 301 | const dst_rep_t = @Int(.unsigned, @typeInfo(dst_t).float.bits); |
| 302 | const src_sig_bits = std.math.floatMantissaBits(f80) - 1; // -1 for the integer bit |
| 303 | const dst_sig_bits = std.math.floatMantissaBits(dst_t); |
| 304 | |
| 305 | const src_exp_bias = 16383; |
| 306 | |
| 307 | const round_mask = (1 << (src_sig_bits - dst_sig_bits)) - 1; |
| 308 | const halfway = 1 << (src_sig_bits - dst_sig_bits - 1); |
| 309 | |
| 310 | const dst_bits = @typeInfo(dst_t).float.bits; |
| 311 | const dst_exp_bits = dst_bits - dst_sig_bits - 1; |
| 312 | const dst_inf_exp = (1 << dst_exp_bits) - 1; |
| 313 | const dst_exp_bias = dst_inf_exp >> 1; |
| 314 | |
| 315 | const underflow = src_exp_bias + 1 - dst_exp_bias; |
| 316 | const overflow = src_exp_bias + dst_inf_exp - dst_exp_bias; |
| 317 | |
| 318 | const dst_qnan = 1 << (dst_sig_bits - 1); |
| 319 | const dst_nan_mask = dst_qnan - 1; |
| 320 | |
| 321 | // Break a into a sign and representation of the absolute value |
| 322 | var a_rep = std.math.F80.fromFloat(a); |
| 323 | const sign = a_rep.exp & 0x8000; |
| 324 | a_rep.exp &= 0x7FFF; |
| 325 | a_rep.fraction &= 0x7FFFFFFFFFFFFFFF; |
| 326 | var abs_result: dst_rep_t = undefined; |
| 327 | |
| 328 | if (a_rep.exp -% underflow < a_rep.exp -% overflow) { |
| 329 | // The exponent of a is within the range of normal numbers in the |
| 330 | // destination format. We can convert by simply right-shifting with |
| 331 | // rounding and adjusting the exponent. |
| 332 | abs_result = @as(dst_rep_t, a_rep.exp) << dst_sig_bits; |
| 333 | abs_result |= @truncate(a_rep.fraction >> (src_sig_bits - dst_sig_bits)); |
| 334 | abs_result -%= @as(dst_rep_t, src_exp_bias - dst_exp_bias) << dst_sig_bits; |
| 335 | |
| 336 | const round_bits = a_rep.fraction & round_mask; |
| 337 | if (round_bits > halfway) { |
| 338 | // Round to nearest |
| 339 | abs_result += 1; |
| 340 | } else if (round_bits == halfway) { |
| 341 | // Ties to even |
| 342 | abs_result += abs_result & 1; |
| 343 | } |
| 344 | } else if (a_rep.exp == 0x7FFF and a_rep.fraction != 0) { |
| 345 | // a is NaN. |
| 346 | // Conjure the result by beginning with infinity, setting the qNaN |
| 347 | // bit and inserting the (truncated) trailing NaN field. |
| 348 | abs_result = @as(dst_rep_t, @intCast(dst_inf_exp)) << dst_sig_bits; |
| 349 | abs_result |= dst_qnan; |
| 350 | abs_result |= @intCast((a_rep.fraction >> (src_sig_bits - dst_sig_bits)) & dst_nan_mask); |
| 351 | } else if (a_rep.exp >= overflow) { |
| 352 | // a overflows to infinity. |
| 353 | abs_result = @as(dst_rep_t, @intCast(dst_inf_exp)) << dst_sig_bits; |
| 354 | } else { |
| 355 | // a underflows on conversion to the destination type or is an exact |
| 356 | // zero. The result may be a denormal or zero. Extract the exponent |
| 357 | // to get the shift amount for the denormalization. |
| 358 | const shift = src_exp_bias - dst_exp_bias - a_rep.exp; |
| 359 | |
| 360 | // Right shift by the denormalization amount with sticky. |
| 361 | if (shift > src_sig_bits) { |
| 362 | abs_result = 0; |
| 363 | } else { |
| 364 | const sticky = @intFromBool(a_rep.fraction << @intCast(shift) != 0); |
| 365 | const denormalized_significand = a_rep.fraction >> @intCast(shift) | sticky; |
| 366 | abs_result = @intCast(denormalized_significand >> (src_sig_bits - dst_sig_bits)); |
| 367 | const round_bits = denormalized_significand & round_mask; |
| 368 | if (round_bits > halfway) { |
| 369 | // Round to nearest |
| 370 | abs_result += 1; |
| 371 | } else if (round_bits == halfway) { |
| 372 | // Ties to even |
| 373 | abs_result += abs_result & 1; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | const result align(@alignOf(dst_t)) = abs_result | @as(dst_rep_t, sign) << dst_bits - 16; |
| 379 | return @bitCast(result); |
| 380 | } |
| 381 | |
| 382 | test { |
| 383 | _ = @import("truncf_test.zig"); |
| 384 | } |