14
2
Anyone who is moderately into low level code optimization know about the perils of branching, be it implemented as if-statements, loops or select-statements the possibility of a branch misprediction is a terrible clock-wasting thing.
Simple problems can be solved much better with simple arithmetic, so let's do that.
For the following problems all variables are 32 bit unsigned integers and the only allowed code is plain set statements involving only the following operators:
+ addition
- subtraction
* multiplication
/ integer division, rounds down, division by 0 not allowed
% modulo
& binary and
| binary or
^ binary exclusive or
>> bitshift right
<< bitshift left
Logic operators, return 1 if the expression is true and 0 if it is false.
== equal
!= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
Set operator
=
Every line must consist of a variable identifier followed by a set operator, followed by an expression.
An expression may not contain additional set operators, but may contain variable identifiers, literal numbers and parentheses.
The golfing score shall only count the number of operators.
Example:
myvar = ( ( ( foo + 5 ) * bar ) % 7 ) == 3
Has a score of 5 operators.
A solution may include as many variables as the author see fit.
Variables that have not been set have value 0
.
Overflow and underflow is allowed, all negative numbers underflow, so 3 - 5
is 4294967294
, even as part of a larger statement.
Task 1: Max
Two values, A
and B
, exist in the scope, make the RESULT
variable contain the largest of those values when the program terminates.
Task 2: Median
Three values, A
, B
and C
, exist in the scope, make the RESULT
variable contain the median of those values when the program terminates.
Task 3: Square root
One value, A
, exist in the scope, make the RESULT
variable contain the square root of A
, rounded down, when the program terminates.
It is ok to post an answer to only one or two of the questions, to some of you just finding valid solutions will be a challenge.
Where are unary operators? I don't care about
-
but~
could be nice (even if I don't know what for). – John Dvorak – 2013-09-11T20:59:33.350Sure,
0xFFFF_FFFF_FFFF_FFFF ^ x
and0 - x
. How could I have forgot? – John Dvorak – 2013-09-11T21:01:23.513@JanDvorak It made the shortest description, for completeness logic not
!
is also pretty trivial:x == 0
. – aaaaaaaaaaaa – 2013-09-11T21:15:32.967What is the behavior of division by zero? – John Dvorak – 2013-09-11T21:17:00.800
In Mathematica (a>b) returns True or False. Boole converts False to 0 and True to 1. Is it legal to use
Boole[a-b]
? – DavidC – 2013-09-11T22:48:00.573@DavidCarraher the task and the operations defined are language-independent. How you implement them is up to you. – John Dvorak – 2013-09-11T22:50:26.840
@JanDvorak Since division by zero on an X86 CPU cause the program to halt I'll have to go with division by zero being illegal. – aaaaaaaaaaaa – 2013-09-12T17:32:39.197