Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler_rt f128 support additions #1166

Merged
merged 10 commits into from Jun 30, 2018
Merged

compiler_rt f128 support additions #1166

merged 10 commits into from Jun 30, 2018

Conversation

tiehuis
Copy link
Member

@tiehuis tiehuis commented Jun 28, 2018

See #495.

Pending completion, but since nothing is blocking this anymore should be done in no time.

const aExp: u32 = aAbs >> srcSigBits;
const shift: u32 = srcExpBias - dstExpBias - aExp + 1;
const aExp = aAbs >> srcSigBits;
const shift = srcExpBias - dstExpBias - aExp + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: @truncate(u32, ...)? The exponent and the shift fit in 32 bits.

I put in the u32 intentionally because I figured that for performance reasons you don't want to use a 64 or 128 bits type when you can avoid it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add an @intCast(u32, ...) here. I omitted it here for the moment since the narrowing doesn't work implicitly for the longer src_t types (such as f128).

test__truncsfhf2(0x33000000, 0x0000); // 0x1.0p-25 -> zero
test__truncsfhf2(0x35800000, 0x0010); // denormal, 0x1.0p-20
test__truncsfhf2(0x33280000, 0x0001); // denormal, 0x1.5p-25 -> 0x1.0p-24
test__truncsfhf2(0x33000000, 0x0000); // 0x1.0p-25 -> zero
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a zig fmt thing? Lots of whitespace churn otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is from zig fmt.

@@ -0,0 +1,28 @@
const builtin = @import("builtin");
const is_test = builtin.is_test;
const std = @import("../../index.zig");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove this import and hardcode the mantissa bits where needed to avoid dragging in all of std on test.

Copy link
Contributor

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a full review, just a quick comment.


const mantissa_bits = std.math.floatMantissaBits(f128);
const exponent_bits = std.math.floatExponentBits(f128);
const exponent_bias = (1 << (exponent_bits - 1)) - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing a right shift? This looks like the max exponent, not the bias.

floatunsitf.zig, line 14: same question.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct since we halve the range. For example, a double with exponent bit count of 11 gives (1 << (11 - 1)) - 1 = 1023. Max exponent is (1 << 11) - 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're right. I don't remember what I was thinking at the time but the logic looks Obviously Correct to me now.


const mantissa_bits = std.math.floatMantissaBits(f128);
const exponent_bits = std.math.floatExponentBits(f128);
const exponent_bias = (1 << (exponent_bits - 1)) - 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're right. I don't remember what I was thinking at the time but the logic looks Obviously Correct to me now.

result += (@intCast(u128, exp) + exponent_bias) << mantissa_bits;

return @bitCast(f128, result);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this can be shared with floatunditf.zig?

},
FLT_MANT_DIG + 2 => {},
else => {
const shift_amt = @bitCast(i32, N +% (FLT_MANT_DIG + 2)) -% sd;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity: N + FLT_MANT_DIG + 2)? When I see %+, I immediately go scan for possible overflow inputs but there can't be any in this case, can it? I'd use wraparound operators sparingly.

Line 14: same comment. Also maybe line 32, 38, 43, etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. For the moment I used existing compiler_rt functions as a template and have done minimal changes to the actual structure. I'll do a more in-depth scan when complete and make these sorts of things more idiomatic/correct.

@tiehuis
Copy link
Member Author

tiehuis commented Jun 30, 2018

This is now complete, will merge when tests have run.

Few notes:

  • I haven't combined the float* and floatun* functions right now since it is a minor.
  • I've removed the wraparound arithmetic where not needed
  • The @bitCast alignment issues (unaligned bitcast causes segfault #1148) with u128/f128 did occur for a few cases. I've added explicit alignment where needed to resolve the issue on my end, but I expect it may be insufficient if LLVM does not lower the code in the same way in future versions, so this may need to be done for all bitCast's via a temporary value if it comes up again.

@bnoordhuis
Copy link
Contributor

@tiehuis I filed #1180 just now to (hopefully) fix the link errors.

@tiehuis
Copy link
Member Author

tiehuis commented Jun 30, 2018

@bnoordhuis

Great, thanks. I noticed that "fix" of mine didn't work so I'll revert it here and we can use the one from your PR instead.

@tiehuis tiehuis merged commit 887c977 into master Jun 30, 2018
@tiehuis tiehuis mentioned this pull request Jun 30, 2018
@tiehuis tiehuis deleted the issue-495 branch July 8, 2018 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants