{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/38034","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/38034/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/38034/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/38034/events","html_url":"https://github.com/rust-lang/rust/pull/38034","id":191878765,"number":38034,"title":"Optimize signed integer saturating_mul","user":{"login":"xfix","id":1297598,"avatar_url":"https://avatars.githubusercontent.com/u/1297598?v=3","gravatar_id":"","url":"https://api.github.com/users/xfix","html_url":"https://github.com/xfix","followers_url":"https://api.github.com/users/xfix/followers","following_url":"https://api.github.com/users/xfix/following{/other_user}","gists_url":"https://api.github.com/users/xfix/gists{/gist_id}","starred_url":"https://api.github.com/users/xfix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xfix/subscriptions","organizations_url":"https://api.github.com/users/xfix/orgs","repos_url":"https://api.github.com/users/xfix/repos","events_url":"https://api.github.com/users/xfix/events{/privacy}","received_events_url":"https://api.github.com/users/xfix/received_events","type":"User","site_admin":false},"labels":[{"id":211668062,"url":"https://api.github.com/repos/rust-lang/rust/labels/T-libs","name":"T-libs","color":"bfd4f2","default":false}],"state":"open","locked":false,"assignee":{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false},"assignees":[{"login":"sfackler","id":1455697,"avatar_url":"https://avatars.githubusercontent.com/u/1455697?v=3","gravatar_id":"","url":"https://api.github.com/users/sfackler","html_url":"https://github.com/sfackler","followers_url":"https://api.github.com/users/sfackler/followers","following_url":"https://api.github.com/users/sfackler/following{/other_user}","gists_url":"https://api.github.com/users/sfackler/gists{/gist_id}","starred_url":"https://api.github.com/users/sfackler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sfackler/subscriptions","organizations_url":"https://api.github.com/users/sfackler/orgs","repos_url":"https://api.github.com/users/sfackler/repos","events_url":"https://api.github.com/users/sfackler/events{/privacy}","received_events_url":"https://api.github.com/users/sfackler/received_events","type":"User","site_admin":false}],"milestone":null,"comments":3,"created_at":"2016-11-27T19:14:36Z","updated_at":"2016-11-27T22:38:09Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/38034","html_url":"https://github.com/rust-lang/rust/pull/38034","diff_url":"https://github.com/rust-lang/rust/pull/38034.diff","patch_url":"https://github.com/rust-lang/rust/pull/38034.patch"},"body":"Previous version of saturating_mul was poorly optimized by LLVM, and had lots of branches (which are slow if CPU mispredicts them). That also had a side-effect of needlessly making code larger.\r\n\r\n```asm\r\n    mov     eax, edi\r\n    imul    eax, esi\r\n    jno     .LBB1_6\r\n    mov     eax, -2147483648\r\n    test    edi, edi\r\n    js      .LBB1_4\r\n    je      .LBB1_6\r\n    test    esi, esi\r\n    jg      .LBB1_5\r\n    jmp     .LBB1_6\r\n.LBB1_4:\r\n    test    esi, esi\r\n    jns     .LBB1_6\r\n.LBB1_5:\r\n    mov     eax, 2147483647\r\n.LBB1_6:\r\n    ret\r\n```\r\n\r\nHowever, it's possible to have branch-less saturating integer multiplication by using the fact that sign of exclusive xor is the same as infinite precision multiplication. This simplifies a complex number comparison function into a one simple test that can be easily implemented in assembly as a conditional move.\r\n\r\nThis assembly does also use the fact that minimal value is just one bigger than maximal value in signed representation. This wasn't done in the code, because LLVM can optimize that.\r\n\r\n```asm\r\n    mov     eax, esi\r\n    xor     eax, edi\r\n    shr     eax, 31\r\n    add     eax, 2147483647\r\n    imul    edi, esi\r\n    cmovo   edi, eax\r\n    mov     eax, edi\r\n    ret\r\n```"},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/263152989","html_url":"https://github.com/rust-lang/rust/pull/38034#issuecomment-263152989","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/38034","id":263152989,"user":{"login":"bluss","id":3209739,"avatar_url":"https://avatars.githubusercontent.com/u/3209739?v=3","gravatar_id":"","url":"https://api.github.com/users/bluss","html_url":"https://github.com/bluss","followers_url":"https://api.github.com/users/bluss/followers","following_url":"https://api.github.com/users/bluss/following{/other_user}","gists_url":"https://api.github.com/users/bluss/gists{/gist_id}","starred_url":"https://api.github.com/users/bluss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluss/subscriptions","organizations_url":"https://api.github.com/users/bluss/orgs","repos_url":"https://api.github.com/users/bluss/repos","events_url":"https://api.github.com/users/bluss/events{/privacy}","received_events_url":"https://api.github.com/users/bluss/received_events","type":"User","site_admin":false},"created_at":"2016-11-27T22:38:09Z","updated_at":"2016-11-27T22:38:09Z","body":"looks good"}}
{"actor":{"display_login":"bluss"}}