Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

Implement copysign for shaders#158

Merged
mergify[bot] merged 3 commits intomainfrom
copysign
Oct 27, 2020
Merged

Implement copysign for shaders#158
mergify[bot] merged 3 commits intomainfrom
copysign

Conversation

@khyperia
Copy link
Copy Markdown
Contributor

Fixes #148

@khyperia khyperia requested a review from repi October 27, 2020 12:43
Copy link
Copy Markdown
Contributor

@repi repi left a comment

Choose a reason for hiding this comment

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

Yay!

@repi
Copy link
Copy Markdown
Contributor

repi commented Oct 27, 2020

Would be good to have tests for these type of stuffs, later on :)

} else {
self.fatal("TODO: Shader copysign not supported yet https://github.com/EmbarkStudios/rust-gpu/issues/148")
let arg0 = args[0].immediate();
let arg1 = args[1].immediate();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be more clear if you use sign, sign_val, sign_src or something like that instead of arg1.

),
_ => panic!("copysign not supported for width {}", width),
};
let arg0_int = self.bitcast(arg0, int_ty);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
let arg0_int = self.bitcast(arg0, int_ty);
let arg0_bits = self.bitcast(arg0, int_ty);

};
let arg0_int = self.bitcast(arg0, int_ty);
let arg1_int = self.bitcast(arg1, int_ty);
let arg0_and = self.and(arg0_int, mask_value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
let arg0_and = self.and(arg0_int, mask_value);
let arg0_masked = self.and(arg0_int, mask_value);

let arg1 = args[1].immediate();
let width = match self.lookup_type(arg0.ty) {
SpirvType::Float(width) => width,
other => panic!(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is panicking appropriate here? Shouldn't this produce a compile error?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Typeck should already ensure that only floats get passed here. Using unreachable! or bug! makes sense here.

Copy link
Copy Markdown
Contributor Author

@khyperia khyperia Oct 27, 2020

Choose a reason for hiding this comment

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

Ah yeah, I guess I should have used the bug! macro - it's a bug/hard-panic, though, because it's breaking the invariants of what is known today (which is that the arguments to the copysignf32 and copysignf64 functions are floats). Similar with sizes that aren't 32 or 64 bits.

Edit: I guess some relevant context is that rustc_codegen_llvm hits some far worse crashes if intrinsics don't line up, e.g. generating straight-up malformed IL.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@khyperia
Copy link
Copy Markdown
Contributor Author

Would be good to have tests for these type of stuffs, later on :)

Yeah! I did smoke-test it real quick, but proper tests would be nice. Also oh my goodness, I spent 5 minutes bending my brain trying to think of what x = copysign(x, y) would do for a xy=-1..1 red/green fullscreen quad, until I realized that example-runner-cpu uses the native copysign implementation and I can just compare against that!

@mergify mergify bot merged commit 5b538ee into main Oct 27, 2020
@mergify mergify bot deleted the copysign branch October 27, 2020 13:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement copysignf32

4 participants