aboutsummaryrefslogtreecommitdiff
path: root/pseudocode/operators/RESCALE.tosac
blob: 0a3ce8d854243dc3f6b81b06b71202d9b8f3e052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// This confidential and proprietary software may be used only as
// authorised by a licensing agreement from ARM Limited
// (C) COPYRIGHT 2020-2024 ARM Limited
// ALL RIGHTS RESERVED
// The entire notice above must be reproduced on all authorised
// copies and copies may only be made to the extent permitted
// by a licensing agreement from ARM Limited.

for_each(index in shape) {
    // uint16 values can have zero_point 0 or 32768
    // int8/uint8 can have zero point within their valid range
    // No other types can have zero point != 0
    ERROR_IF(in_t != i8_t &&
             (in_t != i16_t || input_unsigned == false) && input_zp != 0);
    ERROR_IF(out_t != i8_t &&
             (out_t != i16_t || output_unsigned == false) && output_zp != 0);
    ERROR_IF(in_t == i16_t && input_unsigned == true && input_zp != 0 && input_zp != 32768);
    ERROR_IF(out_t == i16_t && output_unsigned == true && output_zp != 0 && output_zp != 32768);
    ERROR_IF(scale32 && in_t == i48_t);
    ERROR_IF(!scale32 && double_round);
    ERROR_IF(input_unsigned && output_unsigned);
    ERROR_IF(out_t == i32_t && input_unsigned);
    ERROR_IF(in_t == i32_t && output_unsigned);
    ERROR_IF(in_t == i48_t && output_unsigned);

    in_t in_value = tensor_read<in_t>(input, shape, index);

    int48_t value, extended_in_zp;
    if (input_unsigned) {
        value = zero_extend<int48_t>(in_value);
        extended_in_zp = zero_extend<int48_t>(input_zp);
    }
    else {
        value = sign_extend<int48_t>(value);
        extended_in_zp = sign_extend<int48_t>(input_zp);
    }

    value = value - extended_in_zp;
    int c = (per_channel) ? index[rank(input) - 1] : 0;
    int32_t result = (scale32) ?
        apply_scale_32(value, multiplier[c], shift[c], double_round) :
        apply_scale_16(value, multiplier[c], shift[c]);

    if (output_unsigned) {
        int32_t extended_out_zp = zero_extend<int32_t>(output_zp);
        result = apply_add_s<int32_t>(result, extended_out_zp);
        out_t out = static_cast<out_t>(apply_clip_u<i32_t>(result,
                                                           minimum_u<out_t>(),
                                                           maximum_u<out_t>()));
    }
    else {
        int32_t extended_out_zp = sign_extend<int32_t>(output_zp);
        result = apply_add_s<int32_t>(result, extended_out_zp);
        out_t out = static_cast<out_t>(apply_clip_s<i32_t>(result,
                                                           minimum_s<out_t>(),
                                                           maximum_s<out_t>()));
    }
    tensor_write<out_t>(output, shape, index, out);
}