Jorg Brown
586952f4ce
Optimize std::midpoint for integers
...
Same idea as the current algorithm, that is, add (half of the difference between a and b) to a.
But we use a different technique for computing the difference: we compute b - a into a pair of integers that are named "sign_bit" and "diff". We have to use a pair because subtracting two 32-bit integers produces a 33-bit result.
Computing half of that is a simple matter of shifting diff right by 1, and adding sign_bit shifted left by 31. llvm knows how to do that with one instruction: shld.
The only tricky part is that if the difference is odd and negative, then shifting it by one isn't the same as dividing it by two - shifting a negative one produces a negative one, for example. So there's one more adjustment: if the sign bit and the low bit of diff are one, we add one.
For a demonstration of the codegen difference, see https://godbolt.org/z/7ar3K9 , which also has a built-in test.
Differential Revision: https://reviews.llvm.org/D69459
2019-11-04 19:00:23 -08:00
..
2019-10-23 11:19:19 -07:00
2019-10-23 11:19:19 -07:00
2019-10-23 11:19:19 -07:00
2019-07-12 01:01:55 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-10-19 10:59:23 +00:00
2019-08-05 18:29:14 +00:00
2019-10-23 08:08:57 -07:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-09-26 14:51:10 +00:00
2019-01-19 10:56:40 +00:00
2019-07-12 01:01:55 +00:00
2019-07-18 11:51:05 +00:00
2019-08-20 10:19:55 +00:00
2019-09-26 14:51:10 +00:00
2019-04-24 09:43:44 +00:00
2019-01-19 10:56:40 +00:00
2019-08-12 07:51:05 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-10-23 11:19:19 -07:00
2019-10-23 11:19:19 -07:00
2019-07-11 23:13:38 +00:00
2019-09-30 20:55:30 +00:00
2019-01-19 08:50:56 +00:00
2019-08-20 22:23:35 +00:00
2019-06-19 16:33:28 +00:00
2019-04-01 16:39:34 +00:00
2019-09-04 12:44:19 +00:00
2019-10-23 11:19:19 -07:00
2019-07-01 19:59:34 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-06-13 22:27:24 +00:00
2019-09-23 06:16:41 +00:00
2019-01-19 08:50:56 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-10-21 23:38:32 +00:00
2019-10-01 12:12:21 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-05-29 16:01:36 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-09-07 22:18:20 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 08:50:56 +00:00
2019-01-19 10:56:40 +00:00
2019-10-23 11:19:19 -07:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-08-12 07:51:05 +00:00
2019-01-19 10:56:40 +00:00
2019-06-21 15:20:55 +00:00
2019-08-06 21:11:24 +00:00
2019-03-29 16:03:57 +00:00
2019-08-13 15:02:53 +00:00
2019-01-19 10:56:40 +00:00
2019-10-23 11:19:19 -07:00
2019-09-16 19:26:41 +00:00
2019-08-04 07:13:43 +00:00
2019-06-21 13:56:13 +00:00
2019-05-29 16:01:36 +00:00
2019-06-20 15:36:32 +00:00
2019-05-29 16:01:36 +00:00
2019-05-29 16:01:36 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-08-20 18:21:06 +00:00
2019-08-04 07:13:43 +00:00
2019-07-01 16:13:31 +00:00
2019-01-19 10:56:40 +00:00
2019-10-23 11:19:19 -07:00
2019-05-29 16:01:36 +00:00
2019-01-19 10:56:40 +00:00
2019-07-19 17:13:39 +00:00
2019-09-04 13:35:03 +00:00
2019-10-22 15:16:49 +00:00
2019-08-05 18:29:14 +00:00
2019-09-26 14:51:10 +00:00
2019-09-26 14:51:10 +00:00
2019-11-04 19:00:23 -08:00
2019-06-27 18:40:55 +00:00
2019-09-25 18:56:54 +00:00
2019-05-29 16:01:36 +00:00
2019-08-20 15:39:20 +00:00
2019-01-19 10:56:40 +00:00
2019-09-25 16:40:30 +00:00
2019-01-19 10:56:40 +00:00
2019-07-19 17:13:39 +00:00
2019-01-19 10:56:40 +00:00
2019-08-04 07:13:43 +00:00
2019-04-01 16:39:34 +00:00
2019-02-01 21:59:27 +00:00
2019-05-29 16:01:36 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-03-06 20:31:57 +00:00
2019-06-20 15:36:32 +00:00
2019-01-19 10:56:40 +00:00
2019-04-23 18:01:58 +00:00
2019-01-19 10:56:40 +00:00
2019-10-09 03:07:02 +00:00
2019-09-25 18:56:54 +00:00
2019-01-19 08:50:56 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00
2019-08-14 16:21:27 +00:00
2019-10-28 18:04:41 -07:00
2019-10-30 15:52:11 -07:00
2019-01-19 10:56:40 +00:00
2019-05-29 02:38:19 +00:00
2019-07-16 03:21:01 +00:00
2019-07-16 03:21:01 +00:00
2019-09-30 20:55:30 +00:00
2019-04-25 21:31:58 +00:00
2019-07-14 21:29:39 +00:00
2019-09-13 16:09:33 +00:00
2019-05-23 23:46:44 +00:00
2019-01-19 10:56:40 +00:00
2019-01-19 10:56:40 +00:00