blob: 44aea1a10989c6eabcd6a690b59db22ee365ead4 [file] [log] [blame]
// Copyright (c) Facebook, Inc. and its affiliates.
// All rights reserved.
//
// Copyright 2019 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include <stddef.h>
#include <fp16/bitcasts.h>
#include <xnnpack/requantization-stubs.h>
void xnn_qs8_requantize_fp32__scalar_magic(
size_t n,
const int32_t* input,
float scale,
int8_t zero_point,
int8_t qmin,
int8_t qmax,
int8_t* output)
{
assert(n % 4 == 0);
assert(scale < 1.0f);
assert(scale >= 0x1.0p-32f);
const float fmin = (float) ((int32_t) qmin - (int32_t) zero_point);
const float fmax = (float) ((int32_t) qmax - (int32_t) zero_point);
const float fmagic = 12582912.0f;
const int32_t imagic = INT32_C(0x4B400000) - (int32_t) zero_point;
for (; n != 0; n -= 4) {
const int32_t x = input[0];
const int32_t y = input[1];
const int32_t z = input[2];
const int32_t w = input[3];
input += 4;
const float x_scaled = (float) x * scale;
const float y_scaled = (float) y * scale;
const float z_scaled = (float) z * scale;
const float w_scaled = (float) w * scale;
const float x_clamped = x_scaled < fmin ? fmin : x_scaled > fmax ? fmax : x_scaled;
const float y_clamped = y_scaled < fmin ? fmin : y_scaled > fmax ? fmax : y_scaled;
const float z_clamped = z_scaled < fmin ? fmin : z_scaled > fmax ? fmax : z_scaled;
const float w_clamped = w_scaled < fmin ? fmin : w_scaled > fmax ? fmax : w_scaled;
const int32_t x_biased = (int32_t) fp32_to_bits(x_clamped + fmagic) - imagic;
const int32_t y_biased = (int32_t) fp32_to_bits(y_clamped + fmagic) - imagic;
const int32_t z_biased = (int32_t) fp32_to_bits(z_clamped + fmagic) - imagic;
const int32_t w_biased = (int32_t) fp32_to_bits(w_clamped + fmagic) - imagic;
output[0] = (int8_t) x_biased;
output[1] = (int8_t) y_biased;
output[2] = (int8_t) z_biased;
output[3] = (int8_t) w_biased;
output += 4;
}
}