Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/support/primitives.c

raw
1 #include "otfcc/primitives.h"
2 #include "bin-io.h"
3 #include <math.h>
4
5 // f2dot14 type
6 double otfcc_from_f2dot14(const f2dot14 x) {
7 return x / 16384.0;
8 }
9 int16_t otfcc_to_f2dot14(const double x) {
10 return round(x * 16384.0);
11 }
12
13 // F16.16 (fixed) type
14 double otfcc_from_fixed(const f16dot16 x) {
15 return x / 65536.0;
16 }
17 f16dot16 otfcc_to_fixed(const double x) {
18 return round(x * 65536.0);
19 }
20
21 // F16.16 arith
22 // Clamp: remove too-large values
23 static INLINE f16dot16 clamp(int64_t value) {
24 int64_t tmp = value;
25 if (tmp < (int64_t)f16dot16_negativeIntinity) tmp = (int64_t)f16dot16_negativeIntinity;
26 if (tmp > (int64_t)f16dot16_infinity) tmp = (int64_t)f16dot16_infinity;
27 return (f16dot16)tmp;
28 }
29
30 f16dot16 otfcc_f1616_add(f16dot16 a, f16dot16 b) {
31 return a + b;
32 }
33 f16dot16 otfcc_f1616_minus(f16dot16 a, f16dot16 b) {
34 return a - b;
35 }
36
37 f16dot16 otfcc_f1616_multiply(f16dot16 a, f16dot16 b) {
38 int64_t tmp = (int64_t)a * (int64_t)b + f16dot16_k;
39 f16dot16 product = clamp(tmp >> f16dot16_precision);
40 return product;
41 }
42
43 static INLINE f16dot16 divide(int64_t a, int32_t b) {
44 if (b == 0) {
45 if (a < 0)
46 return f16dot16_negativeIntinity;
47 else
48 return f16dot16_infinity;
49 }
50
51 if ((a < 0 != b < 0)) {
52 a -= b / 2;
53 } else {
54 a += b / 2;
55 }
56
57 return (f16dot16)(clamp(a / b));
58 }
59
60 f16dot16 otfcc_f1616_muldiv(f16dot16 a, f16dot16 b, f16dot16 c) {
61 int64_t tmp = (int64_t)a * (int64_t)b + f16dot16_k;
62 return divide(tmp, c);
63 }
64 f16dot16 otfcc_f1616_divide(f16dot16 a, f16dot16 b) {
65 return divide((int64_t)a << f16dot16_precision, b);
66 }