summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/Makefile2
-rw-r--r--src/utils/fpconstants.h28
-rw-r--r--src/utils/fpmath.h47
-rw-r--r--src/utils/utils.c122
-rw-r--r--src/utils/utils.h116
5 files changed, 125 insertions, 190 deletions
diff --git a/src/utils/Makefile b/src/utils/Makefile
index 912590c..71d9817 100644
--- a/src/utils/Makefile
+++ b/src/utils/Makefile
@@ -32,7 +32,7 @@ dirstack_$(sp) := $(d)
d := $(DIR)
# Sources
-SRCS_$(d) := errors.c
+SRCS_$(d) := errors.c utils.c
# Append to sources for component
SOURCES += $(addprefix $(d), $(SRCS_$(d)))
diff --git a/src/utils/fpconstants.h b/src/utils/fpconstants.h
deleted file mode 100644
index b810274..0000000
--- a/src/utils/fpconstants.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * This file is part of LibCSS.
- * Licensed under the MIT License,
- * http://www.opensource.org/licenses/mit-license.php
- * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
- */
-
-#ifndef css_utils_fpconstants_h_
-#define css_utils_fpconstants_h_
-
-/* Useful angles */
-#define F_PI_2 0x00000648 /* 1.5708 (PI/2) */
-#define F_PI 0x00000c91 /* 3.1415 (PI) */
-#define F_3PI_2 0x000012d9 /* 4.7124 (3PI/2) */
-#define F_2PI 0x00001922 /* 6.2831 (2 PI) */
-
-#define F_90 0x00016800 /* 90 */
-#define F_180 0x0002d000 /* 180 */
-#define F_270 0x00043800 /* 270 */
-#define F_360 0x0005a000 /* 360 */
-
-#define F_100 0x00019000 /* 100 */
-#define F_200 0x00032000 /* 200 */
-#define F_300 0x0004b000 /* 300 */
-#define F_400 0x00064000 /* 400 */
-
-#endif
-
diff --git a/src/utils/fpmath.h b/src/utils/fpmath.h
deleted file mode 100644
index c4db605..0000000
--- a/src/utils/fpmath.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * This file is part of LibCSS.
- * Licensed under the MIT License,
- * http://www.opensource.org/licenses/mit-license.php
- * Copyright 2008 John-Mark Bell <jmb@netsurf-browser.org>
- */
-
-#ifndef css_utils_fpmath_h_
-#define css_utils_fpmath_h_
-
-#include <stdint.h>
-
-/* 22:10 fixed point math */
-typedef int32_t fixed;
-
-/* Add two fixed point values */
-#define FADD(a, b) ((a) + (b))
-/* Subtract two fixed point values */
-#define FSUB(a, b) ((a) - (b))
-/* Multiply two fixed point values */
-#define FMUL(a, b) (((a) * (b)) >> 10)
-/* Divide two fixed point values */
-#define FDIV(a, b) (((a) << 10) / (b))
-
-/* Add an integer to a fixed point value */
-#define FADDI(a, b) ((a) + ((b) << 10))
-/* Subtract an integer from a fixed point value */
-#define FSUBI(a, b) ((a) - ((b) << 10))
-/* Multiply a fixed point value by an integer */
-#define FMULI(a, b) ((a) * (b))
-/* Divide a fixed point value by an integer */
-#define FDIVI(a, b) ((a) / (b))
-
-/* Convert a floating point value to fixed point */
-#define FLTTOFIX(a) ((fixed) ((a) * (float) (1 << 10)))
-/* Convert a fixed point value to floating point */
-#define FIXTOFLT(a) ((float) (a) / (float) (1 << 10))
-
-/* Convert an integer to a fixed point value */
-#define INTTOFIX(a) ((a) << 10)
-/* Convert a fixed point value to an integer */
-#define FIXTOINT(a) ((a) >> 10)
-
-#include "utils/fpconstants.h"
-
-#endif
-
diff --git a/src/utils/utils.c b/src/utils/utils.c
new file mode 100644
index 0000000..a8d0cb5
--- /dev/null
+++ b/src/utils/utils.c
@@ -0,0 +1,122 @@
+/*
+ * This file is part of LibCSS.
+ * Licensed under the MIT License,
+ * http://www.opensource.org/licenses/mit-license.php
+ * Copyright 2007-9 John-Mark Bell <jmb@netsurf-browser.org>
+ */
+
+#include "utils/utils.h"
+
+css_fixed number_from_css_string(const css_string *string,
+ bool int_only, size_t *consumed)
+{
+ size_t len;
+ const uint8_t *ptr;
+ int sign = 1;
+ int32_t intpart = 0;
+ int32_t fracpart = 0;
+ int32_t pwr = 1;
+
+ if (string == NULL || string->len == 0 || consumed == NULL)
+ return 0;
+
+ len = string->len;
+ ptr = string->data;
+
+ /* number = [+-]? ([0-9]+ | [0-9]* '.' [0-9]+) */
+
+ /* Extract sign, if any */
+ if (ptr[0] == '-') {
+ sign = -1;
+ len--;
+ ptr++;
+ } else if (ptr[0] == '+') {
+ len--;
+ ptr++;
+ }
+
+ /* Ensure we have either a digit or a '.' followed by a digit */
+ if (len == 0) {
+ *consumed = 0;
+ return 0;
+ } else {
+ if (ptr[0] == '.') {
+ if (len == 1 || ptr[1] < '0' || '9' < ptr[1]) {
+ *consumed = 0;
+ return 0;
+ }
+ } else if (ptr[0] < '0' || '9' < ptr[0]) {
+ *consumed = 0;
+ return 0;
+ }
+ }
+
+ /* Now extract intpart, assuming base 10 */
+ while (len > 0) {
+ /* Stop on first non-digit */
+ if (ptr[0] < '0' || '9' < ptr[0])
+ break;
+
+ /* Prevent overflow of 'intpart'; proper clamping below */
+ if (intpart < (1 << 22)) {
+ intpart *= 10;
+ intpart += ptr[0] - '0';
+ }
+ ptr++;
+ len--;
+ }
+
+ /* And fracpart, again, assuming base 10 */
+ if (int_only == false && len > 1 && ptr[0] == '.' &&
+ ('0' <= ptr[1] && ptr[1] <= '9')) {
+ ptr++;
+ len--;
+
+ while (len > 0) {
+ if (ptr[0] < '0' || '9' < ptr[0])
+ break;
+
+ if (pwr < 1000000) {
+ pwr *= 10;
+ fracpart *= 10;
+ fracpart += ptr[0] - '0';
+ }
+ ptr++;
+ len--;
+ }
+ fracpart = ((1 << 10) * fracpart + pwr/2) / pwr;
+ if (fracpart >= (1 << 10)) {
+ intpart++;
+ fracpart &= (1 << 10) - 1;
+ }
+ }
+
+ *consumed = ptr - string->data;
+
+ if (sign > 0) {
+ /* If the result is larger than we can represent,
+ * then clamp to the maximum value we can store. */
+ if (intpart >= (1 << 21)) {
+ intpart = (1 << 21) - 1;
+ fracpart = (1 << 10) - 1;
+ }
+ }
+ else {
+ /* If the negated result is smaller than we can represent
+ * then clamp to the minimum value we can store. */
+ if (intpart >= (1 << 21)) {
+ intpart = -(1 << 21);
+ fracpart = 0;
+ }
+ else {
+ intpart = -intpart;
+ if (fracpart) {
+ fracpart = (1 << 10) - fracpart;
+ intpart--;
+ }
+ }
+ }
+
+ return (intpart << 10) | fracpart;
+}
+
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 9c6b082..6a3ddad 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -10,8 +10,6 @@
#include <libcss/types.h>
-#include "utils/fpmath.h"
-
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
@@ -29,118 +27,8 @@
#define UNUSED(x) ((x)=(x))
#endif
-static inline fixed number_from_css_string(const css_string *string,
- bool int_only, size_t *consumed)
-{
- size_t len;
- const uint8_t *ptr;
- int sign = 1;
- int32_t intpart = 0;
- int32_t fracpart = 0;
- int32_t pwr = 1;
-
- if (string == NULL || string->len == 0 || consumed == NULL)
- return 0;
-
- len = string->len;
- ptr = string->data;
-
- /* number = [+-]? ([0-9]+ | [0-9]* '.' [0-9]+) */
-
- /* Extract sign, if any */
- if (ptr[0] == '-') {
- sign = -1;
- len--;
- ptr++;
- } else if (ptr[0] == '+') {
- len--;
- ptr++;
- }
-
- /* Ensure we have either a digit or a '.' followed by a digit */
- if (len == 0) {
- *consumed = 0;
- return 0;
- } else {
- if (ptr[0] == '.') {
- if (len == 1 || ptr[1] < '0' || '9' < ptr[1]) {
- *consumed = 0;
- return 0;
- }
- } else if (ptr[0] < '0' || '9' < ptr[0]) {
- *consumed = 0;
- return 0;
- }
- }
-
- /* Now extract intpart, assuming base 10 */
- while (len > 0) {
- /* Stop on first non-digit */
- if (ptr[0] < '0' || '9' < ptr[0])
- break;
-
- /* Prevent overflow of 'intpart'; proper clamping below */
- if (intpart < (1 << 22)) {
- intpart *= 10;
- intpart += ptr[0] - '0';
- }
- ptr++;
- len--;
- }
-
- /* And fracpart, again, assuming base 10 */
- if (int_only == false && len > 1 && ptr[0] == '.' &&
- ('0' <= ptr[1] && ptr[1] <= '9')) {
- ptr++;
- len--;
-
- while (len > 0) {
- if (ptr[0] < '0' || '9' < ptr[0])
- break;
-
- if (pwr < 1000000) {
- pwr *= 10;
- fracpart *= 10;
- fracpart += ptr[0] - '0';
- }
- ptr++;
- len--;
- }
- fracpart = ((1 << 10) * fracpart + pwr/2) / pwr;
- if (fracpart >= (1 << 10)) {
- intpart++;
- fracpart &= (1 << 10) - 1;
- }
- }
-
- *consumed = ptr - string->data;
-
- if (sign > 0) {
- /* If the result is larger than we can represent,
- * then clamp to the maximum value we can store. */
- if (intpart >= (1 << 21)) {
- intpart = (1 << 21) - 1;
- fracpart = (1 << 10) - 1;
- }
- }
- else {
- /* If the negated result is smaller than we can represent
- * then clamp to the minimum value we can store. */
- if (intpart >= (1 << 21)) {
- intpart = -(1 << 21);
- fracpart = 0;
- }
- else {
- intpart = -intpart;
- if (fracpart) {
- fracpart = (1 << 10) - fracpart;
- intpart--;
- }
- }
- }
-
- return (intpart << 10) | fracpart;
-}
+css_fixed number_from_css_string(const css_string *string, bool int_only,
+ size_t *consumed);
static inline bool isDigit(uint8_t c)
{