3 ms·
acc = min(max(acc, -128), 127)?
by plmpsu 4y ago
acc = min(max(acc, -128), 127)?
- Someone 4y agoCommodore 64 basic doesn’t have min or max functions. DEF (https://www.c64-wiki.com/wiki/DEF https://www.c64-wiki.com/wiki/DEF) would be the way to do this, but I don’t see how to write a performant version in a single expression (you can’t use IF as an expression. You could cast to float, compute some specially chosen sigmoid function, and round to int, but that’s slow) A USR function (https://www.c64-wiki.com/wiki/USR https://www.c64-wiki.com/wiki/USR) would work, but that’s cheating.
- kbelder 4y agoAlmost seems like you could get there by bit twiddling, but I'm not sure. What's "-1 AND 127" in Commodore basic?
- Someone 4y ago127. I don’t think bit masking alone will get you there. You want to map anything over 0x80 to 0x7F. I think you can do something like x + (x < -127) * (x + 127) + (x > 127)) * (x - 127) ^^^^^^^^^^^^^^^^^^^^^^ can give you clamping, but haven’t checked it ((x < -127) is minus one for true or zero for false, so the part marked ^^^^ subtracts x + 127 from x when it is too low, and that’s equivalent to subtracting x (yielding zero) and subtracting -127 (yielding -127). The other part is similar, but for values that are too high. That expression can be put in a DEF FN.