MCPcopy Create free account
hub / github.com/Meituan-Dianping/SQLAdvisor / do_add

Function do_add

strings/decimal.c:1810–1888  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1808}
1809
1810static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1811{
1812 int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1813 frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
1814 frac0= MY_MAX(frac1, frac2), intg0= MY_MAX(intg1, intg2), error;
1815 dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
1816
1817 sanity(to);
1818
1819 /* is there a need for extra word because of carry ? */
1820 x=intg1 > intg2 ? from1->buf[0] :
1821 intg2 > intg1 ? from2->buf[0] :
1822 from1->buf[0] + from2->buf[0] ;
1823 if (unlikely(x > DIG_MAX-1)) /* yes, there is */
1824 {
1825 intg0++;
1826 to->buf[0]=0; /* safety */
1827 }
1828
1829 FIX_INTG_FRAC_ERROR(to->len, intg0, frac0, error);
1830 if (unlikely(error == E_DEC_OVERFLOW))
1831 {
1832 max_decimal(to->len * DIG_PER_DEC1, 0, to);
1833 return error;
1834 }
1835
1836 buf0=to->buf+intg0+frac0;
1837
1838 to->sign=from1->sign;
1839 to->frac= MY_MAX(from1->frac, from2->frac);
1840 to->intg=intg0*DIG_PER_DEC1;
1841 if (unlikely(error))
1842 {
1843 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
1844 set_if_smaller(frac1, frac0);
1845 set_if_smaller(frac2, frac0);
1846 set_if_smaller(intg1, intg0);
1847 set_if_smaller(intg2, intg0);
1848 }
1849
1850 /* part 1 - max(frac) ... min (frac) */
1851 if (frac1 > frac2)
1852 {
1853 buf1=from1->buf+intg1+frac1;
1854 stop=from1->buf+intg1+frac2;
1855 buf2=from2->buf+intg2+frac2;
1856 stop2=from1->buf+(intg1 > intg2 ? intg1-intg2 : 0);
1857 }
1858 else
1859 {
1860 buf1=from2->buf+intg2+frac2;
1861 stop=from2->buf+intg2+frac1;
1862 buf2=from1->buf+intg1+frac1;
1863 stop2=from2->buf+(intg2 > intg1 ? intg2-intg1 : 0);
1864 }
1865 while (buf1 > stop)
1866 *--buf0=*--buf1;
1867

Callers 2

decimal_addFunction · 0.85
decimal_subFunction · 0.85

Calls 1

max_decimalFunction · 0.85

Tested by

no test coverage detected