Skip to content

Instantly share code, notes, and snippets.

@bjacob
Last active April 22, 2022 19:57
Show Gist options
  • Save bjacob/ac5beb3e94b8bf6680118ad75dfbe221 to your computer and use it in GitHub Desktop.
Save bjacob/ac5beb3e94b8bf6680118ad75dfbe221 to your computer and use it in GitHub Desktop.
// How to reverse-engineer 1010580540: notice that in hex it is 0x3C3C3C3C,
// and 0x3C = 60, so as Q31 fixedpoint it represents
// 60 * 2 * (1/256 + 1/256^2 + ...) = 120/255
const int32 Q31_constant_neg_120_over_255 = -1010580540 = -120 * (1LL << 31) / 255;
const int32 Q31_constant_180_over_255 = 1515870810 = 180 * (1LL << 31) / 255;
const int32 Q31_constant_1_over_4 = 536870912 = 1 * (1LL << 31) / 4;
int64_t mul_as_int64(int32_t a, int32_t b) {
return int64_t(a) * int64_t(b);
}
int32_t foo(tensor<int32_t> input_1133) {
tmp_1134 = input_1133 >> 12;
tmp_1135 = reduce_sum_over_inner_dimension(tmp_1134);
int32_t tmp_1136 = count_leading_zeros(tmp_1135);
// tmp_1138 is tmp_1135 shifted left as much as possible to not overflow int32.
int32_t tmp_1138 = tmp_1135 << (tmp_1136 - 1);
// In fixed-point: tmp_1139 = tmp_1138 * (-120/255)
tmp_1139 = mul_as_int64(tmp_1138, Q31_constant_neg_120_over_255) >> 31;
// In fixed-point: tmp_1140 = tmp_1138 * (-120/255) + (180/255)
tmp_1140 = tmp_1139 + Q31_constant_180_over_255;
// In fixed-point: tmp_1141 = tmp_1138^2 * (-120/255) + tmp_1138 * (180/255)
tmp_1141 = mul_as_int64(tmp_1140, tmp_1138) >> 31;
// In fixed-point: tmp_1142 = tmp_1138^2 * (120/255) + tmp_1138 * (-180/255) + (1/4)
tmp_1142 = Q31_constant_1_over_4 - tmp_1141;
// In fixed-point: tmp_1143 = (some polynomial of degree 3 in tmp_1138)
tmp_1143 = mul_as_int64(tmp_1140, tmp_1142) >> 31;
// In fixed-point: tmp_1144 = (some polynomial of degree 3 in tmp_1138)
tmp_1144 = 4 * tmp_1143;
// In fixed-point: tmp_1145 = (some polynomial of degree 3 in tmp_1138)
tmp_1145 = tmp_1144 + tmp_1140;
// In fixed-point: tmp_1146 = (some polynomial of degree 4 in tmp_1138)
tmp_1146 = mul_as_int64(tmp_1145, tmp_1138) >> 31;
// In fixed-point: tmp_1147 = (some polynomial of degree 4 in tmp_1138)
tmp_1147 = Q31_constant_1_over_4 - tmp_1146;
// In fixed-point: tmp_1148 = (some polynomial of degree 7 in tmp_1138)
tmp_1148 = mul_as_int64(tmp_1145, tmp_1147) >> 31;
// In fixed-point: tmp_1149 = (some polynomial of degree 7 in tmp_1138)
tmp_1149 = 4 * tmp_1148;
// In fixed-point: tmp_1150 = (some polynomial of degree 7 in tmp_1138)
tmp_1150 = tmp_1149 + tmp_1145;
// In fixed-point: tmp_1151 = (some polynomial of degree 8 in tmp_1138)
tmp_1151 = mul_as_int64(tmp_1150, tmp_1138) >> 31;
// In fixed-point: tmp_1152 = (some polynomial of degree 8 in tmp_1138)
tmp_1152 = Q31_constant_1_over_4 - tmp_1151;
// In fixed-point: tmp_1153 = (some polynomial of degree 15 in tmp_1138)
tmp_1153 = mul_as_int64(tmp_1152, tmp_1150) >> 31;
// In fixed-point: tmp_1154 = (some polynomial of degree 15 in tmp_1138)
tmp_1154 = 4 * tmp_1153;
// In fixed-point: tmp_1155 = (some polynomial of degree 15 in tmp_1138)
tmp_1155 = tmp_1154 + tmp_1150;
// In fixed-point: tmp_1156 = input_1133 * tmp_1155 * 2
tmp_1156 = mul_as_int64(tmp_1155, input_1133) >> 30
tmp_1157 = 35 - tmp_1136
// In fixed-point: tmp_1158 = input_1133 * tmp_1155 / 2^(tmp_1136 - 34)
tmp_1158 = tmp_1156 >> tmp_1157
// In fixed-point: tmp_1159 = input_1133 * tmp_1155 / 2^(tmp_1136 - 35)
tmp_1159 = tmp_1158 * 1073741824 >> 31
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment